In Java an array has a fixed size (after initialisation), meaning that you can't add or remove items from an array.
int[] i = new int[10];
The above snippet mean that the array of integers has a length of 10. It's not possible add an eleventh integer, without re-assign the reference to a new array, like the following:
int[] i = new int[11];
In Java the package java.util contains all kinds of data structures that can handle adding and removing items from array-like collections. The classic data structure Stack has methods for push and pop.
Answer from Kennet on Stack OverflowIn Java an array has a fixed size (after initialisation), meaning that you can't add or remove items from an array.
int[] i = new int[10];
The above snippet mean that the array of integers has a length of 10. It's not possible add an eleventh integer, without re-assign the reference to a new array, like the following:
int[] i = new int[11];
In Java the package java.util contains all kinds of data structures that can handle adding and removing items from array-like collections. The classic data structure Stack has methods for push and pop.
For those who don't have time to refactor the code to replace arrays with Collections (for example ArrayList), there is an alternative. Unlike Collections, the length of an array cannot be changed, but the array can be replaced, like this:
array = push(array, item);
The drawbacks are that
- the whole array has to be copied each time you push, and
- the original array
Objectis not changed, so you have to update the variable(s) as appropriate.
Here is the push method for String:
(You can create multiple push methods, one for String, one for int, etc)
private static String[] push(String[] array, String push) {
String[] longer = new String[array.length + 1];
for (int i = 0; i < array.length; i++)
longer[i] = array[i];
longer[array.length] = push;
return longer;
}
This alternative is more efficient, shorter & harder to read:
private static String[] push(String[] array, String push) {
String[] longer = new String[array.length + 1];
System.arraycopy(array, 0, longer, 0, array.length);
longer[array.length] = push;
return longer;
}
[JAVA] How Do I Use push() and pop() With an Array?
java howto ArrayList push, pop, shift, and unshift - Stack Overflow
Push an item onto the end of array in Java - Code Review Stack Exchange
[Java] I'm having trouble understanding what .push does.
Videos
I've seen that you can use the Stack utility to create a stack which allows the push(), pop(), peek(), and empty() but how can I use these with an array? I am more familiar with using JS and still getting hung up on what Java utilities are used for what.
ArrayList is unique in its naming standards. Here are the equivalencies:
Array.push -> ArrayList.add(Object o); // Append the list
Array.pop -> ArrayList.remove(int index); // Remove list[index]
Array.shift -> ArrayList.remove(0); // Remove first element
Array.unshift -> ArrayList.add(int index, Object o); // Prepend the list
Note that unshift does not remove an element, but instead adds one to the list. Also note that corner-case behaviors are likely to be different between Java and JS, since they each have their own standards.
I was facing with this problem some time ago and I found java.util.LinkedList is best for my case. It has several methods, with different namings, but they're doing what is needed:
push() -> LinkedList.addLast(); // Or just LinkedList.add();
pop() -> LinkedList.pollLast();
shift() -> LinkedList.pollFirst();
unshift() -> LinkedList.addFirst();
Is this really needed?
int[] newArray = (int[]) Array.newInstance(oldArray.getClass().getComponentType(), len + 1);
I would do something like:
public static int[] arrayIntPush(int item, int[] oldArray) {
int len = oldArray.length;
int[] newArray = new int[len+1];
System.arraycopy(oldArray, 0, newArray, 0, len);
newArray[len] = item;
return newArray;
}
So for an array of length 10 you make a new array or length 11, copy all existing data into it and then assign the last index to the item.
Before Java 8, an even more simplified way is to use Arrays.copyOf(int[], int):
private static int[] push(int[] array, int value) {
int[] result = Arrays.copyOf(array, array.length + 1);
result[array.length] = value;
return result;
}
Java 8's IntStream gives you a way to have only a single return statement:
private static int[] push(int[] array, int value) {
return IntStream.concat(Arrays.stream(array), IntStream.of(value)).toArray();
}
Small tip: Since you are pushing a new value to the end of the array, you may want to consider reordering the method parameters so that the incoming value is indeed to the right of the array.
I have been learning Java for a few days now through Codecademy and have been writing down most of things I've learnt. Now I have just came across .push and I am trying to figure out what it does. Codecademy provides an answer but it still makes me lost.
https://gist.github.com/anonymous/5237420
The link on top is the link to the code. When I run it, it gives me the number 33 and Codecademy tells me it's correct! I'm just completely clueless on the code on the 12th line, can someone help me out?