Conversion of an arraylist to a two dimensional array which contains integers
Printable View
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. :)
Enjoy, ofcouse the simplest case is to say new int[1][size] but thats a bit cheecky no?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);
}
}
On a second look that i > secondSize may need to be >=
may need a intValue() call as well
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.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);
}
}