Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: 2D array search

  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 2D array search

    Hi all,

    Any help?

    Im not completely sure if this is correct, I am new to java and want to input kitten and get cat for example on this 2d array. I get the exception ArrayIndexOutOfBounds.

    Code:
    public class Farm
    import java.util.Arrays;
    {
    
    // Class variables
      	
     	public static String[][] animals =
    {
    {"kitten", "cat"},
    {"puppy", "dog"},
    {"lamb", "sheep"},
    {"chick", "bird"},
    {"calf", "cow"},
    };
    
    
    public String ChangefromAnimal1toAnimal2(String aString)
    {
    for (int r=0; r < Farm.animals.length; r++)
    {
    int index = Arrays.binarySearch(Farm.animals[r], aString);
    {
      	
     	for (int c=0; c < Farm.animals[r].length; c++)
    {
    System.out.println(Farm.animals[r][index]);
    }
    
    }
    
    }
    }
    
    }
    I think am sorted!

    Thanks,

    Random
    Last edited by Fou-Lu; 03-06-2011 at 07:43 PM. Reason: Sorted

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    BinarySearch returns -1 for an index on failure. If you search for "kitten" and hit the second array for "puppy" there is no resulting match. Work wise, this is easier using a single loop.
    PHP Code:
        public String ChangefromAnimal1toAnimal2(String aString)
        {
            
    String sResult "";
            for (
    int r 0Farm.animals.length && sResult.isEmpty(); r++)
            {
                if (
    animals[r][0].equals(aString))
                {
                    
    sResult animals[r][1];
                }
            }
            return 
    sResult;
        } 
    Also consider using a Hashtable to associate "kitten" with "cat" for easier lookups.

    Edit:
    BTW, you'd never get the OutOfBounds exception. There are two compile time errors here, one for the import statement, and one for not returning a String from your method.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by RandomRobbo View Post
    Hi all,

    Any help?

    Im not completely sure if this is correct, I am new to java and want to input kitten and get cat for example on this 2d array. I get the exception ArrayIndexOutOfBounds.


    I think am sorted!

    Thanks,

    Random
    I think you removed the content and this against forum rules. Put him back please.

    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •