ArrayList


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: ArrayList

  1. #1
    Join Date
    Feb 2005
    Posts
    8

    ArrayList

    Hi,

    I have an ArrayList, ethernet, of objects, class Ethernet. I am trying to change the contents of one of the objects in the ArrayList. However, I am unable to.

    Here is a sample of what I'm tying to do:

    ((Ethernet) ethernet.get(1)).avgNor = 99;

    The value in avgNor remains to what it was originally, no changes occur.

    I appreciate your help.

    Thanks,
    N29

  2. #2
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    That gets you a copy, not access to the object in the list. You'll need to get the object, change the value and put it back into the list.

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Unhappy Oh .?

    I suspect the sample code :
    Code:
    ((Ethernet) ethernet.get(1)).avgNor = 99;
    is not where the error is.
    It indicates that avgNor is a numeric and also a public member
    of the Ethernet class, if the above compiles then the error is elsewhere.


    Code:
    import java.util.*;
    
    class IntClass {
      public int value=0;
      public IntClass (int value) {
        this.value=value;
      }
    }
    public class Stuff {
      ArrayList list=new ArrayList();
      public Stuff() {
        for (int i=0; i<10; i++) {
          list.add(new IntClass(i));
        }
        for (int i=0; i<list.size(); i++) {
          System.out.print( ((IntClass)list.get(i)).value+" ");
        }
        System.out.println("");
    
        ((IntClass)list.get(5)).value=200; // works like a dream
    
        for (int i=0; i<list.size(); i++) {
          System.out.print( ((IntClass)list.get(i)).value+" ");
        }
      }
      public static void main(String[] args) {
        Stuff stuff1 = new Stuff();
      }
    }
    Output:

    Code:
    0 1 2 3 4 5 6 7 8 9 
    0 1 2 3 4 200 6 7 8 9
    The get method of ArrayList does not give a copy of anything
    else but the object address
    .
    Last edited by sjalle; 06-14-2005 at 06:59 AM.
    eschew obfuscation

  4. #4
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    Thanks for the example.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center