I am trying to add, remove and reference items from an array I create in my main java file, but I am having trouble figuring out the correct syntax. In actionscript they have push() and pop() for adding and removing items in an array, is there an equivalent in android?
What are push() and pop() methods in JavaScript. push() is used to add an element/item to the end of an array. The pop() function is used to delete the last element/item of the array. Let's try to add and remove elements from an array using push() and pop() methods to understand these methods better.
Adding Array into the Array using push()The push() function allows us to push an array into an array. We can add an array into an array, just like adding an element into the Array.
JavaScript Array pop()The pop() method removes (pops) the last element of an array. The pop() method changes the original array. The pop() method returns the removed element.
In Java an array has a fixed size (after initialisation), meaning that you can't add or remove items from an array.
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.
Use Array list http://developer.android.com/reference/java/util/ArrayList.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With