add new elements to array

i hv an array of int type.. and i want to add more elements to it

how can i do tht?

also i want to get those elements in another method to set the location of my label

initially there are no elements in it

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="Demo" width=250 height=150>

</applet>

*/

public class Demo extends Applet{

int a[] = new int[10];

Label l;

public void init()

{

l= new Label("hi");

add(l);

for (int i=1i<3;i++)

{

a[i-1]=i*50;

}

}

public void paint(Graphics g

{

l.setLocation(a[1],a[2]);

}

}

[723 byte] By [brittle_bonda] at [2007-11-27 10:46:58]
# 1

> and i want to add more elements to it how can i do tht

An array has a static size, it cannot grow. You have two basic options:

1) create a new array and copy the old one to it, then replace the reference to your old array with the new one

2) use a Collection class, like a List

gimbal2a at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 2

Whenever you need an array which may shrink or grow in size, you may use vector or arraylist instead of array.

There are various utilities available in java, try to use those according to your need.

Thanks

JL.Nayaka at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 3

can u plzz give me an example tht can i use in my prog

brittle_bonda at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 4

> can u plzz give me an example tht can i use in my prog

ArrayList:

http://mindprod.com/jgloss/arraylist.html

Yannixa at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 5

http://java.sun.com/docs/books/tutorial/collections/implementations/list.html

http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html

java_knighta at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 6

am still not able to implement in my prog.

can u plzz help me in tht

brittle_bonda at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 7

> am still not able to implement in my prog.

> can u plzz help me in tht

show updated codes plz, y not able to implement in ur prog? plz xplain so hlp cumming soon!

OnBringera at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 8

here is my code in which iam trying to implement it

in here i want to set the location of the label on d applet screen and i can only get its coordinate in the init method

so can u now help me in this

import java.awt.*;

import java.awt.event.*;

import java.lang.*;

import java.applet.*;

import java.util.ArrayList;

/*

<applet code="Demo" width=250 height=150>

</applet>

*/

public class Demo extends Applet implements ActionListener{

Label llist[] = new Label[2];

Button blist[] = new Button[2];

static String xx[] = {"one","one1"};

static String yy[] = {"two","two1"};

ArrayList a = new ArrayList();

Button two;

Label one;

public void init()

{

for(int i=0;i<2;i++)

{

one = new Label(xx);

llist=(Label) add(one);

two= new Button(yy);

two.setFont(new Font("SansSerif", Font.BOLD, 18));

blist = (Button) add(two);

two.addActionListener(this);

}

a.add(20);

}

public void actionPerformed(ActionEvent ae)

{

String str = ae.getActionCommand();

if(str.equals("two"))

{

msg = "You pressed two.";

}

repaint();

}

public void paint(Graphics g)

{

llist[0].setLocation(a.get(0),a.get(0));

g.drawString(msg,200,200);

}

}

brittle_bonda at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 9

First, try to write in proper english (it's not hard to write you instead of u, is it?)

I think your problem lies in this line,

llist[0].setLocation(a.get(0),a.get(0));

where

a is the ArrayList filled with ints .

So, you're passing 2 object references to setLocation but seLocation accepts ints. Then you must cast the elements in the arraylist back to ints. Something like this:

llist[0].setLocation(((Integer)a.get(0)).intValue(),((Integer)a.get(0)).intValue());

does it work now?

Manuel Leiria

manuel.leiriaa at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 10

your code got errors how can we compile it

one = new Label(xx); // xx is an array of string

llist=(Label) add(one); // what are you trying to do?

two= new Button(yy); // yy is an array of string

msg = "You pressed two."; // can't find msg

llist[0].setLocation(a.get(0),a.get(0)); // a.get returns object not int

Yannixa at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 11

thanx alot its working now

brittle_bonda at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 12

thanx alot its working now

brittle_bonda at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 13

Don't forget the Dukes!

;)

manuel.leiriaa at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 14

but whats the advantage of dukes?

brittle_bonda at 2007-7-28 20:21:57 > top of Java-index,Java Essentials,Java Programming...
# 15

> but whats the advantage of dukes?

They make you popular and attractive....

ita6cgra at 2007-7-28 20:22:08 > top of Java-index,Java Essentials,Java Programming...
# 16

and handsome! All the girls want to go out with guys with Teh Dukez

:)

manuel.leiriaa at 2007-7-28 20:22:08 > top of Java-index,Java Essentials,Java Programming...