简体   繁体   English

ArrayList 的 ArrayList 添加和检索元素

[英]ArrayList of ArrayLists adding and retrieving elements

I have an Integer ArrayList(mainList) which will have Integer Arrays(subList) inside it, I am trying to add integer array elements to mainList and display them at a later time.我有一个 Integer ArrayList(mainList) 里面有 Integer Arrays(subList),我正在尝试将整数数组元素添加到 mainList 并在以后显示它们。 Adding subLists to mainList and display all elements from subList.将 subLists 添加到 mainList 并显示 subList 中的所有元素。

2 subLists = {1,2,4},{3,2,1}
mainList =[{1,2,4},{3,2,1}]
Display : 1,2,4,3,2,1
  1. How to easily retrieve elements from mainList如何轻松地从 mainList 中检索元素
  2. How to add subLists at a time without looping如何一次添加子列表而不循环

The following is the way I am trying to add subLists to mainList以下是我尝试将 subLists 添加到 mainList 的方式

//Adding elements
 ArrayList<ArrayList<Integer>> mainList = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> subList = new ArrayList<Integer>();
for(int i=0;i<10;i++) {
    for(int j=i+1;j<10;j++){
        //Do something and add elements to subList
        subList.add(element[j]) }
        mainList.add(subList);

        // When I clear subList mainList is also getting cleared. I want to add the elements of subList to mainList. I was able to do it with loops but how to do this way
        subList.clear();
    }

    //Printing, How do I get the elements from mainList which will have subLists
    for(Integer i:mainList){ 
        System.out.println(i);
    }
}

Your code你的代码

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

doesn't compile, see my edit in below code:无法编译,请参阅以下代码中的编辑:

import com.sun.deploy.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> mainList = new ArrayList<ArrayList<Integer>>();

        Integer[] sub1 = {1,2,4};
        Integer[] sub2 = {3,2,1};
        ArrayList<Integer> subList1 = new ArrayList<>(Arrays.asList(sub1));
        ArrayList<Integer> subList2 = new ArrayList<>(Arrays.asList(sub2));

        mainList.add(subList1);
        mainList.add(subList2); //[[1, 2, 4], [3, 2, 1]]


        ArrayList<Integer> intValues = new ArrayList<>();
        for(ArrayList<Integer> inner: mainList){
            intValues.addAll(inner); // add inner list elements to Integer list
        }

        List<String> strings =  intValues.stream().map(Object::toString)
                .collect(Collectors.toList());  // converts to string list

        System.out.println(StringUtils.join(strings, ","));  // prints comma separated string
    }
}

Output:输出:

1,2,4,3,2,1

My explanation is in the code comments.我的解释在代码注释中。

So, you have an array list, of an array list, of integers.所以,你有一个数组列表,一个数组列表,一个整数。 mailList is an array list of subList. mailList 是 subList 的数组列表。 subList is an array list of Integers. subList 是整数的数组列表。 So, to get one element you would have to get a get.因此,要获得一个元素,您必须获得一个元素。 I'll show you.我会给你看。

ArrayList temp = mailList.get(i);
temp.get(j);

So, using the advanced for loop, you would do this to access every element.因此,使用高级 for 循环,您可以访问每个元素。

for(ArrayList i:mailList)
    for(Integer j:i)
        System.out.print(j);

That code would print all Integers in the 2D array list.该代码将打印二维数组列表中的所有整数。 I'll now code it with a normal for loop.我现在将使用普通的 for 循环对其进行编码。

for(int i = 0; i < mailList.length; i++){
    ArrayList subList = mailList.get(i);
    for(int j = 0; j < subList.length; j++){
        System.out.print(subList.get(j));
    }
}

That code would do the exact same thing.该代码将执行完全相同的操作。

So, summary, you get the array list from the array list of array lists, then you get the integer from the array list that you got from the array list of array lists.所以,总而言之,你从数组列表的数组列表中获取数组列表,然后从数组列表中的数组列表中获取整数。 Hope this helps!希望这可以帮助! :) :)

(Yes, I know the array-list-ception is kind of confusing, but it's the only way I can think of to explain) (是的,我知道 array-list-ception 有点令人困惑,但这是我能想到的唯一解释方法)

I have a solution!我有办法!

class Main {

    public static void main(String[] args) {

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

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

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

            subList = new ArrayList < Integer > ();

            for (int j = i + 1; j < 10; j++) {
                subList.add(element[j])
            }
            mainList.add(subList);
        }

    }
}

Here is a generic solution to build an ArrayList of hetergeous ArrayList such as one might encounter in a SQL Query result set.这是构建异构 ArrayList 的 ArrayList 的通用解决方案,例如在 SQL 查询结果集中可能遇到的情况。

package com.mwycombe.arraylistdatatest;

import java.util.ArrayList;

/**
 *
 * @author MWycombe
 */
public class ArrayListDataTest {
    ArrayList<Object> tableData;
    public ArrayListDataTest(){
        int i, j;
        // set up trasnposed table of data
        tableData = new ArrayList<>();
        // set up 6 rows with 3 cols - each column is the same.
        ArrayList<Integer> col1 = new ArrayList<>();
        ArrayList<String> col2 = new ArrayList<>();
        ArrayList<Float> col3 = new ArrayList<>();
        // how build 6 arbitrayr entries for each column and add to the tableData array list
        for (i = 0; i < 6; i++){
            col1.add(i );
            col3.add((float)i + i/10);
        }
        for (i = 0; i < 6; i++) {
            col2.add(String.valueOf(i));
        }
        tableData.add(col1);
        tableData.add(col2);
        tableData.add(col3);
        System.out.println ("Array lists built.");
        System.out.println ("Size tableData, col1, col2, col3");
        //print out class of each column - 1st row entry cell
        for (i=0; i<3; i++){
            System.out.println("Col type " + i + " " + getColumnClass(i));
        }
    }
    private Class getColumnClass(int c){
        System.out.println("Which col? " + c);
        return getValueAt(0,c).getClass();
    }
    private Object getValueAt(int row, int col) {
        System.out.println(("in getValue row: " + row + " col: " + col));
        return ((ArrayList)(tableData.get(col))).get(row);
    }
    public static void main(String[] args) {
        ArrayListDataTest aldt = new ArrayListDataTest();
    }
}

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

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