0
public void setData(double[] d) {
    if (d == null) {
        data = new double[0];
    } else {
        data = new double[d.length];
        for (int i = 0; i < d.length; i++)
            data[i] = d[i];
    }
}

this method in my code is used to set the data of an array. I am also required to write a method called reset() that changes a given array to have a null value. Also, we are practicing overloading in this lab. There are four versions of setData() (double, int, float, long). Since a double array is used internally by the Stat class to store the values, do I only have to make one reset() method of type double?(I think I only need one...) Finally, please give me some hints as to going about this reset business because everything I have tried has failed miserably and usually consists of statements such as "setData(double[] null)" which return errors.

6
  • You can't unless it is an instance variable. If an array is passed as parameter, you can change the array elements, but not the array itself. Commented Nov 9, 2013 at 23:43
  • Array variable "d" is a reference. If you want to change the value of that reference to "null" (as seen outside of your method), then "setData()" must be a FUNCTION (not a "void" procedure). Commented Nov 9, 2013 at 23:44
  • I think, setData(null) already 'resets' the data array. (It does not set it to null but to an empty array - that should do I think). Commented Nov 9, 2013 at 23:45
  • @isnot2bad "that should do": maybe, maybe not. If you have an array variable (which is a reference), setting it to null and setting it to an array of length 0 do not do the same thing. Whether either is acceptable depends on the rest of the project, or on the teacher's instructions--here, if they used the words "null value", I think they mean set the reference to null. Commented Nov 9, 2013 at 23:58
  • @ajb you're right, of course it's not the same. But in that case, it might be sufficient to just empty it. (This is just an assumption, maybe the author of the question can clarify this). Commented Nov 10, 2013 at 0:02

3 Answers 3

1

Everything in java is pass by value; even references are passed by value. So by passing an array through a method, you can change the contents of the array, but you cannot change what the array points to. Now, if you are inside a class and happen to pass an instance member that you already have access to by virtue of being in the class, you will be able to set the array to null.

If you always want to be able to change what an array points to, then simply have a function which returns an array (instead of being void), and assign that returned value to the array of interest.

Sign up to request clarification or add additional context in comments.

Comments

0

Because java is pass by value, you can't reassign a variable passed as a parameter to a method, and expect to see that change reflected outside.

What you can do, is put the array in some sort of wrapper class like this:

class ArrayReference<T> {
  T[] array; // T would be either Double, or Long, or Integer, or whatever
}

and then:

void setData(ArrayReference<Double> myReference) {
  myReference.array = null;
}

Comments

0

I'm not sure if I understood your question, but is it that what you want?

public class Stat {
    private double[] data;

    public void reset() {
        data = null;
    }

    public void setData(double[] d) {
        data = (d == null) ? new double[0] : Arrays.copyOf(d, d.length);
    }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.