Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting List<List<Integer>> to int[][]

I am writing a method which converts Integer data type from a List of Lists, to a primitive array of arrays, type int[][].

Question

Is there any way to convert Integer to int using Java API?

For your information, int[] set will be used for other purposes so please ignore.

What I have found

Apache Commons API "toPrimitive" method converts Integer to int type. However, I'm only looking for solutions using native java API.

This is my code so far

class Test {
    int[][] convert(int[] set) {
        List<List<Integer>> ll = new ArrayList<List<Integer>>();
        ll.add(new ArrayList<Integer>());
        ll.add(new ArrayList<Integer>());
        ll.add(new ArrayList<Integer>());
        ll.get(0).add(1);
        ll.get(0).add(2);
        ll.get(1).add(2);
        ll.get(2).add(3);

        System.out.println(ll + " " + ll.size());

        int[][] tempArray = new int[0][0];
        for (int i = 0; i < ll.size(); i++) {
            for (int j = 0; j < ll.get(i).size(); j++) {
                tempArray[i][j] = ll.get(j);
            }
        }
        return tempArray;
    }
}

Expected results

List entry: [[1,2],[2],[3]]
return: {{1,2},{2},{3}}

Errors

tempArray[i][j] = ll.get(j);

returns java.util.List<java.lang.Integer> cannot be converted to int.

like image 664
Zero Avatar asked Feb 28 '26 23:02

Zero


2 Answers

To answer your exact question, yes an Integer can be converted to an int using the intValue() method, or you can use auto-boxing to convert to an int.

So the innermost part of your loop could be either of these:

tempArray[i][j] = ll.get(i).get(j).intValue();
tempArray[i][j] = ll.get(i).get(j);

However, we can also take a different strategy.

As a modification of this answer to a similar question, in Java 8 you can use Streams to map to an integer array. This structure just requires an extra layer of mapping.

List<List<Integer>> list = new ArrayList<>();

int[][] arr = list.stream()
    .map(l -> l.stream().mapToInt(Integer::intValue).toArray())
    .toArray(int[][]::new);

Ideone Demo

like image 129
4castle Avatar answered Mar 03 '26 12:03

4castle


This

tempArray[i][j] = ll.get(j);

should be something like

tempArray[i][j] = ll.get(i).get(j);

However, you have a few other bugs (you need to declare the arrays); you can also shorten the initialization routines. Something like,

static int[][] convert(int[] set) {
    List<List<Integer>> ll = new ArrayList<>();
    ll.add(Arrays.asList(1,2));
    ll.add(Arrays.asList(2));
    ll.add(Arrays.asList(2,3));

    System.out.println(ll + " " + ll.size());

    int[][] tempArray = new int[ll.size()][];
    for (int i = 0; i < ll.size(); i++) {
        tempArray[i] = new int[ll.get(i).size()];
        for (int j = 0; j < tempArray[i].length; j++) {
            tempArray[i][j] = ll.get(i).get(j);
        }
    }
    return tempArray;
}
like image 42
Elliott Frisch Avatar answered Mar 03 '26 11:03

Elliott Frisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!