简体   繁体   English

ArrayList扫描程序UserInput的Java ArrayList

[英]Java ArrayList of ArrayList Scanner UserInput

I am trying to store user input into ArrayList of ArrayList. 我试图将用户输入存储到ArrayList的ArrayList中。

ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();

For example, storing it as [[1,2,3],[4,5],[6,1,8]] 例如,将其存储为[[1,2,3],[4,5],[6,1,8]]
Below is my code, 下面是我的代码,

    Scanner input = new Scanner(System.in);
    System.out.println("Enter a set of numbers:");
    String line = input.nextLine();
    String[] numbers = line.split(" +");

    ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> a1 = new ArrayList<Integer>();

    for(int i=0; i<numbers.length; i++) {
        a1.add(new Integer(numbers[i]));

    }
    a.add(a1);

But then when I types it in to terminal, it becomes [[1,2,3,4,5,6,1,8]] . 但是,当我在终端输入时,它变成[[1,2,3,4,5,6,1,8]]
Thank you in advance! 先感谢您!

Try this type of i/p and below code . 尝试这种类型的i / p和以下代码。

i/p : 1 2 3-4 5-6 1 8

String[] list= line.split("-");

ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
for(int i=0; i<list.length; i++) {

        String[] number= line.split(" ");
        ArrayList<Integer> a1 = new ArrayList<Integer>();
        for(int i=0; i<numbers.length; i++) {
            a1.add(new Integer(numbers[i]));

        }
        if(a1.length>0)
           a.add(a1);
}

From above comments I am bit clear what do you want to do. 从上面的评论中,我很清楚您想做什么。

You entered 1 2 3 4 5 6 1 8 on terminal. 您在终端上输入了1 2 3 4 5 6 1 8 In your program you used split on - so I am providing some guidelines to solve your problem. 在你的程序中,您使用的分裂-所以我提供一些指引,以解决您的问题。

First, split your input on - to make the separation of arrays 首先,将输入分割为-进行数组分离

String[] numbersArray = line.split("-"); 
//e.g. input 1 2 3 + 4 5 6 + 1 8
//here you will get numbersArray as [1 2 3,4 5 6,1 8]
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();

Now, to separate individual elements of inner array use for loop 现在,要分离内部数组的各个元素,请使用for循环

for(int i=0; i<numbersArray.length; i++) {

    String[] numbers= numbersArray[i].split(" "); //split on " "
    //here you will get [1,2,3] for first iteration and 2nd [4,5,6], last [1,8]

    ArrayList<Integer> a1 = new ArrayList<Integer>();
    for(int j=0; j<numbers.length; j++) {
        a1.add(new Integer(numbers[j]));
        //here adding inner array list elements

    }
    //add to main array list 
    a.add(a1);
}

I think it's due to the wrong input you are trying to enter on console, because when I hard code the ideal input to your ArrayList it prints the result as you expected. 我认为这是由于您尝试在控制台上输入错误的输入引起的,因为当我对ArrayList进行理想的编码时,它会按预期输出结果。 Here is the sample code that I wrote: 这是我编写的示例代码:

private static void doPrint(){
    List<List<Integer>> sample=new ArrayList<List<Integer>>();
    ArrayList<Integer> a1 = new ArrayList<Integer>();
    a1.add(12);
    a1.add(23);
    ArrayList<Integer> a2 = new ArrayList<Integer>();
    a2.add(22);
    a2.add(28);
    sample.add(a1);
    sample.add(a2);

    System.out.println(sample);
}

and here is the output: 这是输出:

[[12, 23], [22, 28]]

The way you are trying to use, will not work since you add all elements to inner list. 由于您将所有元素添加到内部列表中,因此您尝试使用的方法将不起作用。

You can try some thing like following. 您可以尝试以下操作。 You needs to create following arrays. 您需要创建以下数组。

public static void main(String[] args) {
    ArrayList<ArrayList<Integer>> a = new ArrayList<>();
    int[] arr1={1,2,3}; // you need to create these arrays
    int[] arr2={4,5};
    int[] arr3={6,1,8};
    a.add(getList(arr1));
    a.add(getList(arr2));
    a.add(getList(arr3));
    System.out.println(a);
}

public static ArrayList<Integer> getList(int[] arr){
    ArrayList<Integer> list = new ArrayList<>();
    for (int anArr : arr) {
        list.add(anArr);
    }
    return list;
}

You are adding only one list to a that's why it seems like one element inside list a . 您只向a添加了一个列表,这就是为什么在list a看起来像一个元素的原因。 If you want to have multiple elements inside list a then use like this. 如果你想有内部列表中的多个元素a则这样使用。

for(int i=0; i<numbers.length; i++) {
        a1.add(new Integer(numbers[i]));
}
a.add(a1);
a.add(a1);

Output: As per input taken in your question 输出:根据您问题中输入的内容

[[1,2,3,4,5,6,1,8], [1,2,3,4,5,6,1,8]]

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

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