簡體   English   中英

ArrayList掃描程序UserInput的Java ArrayList

[英]Java ArrayList of ArrayList Scanner UserInput

我試圖將用戶輸入存儲到ArrayList的ArrayList中。

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

例如,將其存儲為[[1,2,3],[4,5],[6,1,8]]
下面是我的代碼,

    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);

但是,當我在終端輸入時,它變成[[1,2,3,4,5,6,1,8]]
先感謝您!

嘗試這種類型的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);
}

從上面的評論中,我很清楚您想做什么。

您在終端上輸入了1 2 3 4 5 6 1 8 在你的程序中,您使用的分裂-所以我提供一些指引,以解決您的問題。

首先,將輸入分割為-進行數組分離

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>>();

現在,要分離內部數組的各個元素,請使用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);
}

我認為這是由於您嘗試在控制台上輸入錯誤的輸入引起的,因為當我對ArrayList進行理想的編碼時,它會按預期輸出結果。 這是我編寫的示例代碼:

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);
}

這是輸出:

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

由於您將所有元素添加到內部列表中,因此您嘗試使用的方法將不起作用。

您可以嘗試以下操作。 您需要創建以下數組。

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;
}

您只向a添加了一個列表,這就是為什么在list a看起來像一個元素的原因。 如果你想有內部列表中的多個元素a則這樣使用。

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

輸出:根據您問題中輸入的內容

[[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