Adam
Adam

Reputation: 2570

Specify the type for ArrayList

In Java, and Android, I end up using ArrayList<String> for the supplying list as I find them easier to use than the standard String[]. My real questions though are this:

What is the <String> portion of the ArrayList<String> called?
How can I create classes and use the <> [modifier]? (I don't know what it's actually called, so for now it's modifier).

Thanks!

Upvotes: 12

Views: 48840

Answers (6)

j.jerrod.taylor
j.jerrod.taylor

Reputation: 1140

The is the type parameter. In Java, you have to provide one of these when the class is written as a generic.

Here is an example of a Generic class definition

private class GNode<T>
{
    private T data;
    private GNode<T> next;

    public GNode(T data)
    {
        this.data = data;
        this.next = null;
    }
}

You can now create nodes of any type that you pass in. The T acts as a generic type parameter for your class definition. If you want to create a node of Integers, just do:

 GNode<Integer> myNode = new GNode<Integer>();

It should be noted that your type parameter must be an object. This works through Java's auto-boxing and auto-unboxing. This means that you cannot use java primitive types and you must use the corresponding classes instead.

Boolean instead of bool
Integer instead of int
Double instead of double 
etc...

Also, if you don't pass in a type parameter I'm pretty sure your code will still compile. But it won't work.

Upvotes: 1

joan
joan

Reputation: 2551

Supose you want an ArrayList to be filled only with Strings. If you write:

ArrayList<String> list = new ArrayList<String>();
    list.add("A");
    list.add("B");
    list.add("C");

You can be sure than if somebody tries to fill the arrayList with an int it will be detected in complile time.

So, generics are used in situations where you want enforce restrictions like this.

Upvotes: 3

javadev
javadev

Reputation: 1669

Here, you wil maybe see clearer:

ArrayList<TypeOfYourClass>

You can create a Person class and pass it to an ArrayList as this snippet is showing:

ArrayList<Person> listOfPersons = new ArrayList<Person>();

Upvotes: 8

Mike Thomsen
Mike Thomsen

Reputation: 37524

Look up the generics syntax for Java. That will set you straight (well, sort of; a lot of people find Java's approach inferior to C++ and C#).

Upvotes: 1

OrangeDog
OrangeDog

Reputation: 38835

The bit between <> is a type argument, and the feature is called Generics

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1504182

The <String> part is the type argument. It provides a "value" of sorts for the type parameter which is the E in ArrayList<E>... in the same way that if you have a method:

public void foo(int x)

and you call it with:

foo(5)

the parameter is x, and the argument supplied is 5. Type parameters and type arguments are the generic equivalent, basically.

See section 4.5 of the JLS (and the links from it) for some more details - and the Java Generics FAQ for more information about generics than you could possibly want to read :)

Upvotes: 5

Related Questions