Hey guys I hate to bother you my first 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.
class ArrayBinary
{
private int[][] a; // ref to array a
private int nElems; // number of data items
public ArrayBinary (int x, int y)
{
a = new int[x][y]; // creates array
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; // can't find it
else
{
if (a[middle][middle] < searchKey)
lowerBound = middle + 1; // its in upper half
else
upperBound = middle - 1; // its in lower half
}
} // ends find
}
public void insert (int x, int y, int value) // puts element into array
{
int i;
for (i =0; i < nElems; i++) // finds where it goes
if(a[i] > value)
break;
for (int j = nElems; j > i; j--)
a[j][j] = a[j-1][j-1]; // moves bigger ones up
a[i][i] = value; // inser it
nElems++; // increment size
}
public boolean delete(int value)
{
int i = find(value);
if(i==nElems)
return false; // can't find it
else
{
for(int j = i; j < nElems; j++)
a[j][j] = a[j+1][j+1]; // found it
nElems--; // decrement size
return true;
}
} // end delete
public void display() // displays array contents
{
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("");
}
}
}
Edited by: Leilong on Sep 4, 2008 8:11 AM