Is it possible to do this in Java? Maybe I'm using the wrong syntax?
ArrayList<Integer> iAL = new ArrayList<Integer>();
iAL.addAll(Arrays.asList(new Integer[] {1, 2, 3, 4, 5 }));
for (int i = 0; i < iAL.size(); ++i) {
System.out.println(iAL[i]); //<-------- HERE IS THE PROBLEM
}
Also, is it possible to do something like
iAL.addAll( new int[] {1, 2, 3, 4, 5} );
as is seen on c#?
Thanks
Try System.out.println(iAL.get(i));
. Because it's a List
, not array
ArrayList<Integer> iAL = new ArrayList<Integer>();
iAL.addAll(Arrays.asList(new Integer[] {1, 2, 3, 4, 5 }));
for (int i = 0; i < iAL.size(); ++i) {
System.out.println(iAL.get(i));
}
AFAIK
iAL.addAll(Arrays.asList(new Integer[] {1, 2, 3, 4, 5 }));
// this is the shortest solution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With