• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to return an object in an arraylist  RSS feed

 
Steven Villarreal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have an object named person and they have:

    name
    birth month
    birth year
    birthday

And they are all stored in an arraylist. Is it possible to return certain objects by calling out the month if I create a method called?

I'm fairly new at java so forgive me if I asked anything dumb. I just don't know what else to put. The rest of the code is as follows though.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Yes. The old way would be to loop through the list of people, and add any person that has the matching search criteria to your return list:

However, since Java 8 you can use streams, which support high level filtering operations:

In the above code, toList() was imported from java.util.stream.Collectors.

There are some issues with your code that you should pay attention to:

  • Use strong types. int is a bad representation for a month of the year. Use java.time.Month instead. Even better is to use java.time.LocalDate for birth dates.
  • Declare variables and methods with the least specific type you need. Instead of ArrayList, return List, or even Collection.
  • Don't use raw types. When you declare a variable or method of a generic type, parameterize them properly. For instance, your method should return Collection<Person>.
  • Unless you have good reasons to make a class extensible, make them final.
  • You can get rid of your Analyzer() constructor if you initialize your fields when you declare them.
  • Why does addPerson() not accept a Person instead of a collection of properties that make up a Person?
  • The Person constructor should perform parameter checking and throw an exception if they are not valid.
  • You can implement your printPeople() method easily using the Iterable.forEach() method.
  • Instead of keeping your statistics in sync with the people you've added to the analyzer, you can just calculate them when you need them.
  • If your analyzer only contains a list of people, you can easily implement the removePerson() method using the Collection.removeIf() method.
  • Don't return null from methods. Return Optional.empty() instead.


  • This may appear a little bit advanced for you now, but maybe it will give you something to hold on to when learning about the concepts I've described earlier:

    This assumes that your Person class looks like this:
     
    Liutauras Vilda
    Sheriff
    Posts: 4925
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @OP

    1. Double check if you aren't mistaken about removing person form the stats based on his name equality.

    If there are persons:
    Person(John, 10, 7, 2000)
    Person(John, 22, 1, 1989)


    Your method is able to remove only the very first John running method once.


    2. Consider obtaining hard coded DAYS_PER_MONTH value in an automatic way, here is an old way how to do that, might there is a better way to do with newer classes, however, old way:



    3. I find code snippet below very confusing, especially bottom 2 lines (excl '}'), think if you could find some clearer way:

    Also I don't understand why are you creating Person's object which might be in illegal state, and in case of that, you don't add it. Maybe check beforehand if such Person object can be legal with given values?
     
    Liutauras Vilda
    Sheriff
    Posts: 4925
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also I think Person supposed to have some randomly generated identifier, which uniquely would identify its identity. Current implementation does not permit Persons with same names and which appear got birth on the same date.
     
    Steven Villarreal
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @ Stephan van Hulst I tried your method but it does  not seem to work? Maybe I put it in wrong or something

    and I'm using BlueJay to work on this project and i haven't upgraded to the newest version because my bluejay java book is an older version.
     
    Paul Clapham
    Sheriff
    Posts: 22835
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Two things: You'll want to create your ArrayList only once, before you start going through the Persons. And you'll want to delay returning it until you've gone through all of the Persons. After all, it's possible that there's more than one Person born in July, right?
     
    Paul Clapham
    Sheriff
    Posts: 22835
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Steven Villarreal wrote:... it does  not seem to work?


    If you're looking for help with something like that, it's much more helpful if you tell us what exactly isn't working. Follow this link: ItDoesntWorkIsUseless to our FAQ page which suggests what kind of information you should be posting.
     
    Steven Villarreal
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    LOL okay, but I guess what I was trying to say by " it doesn't work ," was that when I type in this

    I get the error "incompatible types" highlighted on row 3 "people" but the point of this method is to return people born in each month. For instance if I want to know who's born in July I would type in 7 and it would return the people born in that month. I just don't know how to do that though.
     
    Paul Clapham
    Sheriff
    Posts: 22835
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In your original post, the "people" variable was defined to be an ArrayList<Person>. So if that's still the case then "for (Person person: people)" should be correct. Did you change that variable definition? Or is there some other definition of "people" which might be intruding?
     
    Steven Villarreal
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    THANKS I had ArrayList People instead of ArrayList <person> People but now it says that it cannot find the symbol in line 4 birthMonth()
     
    Paul Clapham
    Sheriff
    Posts: 22835
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okay. The person variable is of type Person, so your code says that the Person class has a method named birthMonth(). Does it?
     
    Steven Villarreal
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yep it is as follows
     
    Paul Clapham
    Sheriff
    Posts: 22835
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Are you sure the name of that method is "birthMonth()"? Doesn't look like it to me.
     
    Steven Villarreal
    Greenhorn
    Posts: 13
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I put getBirthMonth and that seemed to do the trick.
     
    Steven Villarreal
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If I wanted to print out the people in the month I selected would I return a System.out.println if so what would I put in that code?
     
    Knute Snortum
    Sheriff
    Posts: 4281
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In general, you would return a List<Person> and the calling code would print:
     
    Knute Snortum
    Sheriff
    Posts: 4281
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Does your method still look like this?

    There are several problems with this code, most of which have been addressed in prior posts.  Can you post the lastest version of this method?
     
    Steven Villarreal
    Greenhorn
    Posts: 13
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I've updated it with the System.out.println but it doesn't seem to do anything like the method itself. Once again i'm trying to make it to where I put in the month for instance 3 and it returns all person objects whose birthMonths start with 3 and supposed to print them out but it does not do that.
     
    Paul Clapham
    Sheriff
    Posts: 22835
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Let's suppose you have to go through a pile of Lego bricks and get all the yellow blocks. Here's how you would do that:

    1. Get an empty box.

    2. Go through the Legos, and for each brick, if it's yellow put it in the box.

    3. Take away the box, which now contains all of the yellow bricks.

    Would you agree with that? Or would you do it differently?

    But your code doesn't do anything remotely like that algorithm. So I'd recommend throwing it away and replacing it with code which does do that.
     
    Knute Snortum
    Sheriff
    Posts: 4281
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think there are two things you are confused about: the concept of scope and the function of the return statement.  

    The scope of a variable is the idea of where a variable can be accessed or "seen."   For local variables (declared inside a method), the scope is generally from the point the variable is declared to the end of the block.  A block of code ends with a closing brace (}).  So let's look at your code.

    As you can see from the comments, the variable peopleMonth can't be accessed by the end of the method.

    Next, the function of the return keyword is that the method is terminated and the value of its argument is returned.  For the statement that means the value of peopleMonth is returned to the caller.  Because of this return statement, your method will return at most one Person -- not what you want.

    With this information you can implement Paul Chapham's algorithm (set of instructions):

    1. Get an empty box. --> declare an array

    2. Go through the Legos, and for each brick ... --> start a for loop

    ...if it's yellow put it in the box.  --> if the person's birth month is equal to the passed-in month, add it to the array.  Don't return because you want the method to keep processing.

    3. Take away the box, which now contains all of the yellow bricks. --> return the array, which is in scope because it was declared outside of the for loop and if statement
     
    Steven Villarreal
    Greenhorn
    Posts: 13
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I've figured it out thanks
     
    Paul Clapham
    Sheriff
    Posts: 22835
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Much better!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!