• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to check if a ArrayList of of object contains a certain String?  RSS feed

 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a number of objects stored in an ArrayList called inventory.
let's say I have two objects inside.



I am making a function to search through the whole inventory to see if any of the Lamborghini object has a certain model name such as aventador, diablo, etc....

This is what I have but I figured there's a big mistake when I make it true / false; it's making it going through the list and what's return is the last one instead of saying there's such match in the whole list or not.



can someone give me a hint? Thanks in advance.

P.S. I figured if I add under it'll work because as soon as it found one match then it'll turn to true and break out the loop but I don't think this is the best way to do it right?
 
Minal Silimkar-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Edin,

You can make use of contains(Object o) method in ArrayList which returns true if this list contains the specified element.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edin Tin wrote:P.S. I figured if I add under it'll work because as soon as it found one match then it'll turn to true and break out the loop but I don't think this is the best way to do it right?


It's largely a matter of personal preference, but I like to avoid using break wherever I can. So I would probably write the code like
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edin Tin wrote:
P.S. I figured if I add under it'll work because as soon as it found one match then it'll turn to true and break out the loop but I don't think this is the best way to do it right?


That is the best way, and the way it is often written for this sort of for loop (ETA: or Darryl's method...).
You can remove the 'else' block at that point as well as you default the 'exist' flag to false anyway.

Minal Silimkar-Urankar wrote:
You can make use of contains(Object o) method in ArrayList which returns true if this list contains the specified element.


That won't work as the value being compared is an attribute of the objects stored in the List.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote: . . .
It's largely a matter of personal preference, but I like to avoid using break wherever I can. . . .
Is there a difference between break and return?

You can avoid both return and break like this:-You can do the same sort of thing with a for loop.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And with Java 8:
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edin Tin wrote:
I figured if I add under it'll work because as soon as it found one match then it'll turn to true and break out the loop but I don't think this is the best way to do it right?

I'm not too picky about putting break in the middle of a loop as long as the enclosing method is small and focused, as is your method. There's really no need to set exists = false in the loop. Here's the story, in plain English:

Which translates directly to:

So what if I exited the for-loop before it could go through the whole list, ¯\_(ツ)_/¯, I didn't really need to continue once I found something so why bother? It may be a personal preference but I actually like that better than while (!found && i < array.length) -- it has one less boolean expression to look at and it's clearer that we're trying to go through the list.

Edit: If this were a method that had 100 lines of code and multiple places where you used break and/or return, then that's a different story. Then we'd have to have a little heart-to-heart talk about good coding practices.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:And with Java 8:

This is even nicer if you generalize it:
 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everyone for the reply~
learned a few things here
this assignment is getting my headaches...might need to bother you guys for a few days heheh

P.S. another quit question about the same function. I was told that return false of modelName is not found or if carLot ArrayList is null. How do I type the statement to say carLot ArrayList is null and it'll be false? I tempt to leave it out because I think it'll be false automatically if nothing is found anyways isn't it?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will likely get a NullPointerException if the object is null. Have you tried searching for check if an object reference is null in Java? Google is your friend.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!