to add elements and to retrieve them

i want to add d elements to d arraylist in init method and then retrieve its value in in paint method

how can i do tht?

Label one;

ArrayList a= new ArrayList();

public void init()

{

a.add(20);

one = new Label("hi"]);

add(one);

}

public void paint(Graphics g)

{

one.setLocation(a.get(0),a.get(0)); // also an error comes here

}

[411 byte] By [brittle_bonda] at [2007-11-27 10:48:07]
# 1

> a.add(20);

U cant directly add a primitive datatype to arraylist unless u use jdk 1.5 or above. R u sure u r using jdk1.5 or above?

Since u have not specified the type in the arraylist, it always returns an object.

So a.get(0) will return an object.

> one.setLocation(a.get(0),a.get(0));

Check the parameters of setLocation();

Note : Plz post the exact error message. Also post the code inside tags.

haishaia at 2007-7-28 22:24:32 > top of Java-index,Core,Core APIs...
# 2

here is the modified code(jdk1.5)

Label one;

ArrayList<Integer> a = new ArrayList<Integer>();

public void init() {

a.add(20);

one = new Label("hi");

add(one);

}

public void paint(Graphics g) {

one.setLocation((Integer) a.get(0), (Integer) a.get(0));

}

haishaia at 2007-7-28 22:24:32 > top of Java-index,Core,Core APIs...
# 3

I give in. What's the error you're getting?

georgemca at 2007-7-28 22:24:32 > top of Java-index,Core,Core APIs...
# 4

one.setLocation((Integer) a.get(0), (Integer) a.get(0));

Why is this cast required if you already have declared ArrayList as follows?

ArrayList<Integer> a = new ArrayList<Integer>();

AshwineeJhaa at 2007-7-28 22:24:32 > top of Java-index,Core,Core APIs...
# 5

> Why is this cast required if you already have

> declared ArrayList as follows?

I was thinking the same. I think the cast is superfluous. Not that it does any great harm, just confuses a little.

OleVVa at 2007-7-28 22:24:32 > top of Java-index,Core,Core APIs...