简体   繁体   English

如何将多个ArrayList放入ArrayList Java中

[英]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). 我正在尝试创建一个父ArrayList,其中包含每个子索引的子ArrayList。 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. 输入描述:输入的第一个整数( int T )告诉我们要创建多少个子ArrayList,这也可以看作是父ArrayList的大小或要遵循的输入行数。 All lines after this point will be the integers we want to store within each sub arraylist. 此点之后的所有行将是我们要存储在每个子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())); 问题描述:在以下行上出现错误: 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. 我试过使用.addAll,但这也不起作用。 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) . java.util.List boolean add(E e) The add method returns boolean , not a list so you cannot add it. add方法返回boolean而不是列表,因此您无法添加它。 You are doing this: 您正在执行此操作:

parentList.add(i, true/false)

The type of parentList is List<List<Integer>> . parentList的类型为List<List<Integer>> You can only add items of type List<Integer> . 您只能添加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. 首先,不要在声明时初始化subList,因为您将再次在for循环中对其进行初始化。 So, new ArrayList() object is never used. 因此,从不使用new ArrayList()对象。

To your question, why are you doing q.nextInt() once in a loop. 对于您的问题,为什么要循环执行一次q.nextInt()。 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. 正如其他人所提到的,您无法像完成操作一样将子列表添加到parentList。

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 :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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