?
Solved

Java - find item in array

Posted on 2008-10-01
7
Medium Priority
?
2,469 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

764 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