简体   繁体   中英

How to put Multiple ArrayLists inside an ArrayList Java

I am trying to create a parent ArrayList which contains a sub ArrayList for each of its index(s). My code involves filling the sub lists by using Scanner Input. See input example below.

Input Description: The first integer inputed ( int T ) tells us how many sub ArrayLists we are going to make,this could also be seen as the size of the parent ArrayList or as how many lines of input are going to follow. All lines after this point will be the integers we want to store within each sub arraylist.

Example Input

2  
5 4 2 9  
1 3 3 7  

Expected Output

SubString 0 contains 5 4 2 9
SubString 1 contains 1 3 3 7

Problem Description: I get an error on the following line parentList.add(i, subList.add(q.nextInt())); . I believe I am not following syntax. I have tried using .addAll but that doesn't work either. Can anyone tell me what I am doing wrong? Thanks for help in advance.

import java.util.*;
import java.io.*;

public class Serials {


    public static void main(String[] args) {

        int T = 0; //First Integer to be inputed 
        //tells me how many sub ArrayLists I will have

        List<List<Integer>> parentList =new ArrayList();  //parentList
        List<Integer> subList = new ArrayList();          //subList

        Scanner q=new Scanner(System.in);
        System.out.println("How many substrings do we need");
        T = q.nextInt(); //Scanning in T Value

        for(int i=0; i<T; i++) {

    subList = new ArrayList<Integer>();
    subList.add(q.nextInt());
    parentList.add(i, subList);  


    }


    System.out.println( "SubString 0 contains" + parentList.get(0));
    System.out.println( "SubString 1 contains" + parentList.get(1));


    }
}

java.util.List boolean add(E e) . The add method returns boolean , not a list so you cannot add it. You are doing this:

parentList.add(i, true/false)

The type of parentList is List<List<Integer>> . You can only add items of type List<Integer> . For example (to at least get it to compile) you could do:

subList.add(q.nextInt());
parentList.add(i, subList);

As mentioned earlier, add for lists don't return a list but rather a boolean.

It would be best to modify the inner array list first, and then add that in once you're finished adding in all of the entries.

Like this:

List<Integer> subList;
for(int i=0; i < T; i++) {
    subList = new ArrayList<Integer>();
    //logic for adding things to subList
    parentList.add(i, subList);
}

First thing, do not initialize your subList while declaration as your are again initializing it in for loop. So, new ArrayList() object is never used.

To your question, why are you doing q.nextInt() once in a loop. It means you are not expecting more than one number per sublist.

As mentioned by others you cannot add sublist to parentList as you have done.

I have also changed delimiter to , instead of space.

I have modified the code keeping these things in mind.

import java.util.*;

 public class Serials {


public static void main(String[] args) {

    int T = 0; //First Integer to be inputed 
    //tells me how many sub ArrayLists I will have

    List<List<Integer>> parentList =new ArrayList();  //parentList
    List<Integer> subList = null;          //subList
    String[] strArr = null;

    Scanner q=new Scanner(System.in);
    System.out.println("How many substrings do we need");
    T = q.nextInt(); //Scanning in T Value

    for(int i=0; i<T; i++) {
        System.out.println("Enter comma seperated integer values for list "+(i+1));
        strArr = q.next().split(",");
        subList = new ArrayList<>();
        for(String str : strArr){
            try{
            subList.add(Integer.parseInt(str));
            }catch(NumberFormatException e){
                System.out.println("Are you share that was an integer?");
                System.exit(0);
            }
        }
        parentList.add(i, subList);  
        }
        parentList.add(i, subList);  
    }


    System.out.println( "SubString 0 contains" + parentList.get(0));
System.out.println( "SubString 1 contains" + parentList.get(1));


    }
}
import java.util.*;
import java.io.*;

public class Serials {


public static void main(String[] args) {        
    int T = 0; //First Integer to be inputed 
    //tells me how many sub ArrayLists I will have

    List<List<Integer>> parentList =new ArrayList();  //parentList
    List<Integer> subList = new ArrayList();          //subList

    Scanner q=new Scanner(System.in);
    System.out.println("How many substrings do we need");
    T = Integer.parseInt(q.nextLine()); //Scanning in T Value

    for(int i=0; i<T; i++) {
    String x = q.nextLine();
    String[] arr= x.split(" ");
    subList = new ArrayList<Integer>();
    for(int j=0;j<arr.length;j++)
    {

    subList.add(Integer.parseInt(arr[j]));

    }
    parentList.add(i, subList);  
    }


    System.out.println( "SubString 0 contains" + parentList.get(0));
    System.out.println( "SubString 1 contains" + parentList.get(1));
    }
}

This will allow input and output in the exact format that you wanted. Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM