Subject: Java Lists Question
Are you refering to class java.awt.List or interface java.util.List? I assume you are referting to the List interface.
There are many types of ‘lists’ in Java. Read up on the collections framework for more information.
Sun has some great resources to help you learn Java, check out:
And bookmark the Java API, you will need it for reference:
http://java.sun.com/j2se/1.4.2/docs/api/index.html
You need to pick an implementation of the List interface (or write your own) before you use it. The existing implementations are: AbstractList, ArrayList, LinkedList, Vector
If you check the Java API for List, you will see the definiton of the add method(among other things):
void add(int index, Object element)
- Inserts the specified element at the specified position in this list (optional operation).
boolean add(Object o)
- Appends the specified element to the end of this list (optional operation).
So you see, that you have to cast your ‘variable name’ to an Object f.x. for an primitive of type int i - you would do something like: myList.add((Object)new Integer(i))
you create a new integer object with the value of i and then cast it to type Object. You can cast it back to type Integer when you retrive it.
Here is a working example:
import java.util.ArrayList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
List myList = new ArrayList();
for(int i = 0; i < 10;i++) {
myList.add((Object)new Integer(i));
}
}
}
For gods sake, don’t write your Java code in Notes - download Eclipse and use that.
Cheers,
VH