Solved

Java - find item in array

Posted on 2008-10-01
7
2,459 Views
Last Modified: 2013-11-23
I need to search an array of strings for a specific item (ID) and return an array of just the items found.  Can someone help me?  I'm very new to Java and am struggling with this.

Thank you,
Twitz76
0
Comment
Question by:Twitz76
7 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22616400
Well you can use the Arrays/Collections API to 1) sort the array - Arrays.sort(strArray) and then 2) search for your item - Arrays.binarySearch(strArray, itemID).

If what you are saying is you want to match all items in array to an ItemID or range of ItemIDs, this may help: http://crossedlogic.blogspot.com/2008/09/range-searching-using-java.html.  There is an example of simple looping and then using binary search.  This was meant for more a range, but can give you helpful tidbits none the less.


0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 22620853
You can use something like this,

List r = new ArrayList();
List l = ... // input;
for(Object o: l) {
  if(id.equals(o)) {
     r.add(o);
  }
}
// Now r contains all the elements you need
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22621111
Twitz76, any of this helping?

Ajay's example is like simple loop example in my link above.  It works well, as you can strongly type the List using List<String> and then eliminate case sensitivity by using id.equalsIgnoreCase(o).  Once you have filled the List object r, you can get an array by using r.toArray().

Let us know if you need anything else clarified.

Regards,
Kevin
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Twitz76
ID: 22623868
Experts,
I'm trying to wrap myself around the concept before I try to write the code.  This is new to me so I'm trying to understand each piece and what it does.  I want to "know" what I am doing before I do it.
Thanks!
Twitz76
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22625279
That is always best.  Just ask questions if something is not clear.
0
 
LVL 6

Accepted Solution

by:
javaexperto earned 500 total points
ID: 22723409
You must loop your array and then extract the id of the objects and compare with your id. If the id match then you add the object to the new array that you'll return if all this is inside a method.
//here you create the list that will contains the match objects

ArrayList<MyObject> myList = new ArrayList<MyObject>();

for (int i = 0; i < myArray.length; i++) { //iterate

     //here you extract the objects in the 0, 1, 2.... position

     MyObject mo = myArray[i]; 

     //here you compare the id in the objects with 3

     //remember you should have the method getId in you MyObject class

     if(mo.getId() == 3) {

           //here you add the objects to a list, I chose list because

           //it hasn't a fixed size remember you dont know how many

           //objects you'll add.

           myList.add(mo);

     }

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
where is session ID cache stored 1 46
groovy example issue 10 89
Java array passed to SQL where clause 23 62
Arrays.asList  VS  ArrayList 4 44
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now