[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Java - find item in array

Posted on 2008-10-01
7
Medium Priority
?
2,472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 60

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 60

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 60

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

656 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