Solved

Java - find item in array

Posted on 2008-10-01
7
2,457 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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.

758 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

21 Experts available now in Live!

Get 1:1 Help Now