?
Solved

Searching An ArrayList in Java

Posted on 2005-05-15
6
Medium Priority
?
226 Views
Last Modified: 2010-03-31
Hello there,

I was wondering if someone could help me with something to do with searching ArrayLists.

I have an arraylist called wrkStnNames, which contains a number of computer names.  I am now in the process of creating a constructor of the class wrkStn which allows one string to be passed to it ,

This constructor has an if statement which checks to see if the string being passed to that constructor matches the string in the array list.  If so it will continue and update that array list .. if not it ends.

I am having problems working out how to search the arraylist for the presence of the string passed to the constructor ... any ideas how I could do this ?

Thanks,

- J
0
Comment
Question by:Jase_x
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
neonlines earned 200 total points
ID: 14006009

ArrayList list = new ArrayList(); //ur ArrayList Object
Collections.sort(list);
int pos = Collections.binarySearch(list, "constructor parameter"); // list must be sorted first

if pos is -1, the given string is not in the Arraylist
0
 

Author Comment

by:Jase_x
ID: 14006029
Thanks mate, that will work a treat !
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14006031
if you call

boolean present = arrayList.contains(name);

it won't alter the current order of the List
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 14006047
>>if pos is -1, the given string is not in the Arraylist

That's not actually how binarySearch works - see api docs
0
 
LVL 3

Expert Comment

by:neonlines
ID: 14008252
>>That's not actually how binarySearch works - see api docs

The following statements taken from api

Returns:
index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

-- Returns the current index or a negative number of the insertion point
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 14008505
Exactly ;-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

809 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