Solved

Java - find item in array

Posted on 2008-10-01
7
2,467 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
swing controls 2 34
jsp error 6 66
netbeans can not run self signed app 8 34
Read CLOB data from Oracle using JAVA 3 33
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

740 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