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

1. Member
Join Date
Oct 2009
Posts
27
Rep Power
0

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

Conversion of an arraylist to a two dimensional array which contains integers

2. 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. :)
Last edited by Fubarable; 02-03-2010 at 04:45 AM.

3. Senior Member
Join Date
Oct 2009
Location
California,US
Posts
201
Rep Power
4
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.

4. Member
Join Date
Jan 2010
Posts
26
Rep Power
0
Java 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?

5. Member
Join Date
Jan 2010
Posts
26
Rep Power
0
On a second look that i > secondSize may need to be >=

6. Member
Join Date
Jan 2010
Posts
26
Rep Power
0
may need a intValue() call as well

7. Senior Member
Join Date
Oct 2009
Location
California,US
Posts
201
Rep Power
4
Originally Posted by JavaJuJitZu
Java 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.

8. Member
Join Date
Jan 2010
Posts
26
Rep Power
0
Java Code:
``` /* declare a list and fill it */
ArrayList<Integer> arrlist = new ArrayList<Integer>();

/* 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.
Last edited by JavaJuJitZu; 02-04-2010 at 11:09 AM. Reason: typo

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•