Nicholas Syhler
Nicholas Syhler

Reputation: 11

How to split an ArrayList<E> into ArrayList<ArrayList<E>>?

I'm pretty new to java and trying to create an ArrayList<ArrayList<Integer>> from ArrayList<Integer> (splitting 28800 data points into pieces of 300).

public static ArrayList<ArrayList<Integer>> createListOfArrays(ArrayList<Integer> list) {

    ArrayList<ArrayList<Integer>> listOfArrays = new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> temp = new ArrayList<Integer>();

    for (int i = 0; i < list.size(); i++) {
        int arrayInt = list.get(i);
        temp.add(arrayInt);

        if (temp.size() % 300 == 0) {
            listOfArrays.add(temp);
            temp.clear();
        }
    }

    return listOfArrays;    

    System.out.println(listOfArrays.size());
    System.out.println(listOfArrays.get(0).size());
}

My print statements give me the right size of 96 ArrayLists but they are all empty. If I remove temp.clear() they all consist of 28800 Integers.

I need 96 * 300 different ArrayLists in an Arraylist.

Upvotes: 0

Views: 80

Answers (3)

SacJn
SacJn

Reputation: 777

for (int i = 0; i < list.size(); i++) {
    Integer arrayInt = list.get(i);
    temp.add(arrayInt);

    if (temp.size() % 300 == 0) {
        listOfArrays.add(temp);
        temp = new ArrayList<>();
    }
}

Dont use primitive int while retrieving element from list. Because it will unnecessarily cause unboxing and then autoboxing again.

Rather than relying on list.size() % 300 == 0, You should change your logic also.

You can even use arraylist method subList.

Upvotes: 0

digidude
digidude

Reputation: 289

 public static ArrayList<ArrayList<Integer>> createListOfArrays(ArrayList<Integer> list) {

 ArrayList<ArrayList<Integer>> listOfArrays = new ArrayList<ArrayList<Integer>>();
 ArrayList<Integer> temp = new ArrayList<Integer>();

 for (int i = 0; i < list.size(); i++) {
     int arrayInt = list.get(i);
     temp.add(arrayInt);

     if (i % 300 == 0) {
         listOfArrays.add(temp);
         //assigning new ArrayList object in temp
         temp = new ArrayList<Integer>();
     }

 }
 //adding last batch of temp to listOfArrays
 if(temp.size()>0){
         listOfArrays.add(temp);
  }

 return listOfArrays;    

 System.out.println(listOfArrays.size());
 System.out.println(listOfArrays.get(0).size());
 }

This should do the trick for you. Explanation provided in comments above.

Upvotes: 1

Andy Turner
Andy Turner

Reputation: 140484

They are empty because you are clearing the list immediately after you add it - adding temp to listOfArrays doesn't copy it. You are just adding the same list to listOfArrays lots of times.

You can do this much more easily using List.subList to extract 300-element blocks from list:

for (int i = 0; i < list.size(); i += 300) {
  // Extract a view of this block of (up to) 300 elements.
  List<E> subList = list.subList(i, Math.min(i+300, list.size()));
  // Copy it into a new list since we require an ArrayList to add
  // to `listOfArrays`.
  ArrayList<E> copy = new ArrayList<>(subList);
  // Put it into the result.
  listOfArrays.add(copy);
}

Upvotes: 1

Related Questions