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 6 of 6

Thread: Ask objects with parameters in Arraylist

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Ask objects with parameters in Arraylist

    I have a dog class type list which contains many objects of that class(many dogs each new object) with constructor input. For example list Dogs with type dog contains dog with weight, age, length: dog(30,5,100), dog(40,6,120). I want the program to print out all dogs with age 5.

    I would think something like that, but cant figure it out.
     
    public Dog askDogs(int age){                  //user enters requested age
    		for(Dog d:Dogs){
    			if(d.askAge()==age){         //I have method askAge() in dog class which returns integer			
    		}
               }
               return Dogs;
       }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ask objects with parameters in Arraylist

    I want the program to print out all dogs with age 5
    The posted code has an if statement that will find dog objects with the age 5. Then what should the code do when it has found one? There is no code inside the if statement. Should there be a println statement to print the info in the dog object?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ask objects with parameters in Arraylist

    I know that I could use Dogs.get(); to print out certain object from list but I want to print out everything if that if statement is filled.

    EDIT: It works now with that code. Anyway is there built in stuff that asks all object parameters(weight, mass, age) and how is it possible to remove these objects from list after program has showed them(after user requested them to see, dogs with age 5)?

    public void askDogs(int age){                  //user enters requested age
    		for(Dog d:Dogs){
    			if(d.askAge()==age){         //I have method askAge() in dog class which returns integer	
                           System.out.println(d.askAge());		
    		}
               }
       }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ask objects with parameters in Arraylist

    You have coded two different ways to call the askAge() method. The first one is correct, the second one should be changed to be like the first one. Use a reference to a Dog object to call a method in the Dog class. In the for loop, d is a reference to a Dog class object.

    What are the contents of a Dog class object that you want to print? The posted code only prints the number returned by the askAge() method. Does the Dog class have other methods that return more data?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ask objects with parameters in Arraylist

    Okay, I wonder now how I can say that I have dog with age 5 in list. This one says sentence so many as I have that item in list. How can I look through list without using for loop than?
    public void askDogs(int age){                  //user enters requested age
    		for(Dog d:Dogs){
    			if(d.askAge()==age){         //I have method askAge() in dog class which returns integer	
                           System.out.println("There is a dog in the list with that age");		
    		}else{
                           System.out.println("There is not a dog in the list with that age");
                    }
               }
       }

    Possibly .contains method but I am not sure how to check if ask.Age()==age in that case.

    	public void hasDog(int age){
    		if (Dogs.contains(askAge()==age)){                                      ????????????????????
                       System.out.println("There is a dog in the list with that age");
    	}else{
                       System.out.println("There is not a dog in the list with that age");
    }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Ask objects with parameters in Arraylist

    if(d.askAge()==age){
    That is the right way to see if the Dog object's age matches the required age.

    The contains() method will not look at the contents of a Dog object.

    How can I look through list without using for loop
    You have to look at each one to see if its contents matches what you are looking for.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Objects as parameters, and implicit calling?i
    By MW130 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 16th, 2013, 05:08 PM
  2. [SOLVED] ArrayLists - Getting index of parameters of objects of ArrayLists.
    By mwebb in forum Object Oriented Programming
    Replies: 1
    Last Post: February 16th, 2012, 07:39 PM
  3. Bubblesort of an ArrayList with objects
    By bondage in forum Algorithms & Recursion
    Replies: 9
    Last Post: January 4th, 2012, 11:51 AM
  4. Really need some help with Objects inside an ArrayList
    By ISuckSoBad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 15th, 2011, 12:33 PM
  5. Minimum value of objects in ArrayList
    By Sputnik in forum Loops & Control Statements
    Replies: 2
    Last Post: October 23rd, 2010, 01:20 PM