7
\$\begingroup\$

The below method is meant to work similar to the way an ArrayList would work with the add method: The given double is added to the end of the array. Obviously since you can't add to arrays, a new array is returned with the number added to the end.

I'm worried about using this in loops due to performance, because if I add something hundreds of times, I'll be creating hundreds of array objects. Is this bad? Will it cause issues?

public static double[] add(double[] array, double... toAdd)
{
    double[] newArray = new double[array.length + toAdd.length];
    System.arraycopy(array, 0, newArray, 0, array.length);
    System.arraycopy(toAdd, 0, newArray, array.length, toAdd.length);
    return newArray;
}

Also would this method have advantages/disadvantages of using an ArrayList to add, and then going through every element in the list and copying it to an array?

//list.add several things
//array[list.size]
//transfer the list of values to an array:
for(int i = 0 ; i<list.size();i++)
{
    array[i] = list.get(i);
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Hi! Welcome to Code Review. Good job on your first post! \$\endgroup\$ – TheCoffeeCup Dec 6 '15 at 21:58
5
\$\begingroup\$

Your code is neat, functional, and I really like that you keep everything in the primitive domain (i.e. you use double[] instead of List<Double>). When dealing with primitives in Java it is almost always faster, and more logical to keep the two domains separate - especially if/when the values are used in calculations.

So, your goal of appending additional double values to an array, and returning a new array with the combined contents, is good.

While using add seems logical (it's what a List calls it) I would recommend you use the name append because add on a double could have different meanings.

As for your implementation, it's correct, and I understand it just fine. In more recent Java versions, it's better to use the Arrays.copy* methods though, instead of the System.arraycopy routines. This is because the Arrays versions are more type safe, and have better in-code semantics. So, your code would be (though you cannot avoid the one System.arraycopy call):

public static double[] add(double[] array, double... toAdd) {
    double[] newArray = Arrays.copyOf(array, array.length + toAdd.length);
    System.arraycopy(toAdd, 0, newArray, array.length, toAdd.length);
    return newArray;
}

In Java8, the "nice" way to do this would be:

public static double[] add(double[] array, double... toAdd) {
    return DoubleStream.concat(DoubleStram.of(array), DoubleStream.of(toAdd)).toArray();
}

About your question for "looping" the values, and adding each of them... well, that will be clear in the logic, but will also be slower. Arrays.copyOf, and System.arrayCopy both use low-level code to make the memory transfers happen quickly, in bulk, and will outperform a higher-level loop.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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