Hi, I have been stuck on this simple array java problem for the longest time and know I am missing a little detail. The code is to find the maximum value in the array and then delete that value. Here is the code that I wrote:
public void removeMax()
{
if (nElems > 0) { // checks if array is empty
long maxSoFar = a[0]; // remembers first value
for (int j=0; j < nElems; j++) // remembers any larger values
{
if (a[j] > maxSoFar)
maxSoFar = a[j];
for (int k = j; k<nElems; k++) // move higher ones down
a[k] = a[k+1];
nElems--; // decrement size
} // end if
} // end if
} // end removeMax
} // end class HigherArray
I think it might have to do with some of the brackets being misarranged. I have to stick with this simple array structure for it is a computer science assignment. Thanks for your input.
public void removeMax()
{
if (nElems > 0) { // checks if array is empty
long maxSoFar = a[0]; // remembers first value
for (int j=0; j < nElems; j++) // remembers any larger values
{
if (a[j] > maxSoFar)
maxSoFar = a[j];
for (int k = j; k<nElems; k++) // move higher ones down
a[k] = a[k+1];
nElems--; // decrement size
} // end if
} // end if
} // end removeMax
} // end class HigherArray
I think it might have to do with some of the brackets being misarranged. I have to stick with this simple array structure for it is a computer science assignment. Thanks for your input.