简体   繁体   English

Java中的2D ArrayList

[英]2D ArrayList in java

I have a 2D ArrayList and I want to take one specific ArrayList from those that are inside the 2D ArrayList. 我有一个2D ArrayList,我想从2D ArrayList内部的那些中获取一个特定的ArrayList。

Is this possible? 这可能吗? If yes how can I do this? 如果是,我该怎么做? How exactly does the 2D arrayList work, I have read a lot about it but I still can't understand. 2D arrayList是如何工作的,我已经读了很多,但是我还是不明白。 My 2D ArrayList has this form: 我的2D ArrayList具有以下形式:

ArrayList<ArrayList<Items>> arrayList = new ArrayList<ArrayList<Items>>();

for (int i = 0; i < 10; i++) {
    ArrayList<Items> row = new ArrayList<Items>();

    for (int j = 0; j < 500; j++)
    {
        // create the items...
    }
}

Yes this is possible. 是的,这是可能的。

Since you have an ArrayList< ArrayList<Items> > when you call arrayList.get() this will return an ArrayList. 由于您在调用arrayList.get()时具有ArrayList< ArrayList<Items> > ,因此将返回ArrayList。 Then you can go ahead and do whatever you need. 然后,您可以继续执行所需的任何操作。

For instance: 例如:

for(int i = 0; i < arrayList.length(); i++){
   ArrayList<Items> innerList = arrayList.get(i);
   for(int j =0; j < 10; j++){
       innerList.add(new Items());
   }
 }

This will take your ten ArrayLists you made above and fill each of them with ten items. 这将采用您在上面创建的十个ArrayList,并用十个项目填充它们。

Starting from the top: 从顶部开始:

Is this possible?

Yes, quite simple actually: 是的,实际上很简单:

ArrayList innerList = arrayList.get(listIndex);

How does the 2D ArrayList work?

Basically, it functions as an array of arrays, so that when you access one element of the 2D list, returns an ArrayList, which you must further call get() on to get the element you want. 基本上,它用作数组的数组,因此当您访问2D列表的一个元素时,将返回一个ArrayList,您必须进一步调用get()以获取所需的元素。

Example: 例:

ArrayList innerList = (ArrayList) arrayList.get(listIndex); // returns an arraylist
Item item = (Item) innerList.get(innerListIndex); // returns your item

Yes, of course, you can pick ArrayList from an ArrayList. 是的,当然,您可以从ArrayList中选择ArrayList。 In order to do this you can follow these 2 steps: 为此,您可以按照以下两个步骤操作:

  1. Initialize and declare 2D ArrayList 初始化并声明2D ArrayList

As per your question, you have 10 rows (i = 10) and 500 columns (j=500). 根据您的问题,您有10行(i = 10)和500列(j = 500)。

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

    for (int i = 0; i < 10; i++) {
        ArrayList<Integer> row = new ArrayList<Integer>();

        for (int j = 0; j < 500; j++)
        {
            //add any integers into "row" ArrayList
            row.add(2);
        }
        //add this above 1d "row" ArrayList to our 2D "arraylist" 
        arrayList.add(row);
    }
  1. Fetch elements from an ArrayList 从ArrayList获取元素
    for (int i = 0; i < arrayList.size(); i++) { 
        for (int j = 0; j < arrayList.get(i).size(); j++) { 
            System.out.print(arrayList.get(i).get(j) + " "); 
        } 
        System.out.println(); 
    }

EDIT: this answer is for the original version of the question (which was a little difficult to understand). 编辑:这个答案是针对问题的原始版本的(这有点难以理解)。

If I understand you correctly, you want to flatten many lists into one. 如果我对您的理解正确,则希望将许多列表拼合为一个列表。

List<List<String>> listOfLists = new ArrayList<List<String>>();
for (int i = 0; i < 5; i++) {
    List<String> thisList = Arrays.asList("a", "b", "c");
    listOfLists.add(thisList);
}

// flatten
List<String> flattenedListOfStrings = new ArrayList<String>();
for (List<String> listOfString : listOfLists) {
    flattenedListOfStrings.addAll(listOfString);
}

// test
for (String string : flattenedListOfStrings) {
    System.out.print(string);
}

Outputs: 输出:

abcabcabcabcabc

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

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