Sort by

recency

|

489 Discussions

|

  • + 0 comments
    //https://www.hackerrank.com/challenges/java-list
    
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            
            int n = scanner.nextInt();
            List<Integer> list = new ArrayList<>();
            
            for(int i = 0; i < n; i++)
                list.add(scanner.nextInt());
                
            int queries = scanner.nextInt();
            for(int i = 0; i < queries; i++) {
                String query = scanner.next();
                
                int index = scanner.nextInt();
                if (query.equals("Insert")){
                    int newValue = scanner.nextInt();
                    list.add(index, newValue); 
                }
                else
                    list.remove(index);
            }
            
            scanner.close();
            
            for(Integer num : list)
                System.out.print(num + " ");
        }
    }
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
        
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            
                int size = sc.nextInt();
                ArrayList<Integer> elements = new ArrayList<>();   
                
                for (int i = 0; i < size; i++) {
                    elements.add(sc.nextInt());
                }
                
                int numberQueries = sc.nextInt();
                
                for (int j = 0; j < numberQueries; j++) {
                    
                    String query = sc.next();
                    if (query.equals("Insert")){
                        int position = sc.nextInt();
                        int replace = sc.nextInt();
                        elements.add(position, replace);
    
                    }
                    else{
                        elements.remove(sc.nextInt());
                    }
                }
                sc.close();
                
                for (Integer integer : elements) {
                    System.out.print(integer + " ");
                }     
        }
    }
    
  • + 0 comments
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class JavaList {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            // tao 1 list kieu so nguyen va dang arraylist
            List<Integer> list = new ArrayList<>();
    
            // them cac phan tu vao list
            for (int i = 0; i < n; i++) {
                list.add(scanner.nextInt());
            }
            int Queue = scanner.nextInt();
            scanner.nextLine();
    
            for(int i = 0; i < Queue; i++){
                String Command = scanner.next();
                if(Command.equals("Insert")) {
                    int a = scanner.nextInt();
                    int b = scanner.nextInt();
                    list.add(a,b);      // chen a vao vi tri b trong listarray
                } else if(Command.equals("Delete")) {
                    int pos = scanner.nextInt();
                    list.remove(pos);
                }
            }
            for(int num:list){
                System.out.print(num+" ");
            }
        }
    }
    
  • + 0 comments

    May I know what is the issue with my code? It passed Test Case 3 but failed the rest of the Test Cases.

    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            sc.nextLine();
    
            String[] s = sc.nextLine().split(" ");
            List<Integer> L = new ArrayList<>();
            for (String num : s) {
                L.add(Integer.parseInt(num));
            }
    
            int q = sc.nextInt();
            sc.nextLine();
            while (q > 0) {
                String operation = sc.nextLine();
                if (operation.equalsIgnoreCase("Insert")) {
                    String[] xy = sc.nextLine().split(" ");
                    L.add(Integer.parseInt(xy[0]), Integer.parseInt(xy[1]));
                }
                else {
                    int index = sc.nextInt();
                    L.remove(index);
                }
                q--;
            }
    
            for (Integer num : L) {
                System.out.printf("%d ", num);
            }
        }
    
  • + 0 comments

    Use stream

    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    
    import static java.util.stream.Collectors.toList;
    
    public class Solution {
    
        private static final String STRING_INSERT = "Insert";
        private static final String STRING_DELETE = "Delete";
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            int n = Integer.parseInt(scanner.nextLine()); // n is the number initial of elements in the list
    
            List<Integer> list = getIntegerList(scanner.nextLine());
    
            int numberOfQuerys = Integer.parseInt(scanner.nextLine());
    
            while (numberOfQuerys-- > 0) {
                String query = scanner.nextLine();
                List<Integer> queryList = getIntegerList(scanner.nextLine());
    
                if (query.equals(STRING_INSERT)) {
                    list.add(queryList.get(0), queryList.get(1));
                } else if (query.equals(STRING_DELETE)) {
                    list.remove((int) queryList.get(0)); // int -> index to delete, Integer -> object to delete
                }
            }
            scanner.close();
            list.forEach(number-> System.out.print(number + " "));
        }
    
        private static List<Integer> getIntegerList(String scanner) {
            return Arrays.stream(scanner.split(" ")).map(Integer::parseInt).collect(toList());
        }
    }