• Post Reply Bookmark Topic Watch Topic
  • New Topic

Searching an ArrayList  RSS feed

 
gail belfast
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to search an array list to see if a actors name appears in that list. The code i use works in that i get back the name of the actor but i am not sure how to amend it so it will print a statement when the actors name given does not appear in the array list. I have tried a number of ways just as using a boolean but i could not get it to work quite right. Any help would be greatly welcomed .

filmInfo is the name of the ArrayList

static ArrayList<DVD> filmInfo = new ArrayList<DVD>();

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code snippet contains a lot of things that are undeclared such as filmInfo and searchedActor. It also contains things that are declared but never used, such as searchDVD and findDVD.

I can also see at least one issue that would stop it compiling even if it was part of a larger piece of code that contained the missing declarations (on line 17 of what you have posted).


As to your question, perhaps if you show us what you tried and tell us what isn't working about it we could point you in the right direction.
 
gail belfast
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:Your code snippet contains a lot of things that are undeclared such as filmInfo and searchedActor. It also contains things that are declared but never used, such as searchDVD and findDVD.

I can also see at least one issue that would stop it compiling even if it was part of a larger piece of code that contained the missing declarations (on line 17 of what you have posted).


As to your question, perhaps if you show us what you tried and tell us what isn't working about it we could point you in the right direction.


I have edited the question to fix what you pointed out. I have rerun the program again using a name that appears in the list and it tells me that that the dvd exists in the list. So what I am asking is there any why of using the Array list method .contains to give back a response it something does not appear in the list.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gail belfast wrote:I need to search an array list to see if a actors name appears in that list.

Actually, you appear to be searching an ArrayList of DVD's to see if an actor stars in it. And you're doing it by brute force.

There's nothing particularly wrong with that, but wouldn't it be better, when you're creating your List of DVD's, to also create a Map (actually a Multimap) that says which actors are in which DVDs?

Alternatively (and possibly better), have each DVD contain a List (or Set) of the actors that star in it. I believe this was already suggested in your other thread.

Make objects do the work for you; not code.

Winston

PS: Your code lines are far too long. I've broken up the worst ones for you, but please read the link.
 
gail belfast
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
gail belfast wrote:I need to search an array list to see if a actors name appears in that list.

Actually, you appear to be searching an ArrayList of DVD's to see if an actor stars in it. And you're doing it by brute force.

There's nothing particularly wrong with that, but wouldn't it be better, when you're creating your List of DVD's, to also create a Map (actually a Multimap) that says which actors are in which DVDs?

Alternatively (and possibly better), have each DVD contain a List (or Set) of the actors that star in it. I believe this was already suggested in your other thread.

Make objects do the work for you; not code.

Winston

PS: Your code lines are far too long. I've broken up the worst ones for you, but please read the link.



i really don't understand what you mean. I have five DVDs in the ArrayList and according to the Head First book i can use .contains to find if something exists in the list so i am guessing it is not possible to use .contains to search for something that is not in the list?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gail belfast wrote:i really don't understand what you mean. I have five DVDs in the ArrayList and according to the Head First book i can use .contains to find if something exists in the list so i am guessing it is not possible to use .contains to search for something that is not in the list?


The contains() method of Lists will return true if the thing you are searching for is in the List and will return false if the thing is not in the list. So, you can use it both to check if something is in the List and to check if something is not in the list.

However what Winston pointed out is that you are searching for Actor's names (as Strings), but your list contains DVDs*. You cannot use contains() to search the contents of the objects in the list, only the contents of the list itself.

I'm assuming here that filmInfo is a List of DVDs, bit that variable is not declared in the code you have posted so I can't be certain of its type.
 
gail belfast
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:
gail belfast wrote:i really don't understand what you mean. I have five DVDs in the ArrayList and according to the Head First book i can use .contains to find if something exists in the list so i am guessing it is not possible to use .contains to search for something that is not in the list?


The contains() method of Lists will return true if the thing you are searching for is in the List and will return false if the thing is not in the list. So, you can use it both to check if something is in the List and to check if something is not in the list.

However what Winston pointed out is that you are searching for Actor's names (as Strings), but your list contains DVDs*. You cannot use contains() to search the contents of the objects in the list, only the contents of the list itself.

I'm assuming here that filmInfo is a List of DVDs, bit that variable is not declared in the code you have posted so I can't be certain of its type.


The Arraylist holds the the actor, the dvd title and the year it was released.
For example DVD dvd6 = new DVD( "Jennifer Lawrence ", "The Hunger Games", 2012 );
then added to the list by filmInfo.add(dvd6).
So because of how the ArrayList has been set up it will therefore not be be possible to search it of the occurrence of one word ?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gail belfast wrote:The Arraylist holds the the actor, the dvd title and the year it was released.
For example DVD dvd6 = new DVD("Gary Ross ", "Jennifer Lawrence ", "The Hunger Games", 2012 );
then added to the list by filmInfo.add(dvd6).
So because of how the ArrayList has been set up it will therefore not be be possible to search it of the occurrence of one word ?


You have to loop over the contents of the whole List and look inside each DVD to see if it contains the Actor you're looking for (which you did on line 11).

Winston suggested using a Map to map Actors to the DVD's they star in which would make it easier to perform the search, but if this is an an educational exercise then Maps may be off topic depending on what it is they're teaching you.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gail belfast wrote:i really don't understand what you mean. I have five DVDs in the ArrayList and according to the Head First book i can use .contains to find if something exists in the list so i am guessing it is not possible to use .contains to search for something that is not in the list?

Well, since you haven't posted your DVD class, it's difficult to comment (especially since I don't know the specific exercise in question), but if I was writing the class, I'd probably have something like:and I'd make my search something like:and finally in findDVD():but it requires you to build objects (and classes) that help you out, rather than relying on Strings all the time. However, something similar could easily be done even with those.

HIH

Winston
 
gail belfast
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:
gail belfast wrote:The Arraylist holds the the actor, the dvd title and the year it was released.
For example DVD dvd6 = new DVD("Gary Ross ", "Jennifer Lawrence ", "The Hunger Games", 2012 );
then added to the list by filmInfo.add(dvd6).
So because of how the ArrayList has been set up it will therefore not be be possible to search it of the occurrence of one word ?


You have to loop over the contents of the whole List and look inside each DVD to see if it contains the Actor you're looking for (which you did on line 11).

Winston suggested using a Map to map Actors to the DVD's they star in which would make it easier to perform the search, but if this is an an educational exercise then Maps may be off topic depending on what it is they're teaching you.


So far we have only been taught simple arrays so using collections and other data structures like ArrayList are new to me and while i understand how to print the list and removing and adding to the list searching it is way over my head .
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gail belfast wrote:So far we have only been taught simple arrays so using collections and other data structures like ArrayList are new to me and while i understand how to print the list and removing and adding to the list searching it is way over my head .

Which is why it's even more important to write down what you need to do before you start coding; otherwise you'll just tie yourself in knots. I should add that what I wrote above is only one way to do it, and it's probably not optimal; but hopefully it gives you some idea of how to tackle the problem.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!