Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

equivalent to push() or pop() for arrays?

Tags:

java

android

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?

like image 306
clayton33 Avatar asked Dec 27 '10 10:12

clayton33


People also ask

Can you push and pop an array?

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.

Can you push to an array?

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.

How do you pop an 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.

Can you push to an array in Java?

In Java an array has a fixed size (after initialisation), meaning that you can't add or remove items from an array.


2 Answers

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.

like image 100
Kennet Avatar answered Sep 19 '22 13:09

Kennet


Use Array list http://developer.android.com/reference/java/util/ArrayList.html

like image 35
Eby Avatar answered Sep 23 '22 13:09

Eby