user3055141
user3055141

Reputation: 109

Java methods with arrays

I am given a question: Write a method, double[] threshold(double[] a, double x), that takes an array, a, of doubles and a double, x, and returns an array containing only the values of a that are greater than or equal to x. I know my code is wrong but can't seem to fix it. Please help!

public static double[] threshold(double[] a, double x){
    double[] ell;
    //ell = new double[];
    for (int i = 0 ; i < a.length; i++){
        if (a[i] >= x) {
            ell.add(a[i]);
        }
    }
    System.out.println(ell);
    return ell;   
}

Upvotes: 0

Views: 166

Answers (5)

emecas
emecas

Reputation: 1586

Reason:

You cannot use the add method for an array.

Solutions:

You have two ways so solve the problem, using a primitive array approach or an objects approach

Option 1. Using Primitive Array Approach:

As you had tried is this approach, but you have forgot to instance the array before to use it, once it is instanced with a specific number of elements, then you have to use the operator [] to access the elements in the array.

So, you have to use something like:

double[] ell = new double[<numElements>];

Where numElement should be at least bigger as a's length parameter to ensure all elements get into the ell array, if in case all are bigger than x parameter, this avoids an IndexOutOfBoundsException.

Then you have to access every single element using the notation [i] instead than add() method which only works for the objects approach.

ell[i]

Option 2. Using Objects Approach:

You should use a collection object that implements the add() method in order to add the elements that you want to filter. Once you loop for classification is done, you can get the content as an array and return it. You can replace:

double[] ell;

By a collection type object that implements an add() method, as: Vector or ArrayList, that are in the class hierarchy for AbstractList, of course you can use another hight level collections but these two are the most simple, then using Vector class you can code:

Vector ell = new Vector();

Then, at the end of the loop, you can use the method copyInto of Vector class to obtain the elements in the vector as an array of primitive data, something like:

double[] result;
ell.copyInto(result);

Upvotes: 0

Asqan
Asqan

Reputation: 4489

The size of arrays in the structure of [] has to be known before you initialize your array. In the case that is unknown you can use objects like LIST, ARRAY or VECTOR.

But in this case, it is necessary firstly to measure the size of array. Then with a second for-lus you can find your desired indices and set it on the new array. Although it could be improved, this way has to be taken.

And yes, this would be problematic with long arrays. So, it would be better if you use LIST or VECTOR.

Upvotes: 0

Alireza Fattahi
Alireza Fattahi

Reputation: 45551

Define ell as

List<Double> ell = new ArrayList<Double>();

The double[] ell is an array and you do not have ell.add. However, if you use ArrayList, you should also return it. Alternatively, if you want to use and return an array, you need to not use ell.add().

Upvotes: 2

musical_coder
musical_coder

Reputation: 3896

You're actually pretty close. First, even though this isn't super efficient, I'd loop through a and count how many numbers are greater then or equal to x. Then call ell = new double[sizeOfArray]. Finally, loop over a again, this time adding the elements you want. To add to an array, you call ell[index] = number.

Upvotes: 1

NPE
NPE

Reputation: 500673

You are not allocating memory for ell (and arrays don't have an add method).

Since you don't know in advance how big ell should be, you have several options:

  1. Allocate ell to be the same size as a, and trim it before returning.
  2. Use an ArrayList and convert it to an array at the end.

Upvotes: 0

Related Questions