Searching An ArrayList in Java

Posted on 2005-05-15
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 ?


- J
Question by:Jase_x
    LVL 3

    Accepted Solution


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

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

    Author Comment

    Thanks mate, that will work a treat !
    LVL 86

    Expert Comment

    if you call

    boolean present = arrayList.contains(name);

    it won't alter the current order of the List
    LVL 86

    Expert Comment

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

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

    Expert Comment

    >>That's not actually how binarySearch works - see api docs

    The following statements taken from api

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

    Expert Comment

    Exactly ;-)

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    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…
    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…
    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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    755 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

    18 Experts available now in Live!

    Get 1:1 Help Now