2

I'm trying to remove the smallest value in an array. I've iterated through the array to find the smallest value but when I try to remove it I get this:

    SportStacker.java:111: error: cannot find symbol
            times.remove(slowestTime);
                 ^
    symbol:   method remove(double)
    location: variable times of type double[]
    1 error

Which I understand that I'm telling it to remove a double[] when I just want to remove a double at a given index. How do I say I want to remove the double at the index which contains the smallest value? This is my code for my method.

     public double removeSlowestTime() {

     double slowestTime;

     for (int i = 0; i < times.length; i++) {
        if (times[i] == 0) {
           continue;
        }
        else {
           if (slowestTime < times[i]) {
              slowestTime = times[i];
            times.remove(slowestTime);
           }
        }
     }
     return slowestTime;
0

5 Answers 5

2

array doesn't have remove() method, you may use ArrayList or set that value to some default value, like

times[someIndex] = null; // put any valid default value based on your array
2

The way you are trying to remove the element doesn't seem right. Try

times = ArrayUtils.removeElement(times, slowestTime);

If you want to do a lot of operations like this, a Collection or an ArrayList might be a better way for you to do it.

0

You cannot call remove on an array because it doesn't exist as a method. There is no simple way to remove an element from an array; I recommend using an implementation of java.util.List instead, which provides functionality such as remove.

0

You cannot remove an element from a double array using:

times.remove(slowestTime);

Arrays are of fixed size and the number of elements cannot be altered once created.

You could build a new double array. You could save the position of the slowest time in the array:

slowestIndex = i;
...

and create your new array on completion of the loop:

   double[] newTimes = new double[times.length - 1];
    for (int i=0, j=0; i < times.length - 1; i++) {
       if (i != slowestIndex) {
          newTimes[j++] = newTimes[i];
       }
    }
1
  • shouldn't the j++ be in newTimes[j++] assignment (not in the loop)?
    – guido
    Oct 16, 2012 at 1:08
0

As has been mentioned elsewhere in this answer, unlike in languages like JavaScript and Python, arrays in Java have a fixed number of elements defined when the array is defined.

int[] foo = new int[10]; // this array object will always have 10 elements
foo[0] = 42;
foo = new int[20];       // now foo refers to a different array
                         // object with 20 elements
return foo[0];           // so this will return the default int value, 0, not 42

If you are looking for a similar type of object to an array, use an ArrayList:

ArrayList<Integer> foo = new ArrayList<Integer>(); // this has zero elements
foo.add(42);                                      // now it has one
foo.add(8675309);                                 // now it has two
foo.remove(0);                                    // now you've removed the first
return foo.get(0);       // returns 8675309, because everything shifted over by one

But if you need to frequently remove the lowest item from the list, and you have a lot of items and performance is important, your best bet is a PriorityQueue. This is a data type that always keeps things in order from least to greatest. That means it takes a little longer to insert an item (O(log n), or proportional to the logarithm of the number of items in the list) but is fast to remove the least item (O(1), or constant no matter how long the list is). In comparison, when using a list, it will take a very short time to add an item (O(1)) but you have to inspect every item when searching for the lowest (O(n)).

PriorityQueue<Integer> foo = new PriorityQueue<Integer>();
foo.add(300);
foo.add(100);
foo.add(200);
int a = foo.remove();  // 100
int b = foo.remove();  // 200
int c = foo.remove();  // 300

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.