# Conversion of an arraylist to a 2D array of integers

• 02-03-2010, 05:25 AM
yanipao
Conversion of an arraylist to a 2D array of integers
Conversion of an arraylist to a two dimensional array which contains integers
• 02-03-2010, 05:38 AM
Fubarable
Quote:

Originally Posted by yanipao
Conversion of an arraylist to a two dimensional array which contains integers

Wishing you much luck with that.

Note that if you have an actual question, I strongly suggest that you do that, ask a question (I don't see one above), and also provide enough information so that it's answerable (right now as far as details, you don't give much). So again, about all I can do is wish you luck. :)
• 02-03-2010, 07:24 AM
[RaIdEn]
Quote:

Originally Posted by yanipao
Conversion of an arraylist to a two dimensional array which contains integers

its pretty broad and a vague question..

try giving some examples and narrowing the question. Displaying the code is even better.
• 02-03-2010, 07:52 AM
JavaJuJitZu
Code:

```int size = arrlist.size(); secondSize = size /2; int secondSize = size%2 == 0 ? secondSize : secondSize+1; int[][] intArray = new int[2][secondSize]; for(int i=0;i<size;i++){ if(i > secondSize){ intArray[1][i%secondSize] = arrlist.get(i); } else{ intArray[0][i]=arrlist.get(i); } }```
Enjoy, ofcouse the simplest case is to say new int[1][size] but thats a bit cheecky no?
• 02-03-2010, 07:53 AM
JavaJuJitZu
On a second look that i > secondSize may need to be >=
• 02-03-2010, 07:56 AM
JavaJuJitZu
may need a intValue() call as well
• 02-03-2010, 08:28 AM
[RaIdEn]
Quote:

Originally Posted by JavaJuJitZu
Code:

```int size = arrlist.size(); secondSize = size /2; int secondSize = size%2 == 0 ? secondSize : secondSize+1; int[][] intArray = new int[2][secondSize]; for(int i=0;i<size;i++){ if(i > secondSize){ intArray[1][i%secondSize] = arrlist.get(i); } else{ intArray[0][i]=arrlist.get(i); } }```

Will this code compile?. i mean excluding the class tags.
• 02-04-2010, 12:09 PM
JavaJuJitZu
Code:

``` /* declare a list and fill it */         ArrayList<Integer> arrlist = new ArrayList<Integer>();         arrlist.add(3);         arrlist.add(4);         arrlist.add(5);         arrlist.add(10);         arrlist.add(2);         arrlist.add(23);         arrlist.add(1);         /* Original Code with a few fixes */         int size = arrlist.size();         int secondSize = size / 2;         secondSize = size % 2 == 0 ? secondSize : secondSize + 1;         int[][] intArray = new int[2][secondSize];         for (int i = 0; i < size; i++) {             if (i >= secondSize) {                 intArray[1][i % secondSize] = arrlist.get(i);             } else {                 intArray[0][i] = arrlist.get(i);             }         }         /* foreach loop on the multidimensional array */         for(int[] i : intArray){             for(int j : i){                 System.out.println("A Number in the array " + j);             }         }```
Just stick that in a main and see for yourself, had to fix the 2nd and third line of the original, the int is on the wrong line.