help with search program.

hi,

I am creating a simple search program for a 2-dimensional array. I am using the following boolean, and am not sure how to create the driver to check if a value is inside the array. Any help would be appreciated.

here is my code thus far:

publicclass search

{

int[][] list ={{1,4,55,88},

{7,15,61,91},

{14,89,90,99}};

publicstaticvoid main(String[] args)

{

}

publicboolean contains(int anEntry)

{

boolean found =false;

for (int row = 0; row < 3; row++)

for (int column = 0; column < 5; column++)

{

if (anEntry.equals(list[row][column]))

{

found =true;

}

}

return found;

}

}

[1824 byte] By [LuckY07a] at [2007-11-26 23:55:45]
# 1

What you have seems find, but some enhancements could be made.

public class search

{

int[][] list = {{1,4,55,88},

{7,15,61,91},

{14,89,90,99}};

public static void main(String[] args)

{

}

public boolean contains(int anEntry)

{

boolean found = false;

for (int row = 0; row < list.length; row++)

for (int column = 0; column < list[row].length; column++)

// use the .length instead

// This means it'll work with any

// size of array.

{

if (anEntry.equals(list[row][column]))

{

return true;

//This means the loop will end, and the method returns true

// Why keep looping if the value is found right?

}

}

return found;

}

}

lethalwirea at 2007-7-11 15:40:09 > top of Java-index,Java Essentials,Java Programming...
# 2
thanks for the help! I am confused as to how to use the BOOLEAN inside the main(). How should I call the boolean method to find a value for example the number 7?
LuckY07a at 2007-7-11 15:40:09 > top of Java-index,Java Essentials,Java Programming...
# 3

In that case, you'd have to pass another parameter inside of your boolean method header.

One that takes a 2-dimensional array.

Also, make your boolean header say

private static boolean contains(int key, int[][] arrayToSearch)

With this you'll have to change a few things inside of your method "contains"

but it should lead you in the right direction.

Now in main, you can use this method as follows:

if ( contains(3, list) ) //Here we call method contains with our 2 parameters

System.out.println("The list contained the value of 3"); //Print if found

else

System.out.println("The list didn't contain the value of 3"); //print if not found

lethalwirea at 2007-7-11 15:40:09 > top of Java-index,Java Essentials,Java Programming...
# 4

I made some adjustments, but am getting the following errors when trying to pass the array:

C:\MCS142\Search.java:11: non-static variable list cannot be referenced from a static context

if ( contains(15, list) ) //Here we call method contains with our 2 parameters

^

C:\MCS142\Search.java:11: non-static method contains(int,int[][]) cannot be referenced from a static context

if ( contains(15, list) ) //Here we call method contains with our 2 parameters

^

C:\MCS142\Search.java:24: int cannot be dereferenced

if (anEntry.equals(arrayToSearch[row][column]))

^

3 errors

Tool completed with exit code 1

here is my code:

import java.util.*;

public class Search

{

int[][] list = {{1,4,55,88},

{7,15,61,91},

{14,89,90,99}};

public static void main(String[] args)

{

if ( contains(15, list) ) //Here we call method contains with our 2 parameters

System.out.println("The list contained the value of 15"); //Print if found

else

System.out.println("The list didn't contain the value of 15"); //print if not found

}

public boolean contains(int anEntry, int[][] arrayToSearch)

{

boolean found = false;

for (int row = 0; row < 3; row++)

for (int column = 0; column < 5; column++)

{

if (anEntry.equals(arrayToSearch[row][column]))

{

found = true;

}

}

return found;

}

}

LuckY07a at 2007-7-11 15:40:09 > top of Java-index,Java Essentials,Java Programming...
# 5

I didn't know what to do so I did the following:

-declared the array 'list' as a STATIC variable.

-created a new object 'a' in main(); >> Search a = new Search();

-converted my INT to a STRING in my for loop so I could use 'equals'.

Does this seem like a logical solution? I have never had to declare an array as static before. Could someone look over my code and let me know if it's ok? tks.

//MCS_142

//Search_Program

/*Program that shows how to search a multidimensional array. The array is

declared as Static so that it can be used in the Static main().

**/

import java.util.*;

public class Search

{

private static int[][] list = {{1,4,55,88},//creates 2-dimensional array 'list'.

{7,15,61,91},

{14,89,90,99}};

public static void main(String[] args)

{

Search a = new Search();

if ( a.contains("99", list) ) //Here we call method contains with our 2 parameters

System.out.println("The list contained the value of 99"); //Print if found

else

System.out.println("The list didn't contain the value of 99"); //print if not found

}

public boolean contains(String anEntry, int[][] arrayToSearch)

{

boolean found = false;

for (int row = 0; row < list.length; row++)

for (int column = 0; column < list[row].length; column++)

{

int temp = arrayToSearch[row][column];//temp variable to hold the integer.

String temp2 = Integer.toString(temp); //convert the temp integer to a String.

if (temp2.equals(anEntry))//uses the String compare the value with our target.

{

found = true;

}

}

return found;

}

}

Message was edited by:

LuckY07

LuckY07a at 2007-7-11 15:40:09 > top of Java-index,Java Essentials,Java Programming...
# 6
Can someone check out the program I wrote above? For this type of search, involving a multidimensional array, would the big Oh notation be (0)n^2? -I know single array searches are just (0)n, but wasn't sure about 2-dimensional arrays.
LuckY07a at 2007-7-11 15:40:09 > top of Java-index,Java Essentials,Java Programming...