Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How do arrayLists know what to take in?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Raknarg




PostPosted: Wed Mar 19, 2014 6:21 pm   Post subject: How do arrayLists know what to take in?

In an ArrayList, the triangle brackets would tell you what kind of objects to take in, so for example:

ArrayList<Person> is an arraylist of people.

How in the arraylist code does that take effect? Is it something any programmer can take advantage of when writing their own classes?
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Wed Mar 19, 2014 7:06 pm   Post subject: RE:How do arrayLists know what to take in?

This is called a generic. It's kind of like a C++ template. You can create generic classes and interfaces, and probably other things too.
nullptr




PostPosted: Wed Mar 19, 2014 7:19 pm   Post subject: Re: How do arrayLists know what to take in?

Generics (types that require angle brackets) are made possible in Java by something called type erasure. Generics were not originally part of the language -- up until Java 1.5, you simply had an ArrayList (no brackets) which contained Objects. You'd have to cast the elements of the list to the type that you wanted whenever you retrieved an element of the list. You can still do this, though it would generate a warning:

code:

ArrayList arr = new ArrayList();
arr.add("hello world");
String element = (String) arr.get(0);


Not only was this cumbersome, it was also dangerous. If you wrote a function taking in an ArrayList of Strings, and some other programmer mistakenly passed in an ArrayList of Integers, the compiler could not detect the error -- it wouldn't be detected until runtime, when the JVM would try to cast an Integer to a String. The angle brackets thus provide type safety and make the code more elegant (in my opinion). However, "under the hood", things still work the same way as before -- the compiler erases the generic types when it generates the class files. This is why the following code can compile -- because fundamentally, an ArrayList<Integer> and an ArrayList<String> are really the same type:

code:

ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(42);
ArrayList<String> arr2 = (ArrayList<String>) ((ArrayList) arr);
String s = arr2.get(0); // only here will a runtime error be generated


If you want to write your own generic class, simply add angle brackets after the declaration. For example, a node in a linked list might look like this:

code:

class Node<T>
{
    T content;
    Node<T> next;
}


You can also write generic functions. For example, this is a function from a project I'm working on which selects an element randomly from a collection:

code:

public static <T> T randomFrom(Iterable<T> it)
{
        int count = 0;
        T crnt = null;
        for (T x : it)
        {
                count++;
                if (Math.random() * count < 1)
                        crnt = x;
        }
        return crnt;
}
Raknarg




PostPosted: Wed Mar 19, 2014 8:04 pm   Post subject: RE:How do arrayLists know what to take in?

So is T basically just a variable to hold the type of class that is being used?
DemonWasp




PostPosted: Wed Mar 19, 2014 8:24 pm   Post subject: RE:How do arrayLists know what to take in?

It's more like a symbol that you use in place of "whatever class the user specifies". It's not actually a variable: you cannot vary it.

You can also put bounds on what type T is, such as "T must be a List". For more details, see: http://docs.oracle.com/javase/tutorial/java/generics/index.html
Raknarg




PostPosted: Wed Mar 19, 2014 9:05 pm   Post subject: RE:How do arrayLists know what to take in?

This is wonderful, thank you. Just created my own ArrayList class for fun
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: