Skip to main content
Tweeted twitter.com/StackCodeReview/status/673829037119262720
added 308 characters in body
Source Link

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);
}

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;
}

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);
}
edited tags
Link
TheCoffeeCup
  • 9.4k
  • 4
  • 36
  • 96
Source Link

Add to array method

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;
}