4

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.

4

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Can an ArrayList store an array? ArrayList<Array> ?
Yeah, it can "store" any type, even ArrayList<ArrayList<String>> works.
I see, does that make the ArrayList 2D? And seeing that String, Integers are Objects. I assuming, ArrayList<Objects> works too right? :)
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).
2

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

Comments

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.

Comments

0

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

3 Comments

Hate to nitpick, but <em>all non-primitive objects in Java</em> would be more technically correct.
@TKKocheran but, what do you mean by "non-premitive" objects? Is there primitive objects in Java?
You can't make an ArrayList<int>, you can only make an ArrayList<Integer>, 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.
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

Comments

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.

1 Comment

How about an Object class? The class contains 4 Strings in it. Can it still be used in an ArrayList?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.