简体   繁体   中英

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. Adding subLists to mainList and display all elements from 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
  2. How to add subLists at a time without looping

The following is the way I am trying to add subLists to 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. subList is an array list of Integers. 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(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(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)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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