Java Lists Question

Im just working through the Using Java in R5 course and creating lists in Java. Ive tried many permutaions but cannot seem to find a way to build dynamic lists? Ie a list that you dont hard code the list entries into using the List.Add(“List Entry”); method ? Instead I wanted to use List.Add(variablename);

Anybody help ? Im new to Java so may be overlooking something ?

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

Subject: RE: Java Lists Question

Can’t let that pass since it’s advice to a noob.

You DO NOT need to cast up EVER.

Given the method:

boolean add(Object o)

since all other classes are subclasses of Object you can simply pass any object to the method.

You still need to wrap any primative with an appropriate wrapper object, but you do not then need to cast it up to Object.

so you would do:

myList.add(new Integer(1));

On the other hand you always need to Cast down. Since the get method of list returns an Object you need to cast this down to what ever you put in there in the first place.

Here is a better 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(new Integer(i));

}

Integer intObj;

for(int i = 0; i < 10;i++) {

intObj=(Integer)myList.get(i);

System.out.println(intObject.toString())

}

}

}

Subject: Java Lists Question

In the Java for R5 course its the java.awt.List we’re talking about. If you look a little later in the course material it shows you how to do a view lookup, creating a Vector of string objects. You then loop through the Vector and add the values to the List. The Vector can be created in a number of other ways too.

Subject: RE: Java Lists Question

Thanks. Im not stuck on writing a stand alone Java app. Cant import the Package lotus.domino. Trawled the forums but cant find ny answers.

Changed the CLASS and CLASSPATH variables but no joy

Subject: RE: Java Lists Question

Did you remember to add notes.jar to your project?

Subject: RE: Java Lists Question

How do you mean ? I have import lotus.domino.*; as the top line of the class file ?

the classpath is as folows:

.; C:\j2sdk1.4.2_08\lib\classes.zip; C:\Program Files\lotus\notes\notes.jar; C:\Program Files\lotus\notes\data\domino\java\ncso.jar;

Im trying to teach myself Java from the R5 training course book but have nobody to ask. Very much appreciate your help

Mark

Subject: RE: Java Lists Question

It’s probably a better idea to teach yourself Java basics, using the material found at Sun.It is also a good idea to work with a good IDE (f.x. Eclipse).

Using Java with Notes should come after the basics.

Just my $0.2

Subject: RE: Java Lists Question

Are you using the Eclipse IDE? If not, I strongly suggest you get it, it’s free (www.eclipse.org).

If (when) you are using Eclipse, do the following:

Right click on your project

  • Select Properties (at the bottom)

Select “java build path” from the left hand pane

Click on libraries tab

Click button “Add external JARs…”

Select notes.jar

You can also try to copy notes.jar to your project folder.

Subject: RE: Java Lists Question

Thanks a lot.

Im downloading Eclips now and Ill give it a go.

Thanks Again

Mark

Subject: RE: Java Lists Question

Good idea. The install is very easy, just unzip it to a folder and click on eclipse.exe - no registry entries, no re-starts.

Only possible problem you could have, is if it does not find the virtual machine. If so, then specify the path in the desktop shorcut, I think the parameter is -JVM (google for it, if you have problems).

Here is a good online Java eBook (free):

Thinking in Java, 2nd Edition

http://www.janiry.com/bruce-eckel/

Here you will find example code:

http://www.javaalmanac.com/

Here’s Sun Java developer forum (like LDD):

http://forum.java.sun.com/index.jspa

And the Sun Java Tutorial:

Good luck

VH

Subject: RE: Java Lists Question

You will have a hard time trying to teach yourself from the R5 Java Courseware. It is full of typos as well as other errors. Only an experienced Lotus Instructor, or a Sun Certified programmer will be able to navigate you through the courseware. I suggest you use some of the other resources mentioned in this thread, and learn Java first. The course material will make more sense and maybe you can have a bit of a laugh as well.