Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can anybody tell me how to use make use of ArrayLists in my program

 
Neeraja Murali
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the headfirst java book to learn Java and although they mention about ArrayLists and their usage, when i try to compile a program using arraylists the way they have asked us to, it does not. Can anybody help me in this please. Below is the code i tried to compile.

import java.util.*;

class a{

int x;

public void setx(int y){
x=y;
}

public int getx(){
return x;}

}

class tester{

public static void main(String a[]){

a a1=new a();
a1.setx(12);

a a2 = new a();
a2.setx(10);

a a3 = new a();
a3.setx(30);

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

alist.add(a1);
alist.add(a2);
alist.add(a3);

}

}
[ July 19, 2006: Message edited by: Neeraja Murali ]
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seeing your code would be nice, but remember you have to import the arraylist class..

and there are predefined methods like

.Size(); and .add();

and i think that initially, the arraylist can only store String variables..

import java.util.ArrayList;// could be wrong..

<int>ArrayList list = new (int)ArrayList();

list.add(1);

etc...

plz, someone correct me if im wrong...

Justin
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Neeraja Murali:
I am using the headfirst java book to learn Java and although they mention about ArrayLists and their usage, when i try to compile a program using arraylists the way they have asked us to, it does not. Can anybody help me in this please

It would help us to answer your question if you post the specific errors you're getting. But my guess is that your code does not include a proper import statement. ArrayList is in the java.util package. Unlike java.lang, you need to explicitly import this to use in your program. So at the top of your code, be sure you have one of the following...

import java.util.*; //imports everything in the java.util package
import java.util.ArrayList; //imports the ArrayList class
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Fox:
...i think that initially, the arraylist can only store String variables...

No, a List can store any type of Object. In fact, when a reference is added to a List, its type is implicitly upcast to Object.
 
Adam Nace
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can probably help you, as can many here, if you want to provide a code sample that you have that is not working correctly.

Without that sample, I can only make suggestions based on what I think you might be doing wrong.


Basically, when you want to replace an array with an array list, there are only a couple of issues to worry about:

  • The initialization: An array list does not HAVE to be initialized with a size, but it is often more efficient to do so, if you know what size it will be. Without a size, the constructor is new ArrayList(); With a size, the constructor is new ArrayList(size);
  • Value Assignment: To assign a value to the list, you can call ArrayList.add(Object), which automatically puts the object at the end of the occupied part of the array list. Or you can call ArrayList.add(index, Object), which performs exactly the same as array[index] = Object;
  • Value Accessing: To access a value from the list, replace array[index] with ArrayList.get(index);




  • That's pretty well all there is to it. If you are doing all that correctly, and still getting errors, post your code, and possible the stack trace of the exception, and I might be able to shed more light on the problem.

    - Adam
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50277
    80
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    As Adam Nace says, ArrayLists are easy to use. I trust you have been through what the Java tutorial says about Collections, and if you are using J2SE5.0, you will want to set up your ArrayList using a generic type.
     
    Neeraja Murali
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Marc,Justin, Adam,& Campbell,

    Thanks for replying to my query. I figured out that the problem arose becos although i had included JDK1.5/bin in the path variable, there also seemed to be some reference to an older version of java. This was the reason why my ArrayList program was not compiling.

    Cheers
    Neeru
     
    Neeraja Murali
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Marc,Justin, Adam,& Campbell,

    Thanks for replying to my query. I figured out that the problem arose becos although i had included JDK1.5/bin in the path variable, there also seemed to be some reference to an older version of java. This was the reason why my ArrayList program was not compiling.

    Cheers
    Neeraja
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50277
    80
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for replying to my query.
    We are only too pleased to help.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic