8 Replies - 5020 Views - Last Post: 20 June 2009 - 04:19 AM Rate Topic: -----

#1 ivey.eli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 196
  • Joined: 11-April 09

Calling an Objects methods from inside an ArrayList

Post icon  Posted 19 June 2009 - 09:57 PM

Hi,

I was wondering how I could call an objects methods when it is stored inside an ArrayList without having to do what I did in this piece of code.

public void printAllPeoplesNames()
{
// people is an ArrayList 
   Iterator it = people.iterator();
	
	while (it.hasNext())
 {   
	 Person checker = (Person) it.next();
	System.out.println(checker.getName());
}
}



Is there a way for me to call the method directly from the ArrayList instead of having to assign a variable (
 Person checker = (Person) it.next(); 
the value of a particular element, then call the method (
 System.out.println(checker.getName()); 
)

Hope that makes sense....

Is This A Good Question/Topic? 0
  • +

Replies To: Calling an Objects methods from inside an ArrayList

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Calling an Objects methods from inside an ArrayList

Posted 19 June 2009 - 11:09 PM

So you just want to loop your Array list, whilst calling a method on the Objects in the Arraylist? WHats your ArrayList called? If its called checker, you can just do
for(int x = 0; x = checker.length;  x++) {  //maybe checker.size()
System.out.println(checker.getName());
}
			

This post has been edited by nick2price: 19 June 2009 - 11:11 PM

Was This Post Helpful? 0
  • +
  • -

#3 ivey.eli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 196
  • Joined: 11-April 09

Re: Calling an Objects methods from inside an ArrayList

Posted 19 June 2009 - 11:19 PM

No, I want to be able to call a method from an object stored inside an ArrayList...

So instead of having to do this
  Person checker = (Person) it.next();
	System.out.println(checker.getName()); 


I was wondering if there was a way to call the method directly from "people" (the ArrayList ) and not a variable that has to be constantly updated...

Does that make more sense?
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Calling an Objects methods from inside an ArrayList

Posted 19 June 2009 - 11:22 PM

So person is your ArrayList? Just change where i placed check in my other post with person. This will loop your person ArrayList printing out the getName method for every person.
Was This Post Helpful? 0
  • +
  • -

#5 ivey.eli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 196
  • Joined: 11-April 09

Re: Calling an Objects methods from inside an ArrayList

Posted 19 June 2009 - 11:37 PM

ok I was getting some errors when I compiled this (I changed x = people.length() to x == people.size(); )

for(int x = 0; x == people.size;  x++) {  //maybe checker.size()
System.out.println(people.getName());



error: size has private access in java.util.ArrayList;

idk why... but that doesn't matter that much (I think :S)

When I tried to compile this part of the code (the only part I really care about [calling methods from an Object stored within an ArrayList])


System.out.println(people.getName()); //people being the ArrayList;



error: cannot find symbol - method.getName (maybe because getName is not a method that an ArrayList has?)

Any Ideas on how I can call methods from objects stored within an ArrayList.

This post has been edited by ivey.eli: 20 June 2009 - 01:50 AM

Was This Post Helpful? 0
  • +
  • -

#6 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 664
  • Joined: 20-March 09

Re: Calling an Objects methods from inside an ArrayList

Posted 20 June 2009 - 01:43 AM

Your method getName() needs parentheses on it.
Was This Post Helpful? 0
  • +
  • -

#7 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Calling an Objects methods from inside an ArrayList

Posted 20 June 2009 - 02:33 AM

Do you actually have a class called person? Do you have a method in that class called getName? If you dont have a class called person, you cant have an ArrayList of type person. If you dont have a method called getName in that class, you cant use this method.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,805
  • Joined: 16-October 07

Re: Calling an Objects methods from inside an ArrayList

Posted 20 June 2009 - 03:21 AM

Well, obviously, you have to get to the objects somehow. If you don't want to cast and assign to a variable, you can cast on the same call.

e.g.
public void printAllPeoplesNames(ArrayList people) {
	Iterator it = people.iterator();
	while (it.hasNext()) {  
		System.out.println(((Person)it.next()).getName());
	}
}



I don't really like collections of "object", though. They force you to use messy casts and pray that you didn't break the rules along the way. Better to be specific about the class type in your collection. Then you don't have to cast and can have cleaner loops.

e.g.
class People extends ArrayList<Person> {
	
	public void printAllNames() {
		for(Person item : this) {
			System.out.println(item.getName());
		}
	}
	
	public void add(String name) { this.add(new Person(name)); }
}


Was This Post Helpful? 1
  • +
  • -

#9 ivey.eli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 196
  • Joined: 11-April 09

Re: Calling an Objects methods from inside an ArrayList

Posted 20 June 2009 - 04:19 AM

Thanks for the help guys :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1