0

So I want to Push a new item onto the end of the array (the numbers 1-9). I was told by a friend that the code I have written is correct, but when I run it on eclipse, nothing happens. What do I need to do? Should I just print the array under the main block? thanks.

public static void main(String[] args) {
    long[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
}

public static long[] push(long[] array, long item) {

    // Add one item to an array
    long cellsToAdd = 1;
    long[] array2 = new long[array.length + 1];

    // copy each item in array2 -> array3
    for (int i = 0; i < array.length; i++) {
        array[i] = array2[i];
    }

    array2[array2.length - 1] = item;

    System.out.println("Array: ");
    // Print the array to the console
    for (long p : array2) {
        System.out.println(p);
    }

    return array2;
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
shaytee
  • 5
  • 4
  • 11
    you're not calling the function – rfreytag Oct 14 '16 at 16:03
  • you should call `push` from the main method. – Linuslabo Oct 14 '16 at 16:04
  • 1
    Note also that Java arrays have fixed length. You *can't* push anything onto the end. The code you present instead creates a new, longer array with the desired contents. There's nothing inherently wrong with that, but it's important to appreciate the difference between that and what you asked. – John Bollinger Oct 14 '16 at 16:09
  • 1
    Possible duplicate of [equivalent to push() or pop() for arrays?](http://stackoverflow.com/questions/4537980/equivalent-to-push-or-pop-for-arrays) – Frederic Klein Oct 14 '16 at 16:24

3 Answers3

3

You can use System.arraycopy to create a new array and increase its size.

Since you are using the primitive long type, you would need to copy-paste this logic for each primitive (int, float, double, etc) if you want to support these types.

public static void main(String[] args) {
    long[] digs = { 0, 1, 2, 3, 5 }                       
    long[] digs2 = push(digs, 6);
    long[] digs3 = pushAll(digs2, new long[] { 7, 8, 9 });

    System.out.println(Arrays.toString(digs));   // [0, 1, 2, 3, 5]
    System.out.println(Arrays.toString(digs2));  // [0, 1, 2, 3, 5, 6]
    System.out.println(Arrays.toString(digs3));  // [0, 1, 2, 3, 5, 6, 7, 8, 9]

    // Generic Example
    Long[] genArr = push(new Long[] { 0L, 1L }, new Long(3L), Long.class);

    // or Long[] genArr = push(new Long[] { 0L, 1L }, new Long(3L));

    System.out.println(Arrays.toString(genArr)); // [0, 1, 3] 
}

Push

public static long[] push(long[] a, long b) {
    long[] result = new long[a.length + 1]; 
    System.arraycopy(a, 0, result, 0, a.length); 
    result[a.length] = b;
    return result;
} 

Push All

public static long[] pushAll(long[] a, long[] b) {
    long[] result = new long[a.length + b.length]; 
    System.arraycopy(a, 0, result, 0, a.length); 
    System.arraycopy(b, 0, result, a.length, b.length); 
    return result;
}

Generic Push

public static <E> E[] push(E[] a, E b, Class<E> classType) {
    @SuppressWarnings("unchecked")
    E[] result = (E[]) Array.newInstance(classType, a.length + 1);
    System.arraycopy(a, 0, result, 0, a.length); 
    result[a.length] = b;
    return result;
}

Optional

// Convenience, so that you don't have to pass in the class.
public static Long[] push(Long[] a, Long b) {
    return push(a, b, Long.class);
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
1

Aside from actually calling the function like others mentioned, your for loop copys from array2 to array, while it should be the other way around

Simon Eismann
  • 273
  • 1
  • 5
  • 17
0

Call the method

You defined a method but did not call it.

public static void main(String[] args) {
    long[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    array = MyClassName.push( array , 10L );
}

If you are merely practicing to learn, well enough. But know that this kind of functionality is already provided for you in the Java Collections framework.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154