Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ArrayList containing Objects. Help!

Status
Not open for further replies.

andypara

Programmer
Apr 25, 2002
32
GB
I have an ArrayList containing Objects which have String member variables. I need to output the values of these String member variables.

The ArrayList is called list1.

I've used list1.get(0) to return the 0 index Object in list1. How can I then access the returned Object's member variables?

Please be specific if you can because I'm quite new to Java.

Many Thanks
 
You need cast this Object to the correct class. Some example code:
Code:
import java.util.*;

class This {
  String x;
}
class That {
  String y;
}

class MixedList {

  private List _list = new ArrayList();

  public void add(Object o) {
    _list.add(o);
  }

  public String toString() {
    StringBuffer sb = new StringBuffer();
    Iterator it = _list.iterator();
    while(it.hasNext()) {
      Object o = it.next();
      if(o instanceof This)
        sb.append(
            ( (This)o ).x );
      if(o instanceof That)
        sb.append(
            ( (That)o ).y );
    }
    return sb.toString();
  }

  public static void main(String[] args) {
    This a = new This();
    a.x = "Hello";
    That b = new That();
    b.y = "There";
    MixedList list = new MixedList();
    list.add(a);
    list.add(b);
    System.out.println(list.toString());
  }
}
Note, using a mixed ArrayList is usually not a good idea. Using instanceof is also not usually preferred because it breaks the intention of polymorphism. The example again, but this time with polymorphism:
Code:
import java.util.*;

class This {
  String x;
  public String toString() {
    return x;
  }
}
class That {
  String y;
  public String toString() {
    return y;
  }
}

class MixedList {

  private List _list = new ArrayList();

  public void add(Object o) {
    _list.add(o);
  }

  public String toString() {
    StringBuffer sb = new StringBuffer();
    Iterator it = _list.iterator();
    while(it.hasNext()) {
      Object o = it.next();
      // example showing polymorphism
      sb.append( o.toString() );
    }
    return sb.toString();
  }

  public static void main(String[] args) {
    This a = new This();
    a.x = "Hello";
    That b = new That();
    b.y = "There";
    MixedList list = new MixedList();
    list.add(a);
    list.add(b);
    System.out.println(list.toString());
  }
}
Hope this helps :)
 
Im really new to java myself but..

Do you really need all that code just to acess a subfield in an object? I mean.. this isnt assembler right?
 
One of OOs aims is to "protect data" by encapsulation. This therefore necessitates calling methods to access data, rather than simply grabbing it. In the above, one could either get the String x, by o.x, OR o.toString() The latter is preferred, as it doesn't allow external objects to change the internal data of o
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top