2

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.

Zero
  • 31
  • 1
  • 3

5 Answers5

9

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

Community
  • 1
  • 1
4castle
  • 32,613
  • 11
  • 69
  • 106
3

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;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • This has a similar issue to [this answer](http://stackoverflow.com/a/718558/5743988) in that it doesn't work so well when the `List` is a `LinkedList` for example. An `Iterator` is a better choice. – 4castle Aug 19 '16 at 03:24
0

Java 7. Converting List<List<Integer>> to int[][]:

// original list
List<List<Integer>> list = Arrays.asList(
        Arrays.asList(1, 2),
        Arrays.asList(2),
        Arrays.asList(3));
// initialize the array,
// specify only the number of rows
int[][] arr = new int[list.size()][];
// iterate through the array rows
for (int i = 0; i < arr.length; i++) {
    // initialize the row of the array,
    // specify the number of elements
    arr[i] = new int[list.get(i).size()];
    // iterate through the elements of the row
    for (int j = 0; j < arr[i].length; j++) {
        // populate the array
        arr[i][j] = list.get(i).get(j);
    }
}
// output
System.out.println(Arrays.deepToString(arr));
// [[1, 2], [2], [3]]
0

Please read the comments in code for better understanding!

public void someFn() {
    List<List<Integer>> outerList = new ArrayList<>();

    /* Outer list index is array row index[i] and inner list index is 
       array column index[j].  This is how we are going to parse and 
       add the data into our array */

    List<Integer> innerList = new ArrayList<>();
    innerList.add(1);  // ---> Index (0, 0) After adding inner list to outer list
    innerList.add(2);  // ---> Index (0, 1)
    innerList.add(3);  // ---> Index (0, 2)
    innerList.add(4);  // ---> Index (0, 3)

    List<Integer> innerList1 = new ArrayList<>();
    innerList1.add(5);  // ---> Index (1, 0)
    innerList1.add(6);  // ---> Index (1, 1)
    innerList1.add(7);  // ---> Index (1, 2)
    innerList1.add(8);  // ---> Index (1, 3)

    outerList.add(innerList);
    outerList.add(innerList1);

    parseArrayFromList(outerList);
}

/* Outer list gives the number of rows size and the inner list 
   gives the number of columns size */
private static void parseArrayFromList(List<List<Integer>> list) {
    int arr[][] = new int[list.size()][list.get(0).size()];
    
    for (int i = 0; i < list.size(); i++) {
        for (int j = 0; j < list.get(i).size(); j++) {
            arr[i][j] = list.get(i).get(j);
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kanagalingam
  • 2,096
  • 5
  • 23
  • 40
-1

I think you don't need API to convert Integer to int. Just because Integer itself has its own method. Let's see following example

Integer integerObj = new Integer(100);
int intValue = integerObj.intValue();     // intValue = 100
Minh
  • 424
  • 3
  • 12
  • The compiler does auto-boxing, so `int intValue = integerObj;` works too. It can't unbox a whole `List` at once though. – 4castle Aug 19 '16 at 03:01
  • Since java 5, it auto-boxing, but i think rely on something like that is bad practice. – Minh Aug 19 '16 at 03:10
  • It's a feature of the language. I've never heard of it being a bad practice, just a style choice. – 4castle Aug 19 '16 at 03:13
  • Well, it make good habit when you come to less-convenience language. – Minh Aug 19 '16 at 03:16
  • I agree. Anyway, you need to describe how this answers the question. This just shows how to unbox a single `Integer`. The question is about a `List>` – 4castle Aug 19 '16 at 03:21
  • No, i think the question is: Is there any way to convert Integer to int using Java API? – Minh Aug 19 '16 at 03:22
  • Ah, I see that now. Title was misleading – 4castle Aug 19 '16 at 03:32