views:

139

answers:

6

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

+9  A: 

Try System.out.println(iAL.get(i));. Because it's a List, not array

splix
So Lists in Java can't use indexers? Probably it's related to Java not supporting Operator Overloading?
devoured elysium
Yes, you are right, there are no indexers and no other stuff like this :(
splix
Java does not support Operator Overloading so it is not possible to use the indexing operator ([]) for anything other than arrays.
Thirler
+4  A: 
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

Anantha Kumaran
Arrays.asList() accepts a varargs parameter, so it can be shorter.
Carl
Now THAT is interesting.
devoured elysium
Also, if you're just going to be immediately adding those elements, using the copy constructor can further shorten things.
Carl
@Carl thanks for the hint.
Anantha Kumaran
+3  A: 

In addition, I would add two other notes about your code:

For example, combining several helpful suggestions from other answers

List<Integer> list = new ArrayList<Integer>();
list.addAll(Arrays.asList(1, 2, 3, 4, 5 ));

for (Integer i : list) {
    System.out.println(i);
}

Addendum: The question of coding to the interface is interesting, and Carl's comment is particularly apropos. Using the interface type, List, minimizes your obligation to use a specific implementation. If the need arises, you can later switch to any class that implements List.

trashgod
I don't get your second point(coding to the interface). What does it have to do with this?
devoured elysium
declaring iAL as a List instead of an ArrayList. unless you plan to use some of the size management methods in ArrayList, that's probably a good idea.
Carl
+4  A: 

Also, is it possible to do something like

iAL.addAll( new int[] {1, 2, 3, 4, 5} );

Close enough:

iAL.addAll(Arrays.asList(1, 2, 3, 4, 5));

No need for the new Integer[] in your code.

Konrad Rudolph
+3  A: 

No, you must use .get(i); [i] is for arrays only. However, if you don't need the index variable for something else, the for-each syntax is preferable (as per trashgod's answer).

For the second, if you aren't resizing the List (it is still fine to mutate individual elements), it would be perfectly reasonable to do the following:

List<Integer> iAL = Arrays.asList(1, 2, 3, 4, 5);

Note, that Arrays.asList() accepts a varargs parameter, so no reason to explicitly construct the array.

If you want a resizable List, the following is probably the shortest:

List<Integer> iAL = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5));
Carl
+2  A: 

I will refactor your entire code to :

List<Integer> iAL = Arrays.asList(1, 2, 3, 4, 5 );

for (int i : iAL) {
  System.out.println(i);
}
missingfaktor