0

I have :

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

which I want to convert to `

int[][] adjL = new int[?][?];

Is that possible? And what should I replace ? in the array sizes with?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Thammarith
  • 662
  • 5
  • 19

2 Answers2

3

Not simple but this should help you :

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

Integer[][] result = new Integer[adjList.size()][];//create your array of arrays
for (int i = 0; i < adjList.size(); i++) {
    //foreach array you have to inisialize it with the correct size
    result[i] = new Integer[adjList.get(i).size()];

    //the fill the array with the value of your list
    for(int j = 0; j < adjList.get(i).size(); j++){
        result[i][j] = adjList.get(i).get(j);
    }
}

for inputs like this :

adjList.add(new ArrayList<>(Arrays.asList(1, 2, 3, 4)));
adjList.add(new ArrayList<>(Arrays.asList(1, 2, 3)));
...    
System.out.println(Arrays.deepToString(result));

The output :

[[1, 2, 3, 4], [1, 2, 3]]
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 1
    Thank you so much. I see I missed this step `result[i] = new Integer[adjList.get(i).size()];` and I didn't get the correct size. – Thammarith Dec 16 '17 at 16:46
2

The question was basically answered, although the proposed solution returns an Integer[][] and not the int[][] that was asked for in the question - but that may be a detail.

However, the task of converting a "collection of collections of numbers" into a 2D primitive array is a very generic one, and thus, it can be written very generically.

So here is another solution which uses streams and can be applied to arbitrary collections of arbitrary numbers. It may not necessarily be "better" than another solution, but maybe as a reference for other application cases.

It shows the case of converting an ArrayList<ArrayList<Integer>> into an int[][] array, but also contains a method showingThatThisIsFarMoreGeneric.

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Vector;

public class ToIntArray2D
{
    public static void main(String[] args)
    {
        ArrayList<ArrayList<Integer>> adjList = createTestLists();
        int[][] result = toIntArray2D(adjList);
        System.out.println(Arrays.deepToString(result));
    }

    private static int[][] toIntArray2D(
        Collection<? extends Collection<? extends Number>> collections)
    {
        return collections.stream()
            .map(c -> toIntArray(c))
            .<int[]> toArray(int[][]::new);
    }

    private static int[] toIntArray(
        Collection<? extends Number> collection)
    {
        return collection.stream().mapToInt(Number::intValue).toArray();
    }

    private static void showingThatThisIsFarMoreGeneric()
    {
        ArrayList<ArrayList<Integer>> exampleA = null;
        List<List<Integer>> exampleB = null;
        Set<Vector<Float>> exampleC = null;
        LinkedList<HashSet<? extends BigInteger>> exampleD = null;
        LinkedList<? extends HashSet<? extends BigDecimal>> exampleE = null;

        toIntArray2D(exampleA); // Works
        toIntArray2D(exampleB); // Works
        toIntArray2D(exampleC); // Works
        toIntArray2D(exampleD); // Works
        toIntArray2D(exampleE); // Works
    }

    private static ArrayList<ArrayList<Integer>> createTestLists()
    {
        ArrayList<ArrayList<Integer>> adjList =
            new ArrayList<ArrayList<Integer>>();

        ArrayList<Integer> adjList0 = new ArrayList<Integer>();
        adjList0.add(0);
        adjList0.add(1);
        adjList0.add(2);
        adjList.add(adjList0);

        ArrayList<Integer> adjList1 = new ArrayList<Integer>();
        adjList1.add(3);
        adjList1.add(4);
        adjList.add(adjList1);

        ArrayList<Integer> adjList2 = new ArrayList<Integer>();
        adjList2.add(5);
        adjList2.add(6);
        adjList2.add(7);
        adjList2.add(8);
        adjList.add(adjList2);
        return adjList;
    }

}

A side note: Considering the fact that you should usually program to an interface, a declaration like

ArrayList<ArrayList<Integer>> adjList;

should hardly ever appear in real code anyhow. It should rather be a

List<List<Integer>> adjList;
Marco13
  • 53,703
  • 9
  • 80
  • 159