2

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Bob
  • 1,088
  • 1
  • 9
  • 15
  • http://docs.oracle.com/javase/tutorial/extra/generics/index.html – BalusC Mar 05 '12 at 18:43
  • 2
    It 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 Answers6

5

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.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
2

It means the ArrayList can contain any non-primitive type.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

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.

aidanok
  • 859
  • 1
  • 7
  • 11
0

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

Eng.Fouad
  • 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`, 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. – Naftuli Kay Mar 05 '12 at 19:13
0

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":

'Hope that helps .. pSM

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

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.

Shawn Shroyer
  • 901
  • 1
  • 8
  • 18