In an ArrayList for android such as the simplelistadapter I commonly see ArrayList<?>
and in some tutorials I've reviewed the <?>
is replaced by some value. But I'm still not quite sure what variable or qualifier this stands for.
-
http://docs.oracle.com/javase/tutorial/extra/generics/index.html – BalusC Mar 05 '12 at 18:43
-
2It simply means that the ArrayList is of the type ? (ie: the type that the ArrayList describes or wraps is of type ?) which means that it can be any non-primitive type, as mentioned below. If you had an `ArrayList>`, it could contain a `Long`, a `String`, and even a `Map`. An `ArrayList
`, however, could only contain `Integer` values. – Naftuli Kay Mar 05 '12 at 18:48 -
http://stackoverflow.com/questions/6938488/what-is-the-meaning-of-the-token-in-java/6938523#6938523 – omnomnom Mar 05 '12 at 20:06
-
possible duplicate of [What does List> mean in java generics?](http://stackoverflow.com/questions/1844770/what-does-list-mean-in-java-generics) – Ken Redler Mar 06 '12 at 05:16
6 Answers
It's called Generics.
For example, you can do this:
ArrayList<Integer> list = new ArrayList();
list.add(new Integer(1));
list.add(new Integer(2));
.....
Integer i = (Integer)list.get(1);
Or you can do:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(new Integer(1));
list.add(new Integer(2));
.....
Integer i = list.get(1);
As you can see, no need for casting.

- 155,636
- 47
- 315
- 299
-
-
-
I see, does that make the ArrayList 2D? And seeing that String, Integers are Objects. I assuming, ArrayList
works too right? :) – Melvin Lai May 08 '12 at 09:33 -
Yeah, I guess that you can look at it as a 2D array kind of thing. You can go to 3D, 4D, etc.. if you want/need to. And yes, you can put *Object* there or any other type (not primitives). – Nitzan Tomer May 08 '12 at 09:55
The is a wildcard character for a generic type. Normally you declare an array list like:
ArrayList<String>
Where the type is specified exactly. The list will contain Strings. But sometimes you want to make a method or a class that takes an ArrayList of any type, or you want a field that points an ArrayList of any type
public void removeFirstItem(ArrayList<?> target) {
...
}
Now this method can take an ArrayList<String>
, or an ArrayList<Long>
, etc, and do some operation on it.
Similiary you can have a local variable:
ArrayList<?> someList;
someList = new ArrayList<String>();
someList = new ArrayList<Long>);
This works, whereas:
ArrayList<String> someList = new ArrayList<String>();
someList = new ArrayList<Long>();
Will not, since someList is specified as an ArrayList<String>
, so only ArrayList<String>
can be assigned to it.

- 859
- 1
- 7
- 11
Similar to ArrayList<? extends Object>
, which means it can contain any object inherits from Object
class (i.e. All objects in Java).

- 115,165
- 71
- 313
- 417
-
Hate to nitpick, but all non-primitive objects in Java would be more technically correct. – Naftuli Kay Mar 05 '12 at 18:49
-
@TKKocheran but, what do you mean by "non-premitive" objects? Is there primitive objects in Java? – Eng.Fouad Mar 05 '12 at 18:52
-
You can't make an `ArrayList
`, you can only make an `ArrayList – Naftuli Kay Mar 05 '12 at 19:13`, because an `int` is a primitive type and can't be null, but an `Integer` is a real Java type and can, in fact, be null.
The syntax is a "wildcard". It's needed with Java generics:
A wildcard can be "bounded" or "unbounded". The whole issue of compile/design time generic typing is closely associated with the runtime JVM issue of "type erasure":
http://docs.oracle.com/javase/tutorial/java/generics/wildcards.html
http://docs.oracle.com/javase/tutorial/java/generics/erasure.html
'Hope that helps .. pSM
This is a question regarding generics. The <(Kind of Object)> syntax represents that only a certain kind of object (class instance) may be passed as an argument. When you have the syntax, you are saying that any kind of class instance may be passed.

- 901
- 1
- 8
- 18
-
How about an Object class? The class contains 4 Strings in it. Can it still be used in an ArrayList? – Melvin Lai May 07 '12 at 05:07