Sort by

recency

|

493 Discussions

|

  • + 0 comments

    public class Solution {

    private List<Integer> theList;
    private Scanner sc;
    
    public Solution(Scanner sc) {
    
        this.theList = new ArrayList<>();
        this.sc = sc;
    
        int sizeList = sc.nextInt();
        init(sizeList, sc);
    }
    
    private void init(int sizeList, Scanner sc) {
    
        if (sizeList > 0) {
    
            theList.add(sc.nextInt());
            init(sizeList - 1, sc);
        }
    }
    
    public void choose(String action){
    
        int index = sc.nextInt();
    
        if(action.equalsIgnoreCase("insert")){
    
            int element = sc.nextInt();
            theList.add(index, element);
    
        } else theList.remove(index);
    }
    
    public List<Integer> getList() {
    
        for (int element : theList) {
            System.out.print(element + " ");
        }
    
        return theList;
    }
    
    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
    
        Solution solution = 
            new Solution(sc);
    
        int queries = sc.nextInt();
    
        for(int i = 0; i < queries; i++){
    
            sc.nextLine();
            String action = sc.nextLine();
    
            solution.choose(action);
        }
    
        solution.getList();
    
    
    
    }  
    

    }

  • + 0 comments

    This is a great exercise for practicing list manipulation and handling dynamic input! funinexchange 247.com

  • + 0 comments
    import java.util.*;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            int n = scanner.nextInt();
    
            if (n < 1 || n > 4000) {
                throw new IllegalArgumentException("The entry isn't an integer between 1 and 4000");
            }
    
            List<Integer> list = new ArrayList<>();
    
            for (int i = 0; i < n; i++) {
                if (scanner.hasNextInt()) {
                    list.add(scanner.nextInt());
                } else {
                    throw new IllegalArgumentException("The entry isn't an integer");
                }
            }
    
            int q = scanner.nextInt();
    
            if (q < 1 || q > 4000) {
                throw new IllegalArgumentException("The entry isn't an integer between 1 and 4000");
            }
    
            for (int i = 0; i < q; i++) {
                String queryType = scanner.next().trim();
    
                if ("insert".equalsIgnoreCase(queryType)) {
                    int x = scanner.nextInt();
                    int y = scanner.nextInt();
    
                    if (x < 0 || x > list.size()) {
                        throw new IllegalArgumentException("Insert index out of bounds");
                    }
    
                    list.add(x, y);
    
                } else if ("delete".equalsIgnoreCase(queryType)) {
                    int x = scanner.nextInt();
    
                    if (x < 0 || x >= list.size()) {
                        throw new IllegalArgumentException("Delete index out of bounds");
                    }
    
                    list.remove(x);
                } else {
                    throw new IllegalArgumentException("Unknown query type: " + queryType);
                }
            }
    
            for (int number : list) {
                System.out.print(number + " ");
            }
            scanner.close();
        }
    }
    
  • + 0 comments

    Here is Java List solution - https://programmingoneonone.com/hackerrank-java-list-problem-solution.html

  • + 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 + " ");
        }
    }