Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: looping through an ArrayList of objects with their own attributes

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question looping through an ArrayList of objects with their own attributes

    hello again,

    i am trying to loop through an ArrayList of type object. the objects have their own attributes and i'm trying to call a specific method that accesses a specific attribute. in this case, the objects refer to pets and i am trying to total up their leg count in separate java files. i'll have to call this method in my tester class.

    here is PetTester.java

    import java.util.ArrayList;
     
    /* This program tests the Pet superclass and its subclasses */
    public class PetTester
    {
     
    	public static void main(String [] args)
    	{	
    		Cat spider = new Cat("Spider", "Alley","Black", "crystals", 7);
    		Dog rover = new Dog("Rover", "Labrador","Chocolate", true, "large", 100);
    		Dog tippi = new Dog("Tippi", "Poodle","Silver",false, "small", 15);
    		Bird toucan = new Bird("Nismo", "toucan", 2, true);
     
    		ArrayList<Pet> pets = new ArrayList<Pet>();
    		pets.add(spider);
    		pets.add(rover);
    		pets.add(tippi);
    		pets.add(toucan);
     
    		String spiderName = spider.getName();
    		String spiderLitterType = spider.getLitterType();
    		String spiderColor = spider.getColor();
    		String spiderBreed = spider.getBreed();
    		int spiderLegs = spider.getLegs();
    		System.out.println(spiderName + " who is of type " + spiderBreed + " and color " + spiderColor +
    		" which uses " + spiderLitterType + " litter has " + " legs: "+ spiderLegs);
     
    		String roverName = rover.getName();
    		boolean roverTraining = rover.getTrained();
    		String roverSize = rover.getSize();
    		String roverColor = rover.getColor();
    		String roverBreed = rover.getBreed();
    		int roverLegs = rover.getLegs();
     
    		System.out.println(roverName + " who is of type " + roverBreed + " and color " + roverColor +
    		" and " + roverSize + " size which is trained " + roverTraining +
    		" has " + " legs: "+ roverLegs);
     
    		System.out.println(tippi.getName() + " who is of type " + tippi.getBreed() + " and color " + tippi.getColor() + " and " + tippi.getSize() +
    		" size which is trained " + tippi.getTrained() + " weighs " + tippi.getWeight() + " has " + " legs: "
    		+ tippi.getLegs());
     
    		System.out.println(toucan.getName() + " is a " + toucan.getBreed() + " who has " + toucan.getLegs()
    				+ " legs, and " + toucan.canTalk() + " talk.");
    	}
    }

    now here is Pet.java where this method, totalLegs(ArrayList<Pet> pets) is actually created.
    import java.util.ArrayList;
     
    public class Pet
    {
    	String name;
    	String breed;
    	String color;
    	int legs;
    	int weight;
    	boolean speaks;
     
    	public Pet(String name, String breed, String color, int legs, int weight)
    	{
    		this.name = name;
    		this.breed = breed;
    		this.color = color;
    		this.legs = legs;
    		this.weight = weight;
    	}
     
    	public Pet(String name, String breed, String color, int weight)
    	{
    			this.name = name;
    			this.breed = breed;
    			this.color = color;
    			this.legs = 4;
    			this.weight = weight;
    	}
     
    	public Pet(String name, String breed, int legs, boolean speaks)
    	{
    		this.name = name;
    		this.breed = breed;
    		this.legs = 2;
    		this.speaks = speaks;
    	}
     
    	public Pet()
    	{
    		this.name = "";
    		this.breed = "mutt";
    		this.color = "black";
    		this.legs = 4;
    		this.weight = 0;
    	}
    	public String getName()
    	{
    			return name;
    	}
     
    	public String getBreed()
    	{
    		return breed;
    	}
     
    	public String getColor()
    	{
    		return color;
    	}
     
    	public int getLegs()
    	{
    		return legs;
    	}
    	public int getWeight()
    	{
    		return weight;
    	}
     
    	[B]public static int totalLegs(ArrayList<Pet> pets)
    	{
    		int totalLegs = 0;
    		for (int i = 0; i < pets.size(); i++)
    		{
    			totalLegs = pets.getLegs();
    		}[/B]
    	}
    }

    how do i access their parameters and add them all up?


  2. #2
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: looping through an ArrayList of objects with their own attributes

    thats easy , i do that all the time

    int legCount = 0;  // create the int to total the legs 
    for ( int i = 0; i < pets.size(); i++){
       legCount += pets.get(i).getLegs(); // use the get(i) to get that index of pet , and then the getLegs() 
    }                                                            // to return the number of legs that that pet has to be added to the total
    Programming: the art that fights back

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: looping through an ArrayList of objects with their own attributes

    Sure thing, but would a cat named spider have 4 or 8 legs?

    Or maybe none, what with a 100 lb (kg?) dog being so near

    db

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Arraylist Objects to Database
    By frankycool in forum JDBC & Databases
    Replies: 3
    Last Post: November 15th, 2009, 08:01 PM
  3. How can i store ArrayList objects in Access database
    By frankycool in forum JDBC & Databases
    Replies: 0
    Last Post: November 4th, 2009, 12:44 AM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM