Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > pulling object methods out of an ArrayList

Reply
Thread Tools

pulling object methods out of an ArrayList

 
 
SuperGh0d@gmail.com
Guest
Posts: n/a
 
      12-22-2006
Hello all.

I have build several objects and stored them in an ArrayList. I have
try many different things to iterate through the Arraylist and retrieve
data stored in the object but I am having no luck. If I just print the
ArrayList myVars I get

[com.paul.stuff.Variable@fa4a19, com.paul.stuff.Variable@16bb9b,
com.paul.stuff.Variable@76d64a]

How to I access the objects inside the arraylist?

thanks!

 
Reply With Quote
 
 
 
 
Manish Pandit
Guest
Posts: n/a
 
      12-22-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello all.
>
> I have build several objects and stored them in an ArrayList. I have
> try many different things to iterate through the Arraylist and retrieve
> data stored in the object but I am having no luck. If I just print the
> ArrayList myVars I get
>
> [com.paul.stuff.Variable@fa4a19, com.paul.stuff.Variable@16bb9b,
> com.paul.stuff.Variable@76d64a]
>
> How to I access the objects inside the arraylist?
>
> thanks!


If you are using JDK 1.5, try:

for (Variable var : myVars){

//do something with 'var'

}

-cheers,
Manish

 
Reply With Quote
 
 
 
 
ck
Guest
Posts: n/a
 
      12-22-2006
By defrault toString method would be invoked when you try to print the
obtained object directly.
Even if you try to print the ArrayList toString would be invoked again.
In most of the cases toString would have the default implementation of
Object class i.e it would return the

ClassName@hashcode

So you are getting the output as
> [com.paul.stuff.Variable@fa4a19, com.paul.stuff.Variable@16bb9b,
> com.paul.stuff.Variable@76d64a]


If you would like to get some sensible output for the same you would
need to over ride the toString method in Variable class.

May be this small example would make things clear to you


=============== Code Start ==========================
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* @date Dec 22, 2006
* @author Ck
* @copyright (c) http://www.gfour.net
*/
public class TestArrayList {
public static void main(String [] args){
Object o=null;
List< Object> list = new ArrayList<Object>();
list.add(new String("String1"));
list.add(new Integer(10));
list.add(new Person("Sample name Example"));
Iterator<Object> it = list.iterator();
// if you iterate normally and toString() is not implemented
System.out.println("if you iterate normally and toString() is not
implemented");
while (it.hasNext()){
o = it.next();
System.out.println(o);
}
// Here we are casting the object to Person type and invoking getName
System.out.println(((Person) o).getName());
}
}
class Person {
Person (String name){
this.name=name;
}
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

=============== Code ends ========================

Please remember that String and Integer wrapper classes have the
toString method to return the value.

If you modify the Person class as follow you would notice the change in
output

=============== Code Starts ===========================

class Person {
Person (String name){
this.name=name;
}
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
// Overriding the toString method of the object class
public String toString(){
return this.name;
}
}

============== Code ends ==============

Hope this helps.

Cheers,
Ck
http://www.gfour.net



(E-Mail Removed) wrote:
> Hello all.
>
> I have build several objects and stored them in an ArrayList. I have
> try many different things to iterate through the Arraylist and retrieve
> data stored in the object but I am having no luck. If I just print the
> ArrayList myVars I get
>
> [com.paul.stuff.Variable@fa4a19, com.paul.stuff.Variable@16bb9b,
> com.paul.stuff.Variable@76d64a]
>
> How to I access the objects inside the arraylist?
>
> thanks!


 
Reply With Quote
 
Alfred
Guest
Posts: n/a
 
      12-22-2006
(E-Mail Removed) wrote:
> Hello all.
>
> I have build several objects and stored them in an ArrayList. I have
> try many different things to iterate through the Arraylist and retrieve
> data stored in the object but I am having no luck. If I just print the
> ArrayList myVars I get
>
> [com.paul.stuff.Variable@fa4a19, com.paul.stuff.Variable@16bb9b,
> com.paul.stuff.Variable@76d64a]
>
> How to I access the objects inside the arraylist?


via Iterator: (mostly the better way)
for (Iterator iter = yourList.iterator(); iter.hasNext(); ) {
System.out.println(iter.next());
}
or index:
for (int i = 0, size = yourList.size(); i < size; i++) {
System.out.println(yourList.get(i));
}

Alfred
 
Reply With Quote
 
SuperGh0d@gmail.com
Guest
Posts: n/a
 
      12-22-2006
Thank you!!!!!! Casting worked great! I really appreciate everyones
help.

ck wrote:
> By defrault toString method would be invoked when you try to print the
> obtained object directly.
> Even if you try to print the ArrayList toString would be invoked again.
> In most of the cases toString would have the default implementation of
> Object class i.e it would return the
>
> ClassName@hashcode
>
> So you are getting the output as
> > [com.paul.stuff.Variable@fa4a19, com.paul.stuff.Variable@16bb9b,
> > com.paul.stuff.Variable@76d64a]

>
> If you would like to get some sensible output for the same you would
> need to over ride the toString method in Variable class.
>
> May be this small example would make things clear to you
>
>
> =============== Code Start ==========================
> import java.util.ArrayList;
> import java.util.Iterator;
> import java.util.List;
>
> /**
> * @date Dec 22, 2006
> * @author Ck
> * @copyright (c) http://www.gfour.net
> */
> public class TestArrayList {
> public static void main(String [] args){
> Object o=null;
> List< Object> list = new ArrayList<Object>();
> list.add(new String("String1"));
> list.add(new Integer(10));
> list.add(new Person("Sample name Example"));
> Iterator<Object> it = list.iterator();
> // if you iterate normally and toString() is not implemented
> System.out.println("if you iterate normally and toString() is not
> implemented");
> while (it.hasNext()){
> o = it.next();
> System.out.println(o);
> }
> // Here we are casting the object to Person type and invoking getName
> System.out.println(((Person) o).getName());
> }
> }
> class Person {
> Person (String name){
> this.name=name;
> }
> private String name;
>
> public String getName() {
> return name;
> }
>
> public void setName(String name) {
> this.name = name;
> }
> }
>
> =============== Code ends ========================
>
> Please remember that String and Integer wrapper classes have the
> toString method to return the value.
>
> If you modify the Person class as follow you would notice the change in
> output
>
> =============== Code Starts ===========================
>
> class Person {
> Person (String name){
> this.name=name;
> }
> private String name;
>
> public String getName() {
> return name;
> }
>
> public void setName(String name) {
> this.name = name;
> }
> // Overriding the toString method of the object class
> public String toString(){
> return this.name;
> }
> }
>
> ============== Code ends ==============
>
> Hope this helps.
>
> Cheers,
> Ck
> http://www.gfour.net
>
>
>
> (E-Mail Removed) wrote:
> > Hello all.
> >
> > I have build several objects and stored them in an ArrayList. I have
> > try many different things to iterate through the Arraylist and retrieve
> > data stored in the object but I am having no luck. If I just print the
> > ArrayList myVars I get
> >
> > [com.paul.stuff.Variable@fa4a19, com.paul.stuff.Variable@16bb9b,
> > com.paul.stuff.Variable@76d64a]
> >
> > How to I access the objects inside the arraylist?
> >
> > thanks!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does the clone() method of ArrayList<> make a copy of the objects in the ArrayList? xz Java 16 08-04-2007 10:33 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
writeObject with ArrayList of ArrayList? Kaidi Java 4 01-03-2004 08:16 PM
Error calling method on object... I'M PULLING MY HAIR OUT HERE!! Richard C++ 6 11-15-2003 01:06 AM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments