aspose file tools*
The moose likes Beginning Java and the fly likes Can anybody tell me how to use make use of ArrayLists in my program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can anybody tell me how to use make use of ArrayLists in my program" Watch "Can anybody tell me how to use make use of ArrayLists in my program" New topic
Author

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

Neeraja Murali
Greenhorn

Joined: Jun 08, 2006
Posts: 4
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

Joined: Jan 24, 2006
Posts: 802
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


You down with OOP? Yeah you know me!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Jul 17, 2006
Posts: 117
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

    Joined: Oct 13, 2005
    Posts: 38765
        
      23
    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

    Joined: Jun 08, 2006
    Posts: 4
    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

    Joined: Jun 08, 2006
    Posts: 4
    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

    Joined: Oct 13, 2005
    Posts: 38765
        
      23
    Thanks for replying to my query.
    We are only too pleased to help.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Can anybody tell me how to use make use of ArrayLists in my program