• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is this?  RSS feed

 
catherine matthews
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the use of this?? Is this a dynamic array declaration?? how can we use it??

ArrayList list= new ArrayList();
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reading the api docs will tell you 'what'
http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html

searching the forums/google for
"new ArrayList"
will give you plenty of examples on 'how'
 
catherine matthews
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks micheal...

now let say i have initialized ......
ArrayList abc = new ArrayList();

then i need to add string elements into the array
so i just put like
abc.add()
then if i want to get the elements from other methods how do i call them?
abc???

please help
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from the api docs these are the methods you need
abc.add(..)
abc.get(..)

note:
ArrayList abc = new ArrayList();

it is much better this way
List abc = new ArrayList();

and if using any awt components in your program, you will need it as
java.util.List abc = new ArrayList();

you probably won't understand why, at this stage, but a good habit to get into
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you probably won't understand why, at this stage, but a good habit to get into

An habit you don't understand is not a good habit
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Catherine, it's probably a good idea for you to learn the basics of Java very well first by for example looking at the Learning the Java Language and Essential Java Classes tutorials.

After that, have a look at the Collections tutorial.
 
perfect kimei
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,

i have tried to make a simple program on how you can use the array list...but is so late and i though like i shuld give my contribution..

the ArrayList creates an array of list which a programmer can use..to use the arraylist you have to import from the java classes..

you create an array list by: ArrayLIst myList = new ArrayList () ;

then you have to create the objects which will be referencing to the your ArrayList myList.

You add the object by method : myList.add(object_name) ; like in my sample program: myList.add(name);

THE USE OF THE ARRAYLIST is when you want to create many objects to a single array. This is like saying...you have an array of students and you want their NAMES, AGE. Then you can create an array list of students and call the objects name and age which will be referencing to the arraylist

import java.util.ArrayList;

public class ArrayLists
{
public static void main(String[] args)
{
ArrayList myList = new ArrayList ();
String name ;
int listLength ;
System.out.println("Welcome");

listLength = Keyboard.readInt("Enter length of the array : ");

for (int i = 0 ; i < listLength ; i++ )
{
name = Keyboard.readString (" Enter your name : ");
myList.add( name);
}
for (int i = 0 ; i < myList.size() ; i++)
{
System.out.println(myList);
}
}
}

Ooooppppsssssssss!!!

i hope i did not confuse you.....

....i am a new babe here and also in programming...

i think this is a nice link...just try to see how it can help you

http://leepoint.net/notes-java/data/collections_non_generic/20lists/20arraylist.html
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Dunn:
from the api docs these are the methods you need
abc.add(..)
abc.get(..)

note:
ArrayList abc = new ArrayList();

it is much better this way
List abc = new ArrayList();

and if using any awt components in your program, you will need it as
java.util.List abc = new ArrayList();

you probably won't understand why, at this stage, but a good habit to get into


That's quite the generalization. I use AWT and Swing on a daily basis and have never needed to fully qualify my java.util.List just because I imported an AWT or Swing class. The only way you would have this problem any time you use any AWT component is if you imported java.awt.*, in which case I would say that habit needs to be ejected.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!