2 Replies Latest reply on Oct 27, 2010 10:01 PM by YoungWinston

    Help to search for a string value and return the index in the arraylist

    808473
      Hello,

      I just start java programming for the last three weeks and I cannot find a solution for this problem. I have the following List with string and integer value as shown below:

      List<Empl> list= new ArrayList<Empl>();
                
           list.add(new Empl(1,"Jim", "Balu",88);
           list.add(new Empl(3,"Bob", "Howards",2);
           list.add(new Empl(2,"Chris", "Hup",8);
      .........

      I have no problem of sorting this arraylist either by firstname or lastname. However, I cannot think of a way to search for a firstname or lastname and returing the whole row. I do not want to use index since I am asking user to enter the name to search. Here is my code to search for an empl based on index.

      ........
      System.out.print("Please enter index to search or (q)uit: ");
                String ans = sc.next();
                System.out.println();
                int ians = Integer(ans);
                if (ans.equalsIgnoreCase("q"))
                {
                          choice = "n";
                }
                else
                System.out.println("index " + list.get(ians)); //this will print out the whole row based on the index
      ...........

      Since the user will never seen the contents of the arraylist, is there a way that I can search by string last/first name and will get the index location in int if the result is met?

      Please advice and thank you.
      Bob.
        • 1. Re: Help to search for a string value and return the index in the arraylist
          jschellSomeoneStoleMyAlias
          Depends on what you are learning and what the teacher is teaching (if a teacher.)

          The most basic way is to create a loop that gets each item, extracts the instance of Empl, compares the data in that to what you are looking for. In terms of an 'index' you would use a for rather than foreach to sequence through the collection.

          I suspect, without having looked at all, that there is probably some truely clever and convoluted way to do with via the api as well which probably involves creating at least one other class.
          • 2. Re: Help to search for a string value and return the index in the arraylist
            YoungWinston
            user11191663 wrote:
            Since the user will never seen the contents of the arraylist, is there a way that I can search by string last/first name and will get the index location in int if the result is met?
            Another possibility is to set up an ArrayList for each thing you want to search on and then every time you add an employee, add the piece of data you want as well, viz:
            Empl e = new Empl(1,"Jim", "Balu",88);
            list.add(e);
            firstNames.add(e.firstName());
            As long as you always add them in the same order, the indexes will match, so you could do something like
            ians = firstNames.indexOf(nameToFind);
            to return the index of the name, if it's there (if not, it will return -1).

            I wouldn't recommend this as standard practise, but it should be OK for the level you're at.

            NOTE: indexOf() returns the index of the FIRST matching item. You may want to think about what you want to do if there are more than 1.

            Winston