Reputation: 119
I have instantiated a 2D array of an editable number of rows and a set number of three columns.
It is randomly filled with 0's and 1's using the Random .nextInt(2) method.
After the array is filled, I want to be able to search the array and return the first occurrence of a 0.
How can I do this?
For example, if i had an array that looked something like this:
The first occurence would be at (0,3). I want to search the array horizontally and when it reaches the third column (the end), it will go to the next row.
Note: I originally tested the following section of code with a 2D array that was completely filled with 0's and when I manually inserted 1's in the array and then tried to search for the first occurence of a 0 it worked. However, the code doesn't work when the array is randomly filled..
public String findNextAvailable()
{
for (int i=0; i<seatlist.length; i++)
{
for (int j=0; j<seatlist[i].length; j++)
{
int k=0;
if (seatlist[0][0]==0)
{
nextavailable= seatchart[0][0];
break;
}
else
if(seatlist[k][j]==0)
{
nextavailable= seatchart[k][j];
break;
}
else
{ k++;
if(seatlist[k][j]==0)
{
nextavailable= seatchart[k][j];
break;
}
}
}
}
return nextavailable;
}
Thanks in advance!
Upvotes: 2
Views: 32594
Reputation: 66
for (int i = 0; i < seats.length; i++) {
for (int j = 0; j < seats[i].length; j++) {
if (seats[i][j] == 0) {
return "Next available seat at position: [" + i + "][" + j + "]";
}
}
}
return "No seat available";
Although you might want to create a seat object instead that is easier to work with:
public class Seat {
private int row;
private int column;
public Seat(int row, int column){
this.row = row;
this.column = column;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
}
and replace the returning of a string with:
return new Seat(i,j);
Upvotes: 3
Reputation: 359
You need to return the positions of the first encountered 0, so why are you breaking out of the if statement, the outer loop will still run!
Simply create an integer array:
int[] pos=new array[2];
Change the return type:
public int[] findNextAvailable(){
In each of the if statements change the contents so that it reads:
pos[0]=i;
pos[1]=j;
return pos;
The end result will look something like this:
public int[] findNextAvailable()
{
int[] pos=new array[2];
for (int i=0; i<seatlist.length; i++)
{
for (int j=0; j<seatlist[i].length; j++)
{
if (seatlist[i][j]==0)
{
pos[0]=i;
pos[1]=j;
return pos;
}
}
}
//none found so return minus one.
pos[0]=-1;
pos[1]=-1;
return pos;
}
Upvotes: 1
Reputation: 664
well when you break in the inner loop, you still execute again the outer loop and you wind up replacing what you think is your final result by the next run of the outer loop. rather than use break, just return right there.
Upvotes: 1
Reputation: 1
there are many different types of searches, some faster and some easier to do. Here's a program i made that has methods for all types of them. you will have to modify them a bit so it will search a 2d array but it shouldn't be too hard. ' package linear_search; import java.util.Arrays; import java.util.Scanner;
public class Linear_search {
int[] array = {10,12,42,7,22,1,3,4,5,9};
int ans;
int num;
int min;
int max;
void start(){
arraySort(array);
dump(array);
Scanner scan = new Scanner(System.in);
System.out.println("Enter a value to search:");
num = scan.nextInt();
ans = recursiveBinarySearch(array, 0, array.length-1);
if(ans == -1){
System.out.println("Your value was not found");
} else {
System.out.println("Your value was found at position " + ans);
}
}
void dump(int[] array){
for(int i = 0; i < array.length ; i++){
System.out.print(array[i] + " ");
}
System.out.println();
}
int linearsearch(int[] array){
for(int i = 0; i < array.length; i++){
if(array[i] == num){
return i;
}
}
return -1;
}
int binarysearch(int[] array){
min = 0;
max = array.length -1;
int mid = (min + max) / 2;
while(array[mid] != num){
if(num > array[mid]){
min = mid+1;
mid = (min + max) / 2;
}
if(num < array[mid]){
max = mid-1;
mid = (min + mid) / 2;
}
if(min == max && array[mid] != num){
return -1;
}
}
return mid;
}
int recursiveBinarySearch(int[] array, int min, int max){
int mid = (min + max) / 2;
if(array[mid] == num){
return mid;
}
if(min == max && array[mid] != num){
return -1;
}
if(num > array[mid]){
return recursiveBinarySearch(array, mid+1, max);
}
if(num < array[mid]){
return recursiveBinarySearch(array, min, mid-1);
}
return mid;
}
void arraySort(int[] a){
Arrays.sort(array);
}
public static void main(String[] args) {
Linear_search main = new Linear_search();
main.start();
}
}
' you will just have to remove the scanner and hard code in "0" for the default value you should search for.
Upvotes: 0