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]);
}
}
> 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
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
can u plzz give me an example tht can i use in my prog
> can u plzz give me an example tht can i use in my prog
ArrayList:
http://mindprod.com/jgloss/arraylist.html
http://java.sun.com/docs/books/tutorial/collections/implementations/list.html
http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html
am still not able to implement in my prog.
can u plzz help me in tht
> 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!
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);
}
}
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
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
thanx alot its working now
thanx alot its working now
Don't forget the Dukes!
;)
but whats the advantage of dukes?
> but whats the advantage of dukes?
They make you popular and attractive....
and handsome! All the girls want to go out with guys with Teh Dukez
:)