Results 1 to 10 of 10

Thread: Java: ArrayLists

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    11

    Java: ArrayLists

    Hey there,

    I am trying to use an ArrayList to store my user-defined objects yet I keep coming up with the same problem...

    Code:
    public static void main()
    {
    
    ArrayList list = new ArrayList();
    
    list.add(new item("Fred", 100));
    list.add(new item("Jim",200));
    
    System.out.println(((item)(list.get(0)).getIdentity());
    
    }
    When I run that code, the output is...

    JIM

    but of course is should be fred, is this a problem with referencing, it seems like the last item I add is overwriting the reference or data of the object(s) in the list. Can anyone suggest a solution or at least an explanation of the problem.

    Freddy_no_nose.
    Win if you can, Lose if you have to. BUT ALWAYS CHEAT.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Works fine for me.

    Delete all .class files and recompile everything.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    11
    Hey there,

    I just tried it and unfortuantly it didnt work. Could there be another reason for this. Lee.
    Win if you can, Lose if you have to. BUT ALWAYS CHEAT.

  4. #4
    Addicted Member MethadoneBoy's Avatar
    Join Date
    Oct 2001
    Location
    Preferably somewhere between Keira Knightley and Diane Kruger but I'm not fussy
    Posts
    180
    Try copying your .java files (not the .class files) into another directory, then compile and run from there.
    "'Oh, hello Mr. Crick! What do you think of Jeffrey Archer?' Clip-clip-clip! Oh, come on! Who are you kidding? You wait til I'm mayor, you'll see how tough I am! Christ almighty...."

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    11
    Sorry, no luck.

    Im building an example of the problem now.
    Win if you can, Lose if you have to. BUT ALWAYS CHEAT.

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    11
    Heres the test, with the same result. Lee
    Attached Files Attached Files
    Win if you can, Lose if you have to. BUT ALWAYS CHEAT.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Take your Java book and learn what the static keyword means...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    11
    What java book

    Ok, Ill take a look. Thanks.
    Win if you can, Lose if you have to. BUT ALWAYS CHEAT.

  9. #9
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104
    I agree with cornedbee...

    Is your item class possibly be static in your declaration? since you did not include the item class I'm not quite sure though, but the problem really lies there..static is the keyword.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yes, the data members are static in the zip file.

    Which, btw, means that they exists only once and are shared between all instances of the class. What you write out is actually the first class instance, but as all of them share the same data the second constructor call has simply overwritten it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width