aspose file tools*
The moose likes Beginning Java and the fly likes Searching through Array List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Searching through Array List" Watch "Searching through Array List" New topic
Author

Searching through Array List

Mark Miranda
Greenhorn

Joined: Feb 25, 2011
Posts: 24
Hello,

I was wondering if someone could help me try to figure this out.

I have an ArrayList which contains two entries.

[Keith Legend, John James]

How can I search through the ArrayList based on the last name? For example, if I type in James....it should print out John James.

Here's what I have so far:



and here's my CD class



Here's my output:


[Keith Legend]
[Keith Legend, John James]
false


At this point, i've just hard coded what I want to find.

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9


Step back and think a bit. You are asking your record (which is an ArrayList of CD objects), if it contains the name "James"
Is that what you really want to do? Or do you want to find out if record contains a CD whose first/last name is "James"?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Mark Miranda
Greenhorn

Joined: Feb 25, 2011
Posts: 24
Maneesh Godbole wrote:
Step back and think a bit. You are asking your record (which is an ArrayList of CD objects), if it contains the name "James"
Is that what you really want to do? Or do you want to find out if record contains a CD whose first/last name is "James"?


I would like to find out if my ArrayList contains the last name of "James".
Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

I would probably write a new class to hold all the cd's, eg. DiscWarehouse, that would expose all the methods I need..



Also, pay attention to your variable names, as you declared record as an ArrayList, but by its name, I would have thought it was just a CD.

Cheers
Claudiu
Mark Miranda
Greenhorn

Joined: Feb 25, 2011
Posts: 24
thanks! i'll give it a try.

Also, I read somewhere about implementing compareTo() is that something I could use?

Mark Miranda
Greenhorn

Joined: Feb 25, 2011
Posts: 24
Claudiu Chelemen wrote:I would probably write a new class to hold all the cd's, eg. DiscWarehouse, that would expose all the methods I need..



Also, pay attention to your variable names, as you declared record as an ArrayList, but by its name, I would have thought it was just a CD.

Cheers
Claudiu


I'm still a bit confuse with the implementation of this class. Would you be able to show me how to incorporate this to my test code?

Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

If you make your CD class implement java.lang.Comparable interface, you may use the compareTo() method to compare two CD objects given a certain criteria. But that would be helpful mostly in sorting your list, and not to extract a sublist of your choice from it.

Claudiu
Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

Something like...


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
Not quite. I would have thought that a CD will always have a title, artist’s name, etc. So you won’t want a no-arguments constructor. The compareTo method isn’t appropriate because CDs don’t have a “natural ordering”. But you are right that you can iterate through the List, find the name/first name/last name etc, and add them to another List if the name is equal to “James”

Another thing you can do, which is more complicated, is
  • When you add a CD to the List, add it to a Map.
  • Use the put() method of the Map to put the name as a key and a List<CD> as the value, which you add the CD to.
  • You now have a Map linking name of performer to all the CDs.
  • The type of the Map would be Map<String, List<CD>>, (or Map<Name, List<CD>>).
  • If you use a SortedMap (eg TreeMap) you can get all the names in alphabetical order.
  • If you go here, there is a counting example, which uses a Map rather similarly.
  • dennis deems
    Ranch Hand

    Joined: Mar 12, 2011
    Posts: 808
    Mark Miranda wrote:
    Maneesh Godbole wrote:
    Step back and think a bit. You are asking your record (which is an ArrayList of CD objects), if it contains the name "James"
    Is that what you really want to do? Or do you want to find out if record contains a CD whose first/last name is "James"?


    I would like to find out if my ArrayList contains the last name of "James".


    When you call contains on an ArrayList, it is going to iterate over its elements and use the equals method to find the object you have requested. So in your code you have asked your list to find out if the String "James" equals any of the CD objects. This won't work.
    Mark Miranda
    Greenhorn

    Joined: Feb 25, 2011
    Posts: 24
    Thanks for the advice everyone!

    Since I am storing objects in my ArrayList, I figured maybe its best if i access the methods directly like so:

    record.get(i).getLastName();
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Searching through Array List