Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Linear Search / Index Location of Strings

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Linear Search / Index Location of Strings

    I have an array and in this array it contains strings like so:

    Contact[] guys = new Contact[8];
     
    guys[0] = new Contact ("Rob", "Smith", "610-555-7384");

    This is just an example of course. If I created a linear search how would I output the index location of this particular element (I would test for "Rob", "Smith" and output the index location)? I think the linear search below is what I would need to use but I don't know how to implement the output of the index location in the main method. Any help?

    public static int linear(Contact[] list, String target) 
        {
    	int index; 
    	for (index=0; index < list.length; index++)
    	    if ( target.equals(list[index]) ) 
                   return index;
    	    return -1;
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,039
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Linear Search / Index Location of Strings

    this array it contains strings like so:
    What you show is NOT an array of Strings. Its an array of Contact class objects.

    how would I output the index location of this particular element
    The Contact class could have a method that could take an arg(s) and say if that instance of the class matches the arg(s). Then use a loop as you have and ask each element in the array by calling the method if it matches the target.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Linear Search / Index Location of Strings

    Quote Originally Posted by Norm View Post
    What you show is NOT an array of Strings. Its an array of Contact class objects.


    The Contact class could have a method that could take an arg(s) and say if that instance of the class matches the arg(s). Then use a loop as you have and ask each element in the array by calling the method if it matches the target.
    Ya lost me...lol...so the loop method should work for my application...I don't know how I would go about implementing that.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,039
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Linear Search / Index Location of Strings

    Do you know how to add a method to the Contact class? What do you want to compare or test that is in the Contact object against the value of the variable: target?
    Return true if its a match and false if not.

    With that method to test the contents of an instance of the Contact class to see if its contents matches the target variable, you could write a loop very similar to the one you have now that uses the new method to see if the Contact object from the list matches the value of target.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Linear Search / Index Location of Strings

    Quote Originally Posted by Norm View Post
    Do you know how to add a method to the Contact class? What do you want to compare or test that is in the Contact object against the value of the variable: target?
    Return true if its a match and false if not.

    With that method to test the contents of an instance of the Contact class to see if its contents matches the target variable, you could write a loop very similar to the one you have now that uses the new method to see if the Contact object from the list matches the value of target.
    public class Searching
    {
      public static Comparable linearSearch (Comparable[] list, Comparable target)
      {
          int index = 0;
          boolean found = false;
     
          while (!found && index < list.length)
          {
             if (list[index].equals(target))
                found = true;
             else
                index++;
          }
     
          if (found)
             return list[index];
          else
             return null;

    I think this would work...but how can I output "Index is " the index (as an integer) in the main method?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,039
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Linear Search / Index Location of Strings

    how can I output "Index is " the index (as an integer)
    The method in post #1 returns the value of the index where the matching Contact object was found.If you called it like this:
      int theIndex = linear(guys, "The name to match");
    Then you could use the value in theIndex in the message you want to display.
    The contents of the linear() method would have to be changed to use the new method in the Contact class that was discussed earlier.


    I have no idea what the code in post#5 is supposed to do.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Linear Search / Index Location of Strings

    I understand what you're saying...I used that call...I kept my loop...I know I have to change the type because String target wont work with an integer (threw an error) I don't know how I can look for this object in guys...and return the integer index. I am confused. How do you go about finding an object and returning its index

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,039
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Linear Search / Index Location of Strings

    How do you go about finding an object and returning its index
    The linear method in post #1 is an example of code that searches through the elements in an arrray of objects and returns the index to an element in the array. The code that tests for the match needs to be changed to use a different method to detect the match.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Code for Linear & Binary Search using ArrayLists
    By JavaBeginner12 in forum Java Theory & Questions
    Replies: 2
    Last Post: December 11th, 2012, 08:22 AM
  2. [SOLVED] Linear array search(without Arraylist)
    By Usoda in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 18th, 2011, 12:12 AM
  3. Search for number in array and return index
    By Kevinius in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2011, 12:00 AM
  4. [SOLVED] Help with array of Strings...Linear Search?
    By Stockholm Syndrome in forum Collections and Generics
    Replies: 4
    Last Post: March 22nd, 2011, 03:31 PM
  5. [SOLVED] Interpolation Search for Strings ??
    By george in forum Algorithms & Recursion
    Replies: 3
    Last Post: May 18th, 2010, 06:11 AM