Hey guys I hate to bother you with another question so soon, but I've set up a 2d ordered array, and while my delete and search methods work okay, my display/insertion methods have to be missing something, or perhaps I've used too much information. Don't get me wrong, the 2d array in my driver is in ascending order like it should be, but the elements are not in the right coordinates. The only one that is placed right is 0,0. Everything else just shows up in 1,1 or 2,2, or 3,3, or 4,4, instead of where they are supposed to go. Any hints would be greatly appreciated.
Code:
class ArrayBinary
{
private int[][] a;
private int nElems;
public ArrayBinary (int x, int y)
{
a = new int[x][y];
nElems = 0;
}
public int size()
{
return nElems;
}
public int find(int searchKey)
{
int lowerBound = 0;
int upperBound = nElems-1;
int middle;
while(true)
{
middle = (lowerBound + upperBound) / 2;
if(a[middle][middle] == searchKey)
return middle; //found it
else if (lowerBound > upperBound)
return nElems;
else
{
if (a[middle][middle] < searchKey)
lowerBound = middle + 1;
else
upperBound = middle - 1;
} // ends find
}
public void insert (int x, int y, int value)
{
int i;
for (i =0; i < nElems; i++)
if(a[i][i] > value)
break;
for (int j = nElems; j > i; j--)
a[j][j] = a[j-1][j-1];
a[i][i] = value;
nElems++;
}
public boolean delete(int value)
{
int i = find(value);
if(i==nElems)
return false;
else
{
for(int j = i; j < nElems; j++)
a[j][j] = a[j+1][j+1];
nElems--;
return true;
}
} // end delete
public void display()
{
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println("");
}
}
}
Comment