Java howto ArrayList push, pop, shift and unshift

I determined that Java ArrayList.add is similar to JavaScript Array.push

I am stuck looking for ArrayList functions similar to the following

  • Array.pop
  • Array.shift
  • Array.unshift I lean towards ArrayList.remove[At]

The last one is the most important for what I'm currently working on (Android). Thanks in advance!

+61
java android arraylist
Dec 09 '11 at 10:45
source share
5 answers

ArrayList is unique in its naming standards. The following are equivalences:

 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 

Please note that unshift does not delete the item, but adds it to the list. Also note that the behavior of the code in root mode is likely to be different from Java and JS, since each of them has its own standards.

+109
Dec 09 '11 at 10:50
source share

I ran into this problem a while ago and I found java.util.LinkedList for my case. It has several methods with different names, but they do what is needed:

 push() -> LinkedList.addLast(); // Or just LinkedList.add(); pop() -> LinkedList.pollLast(); shift() -> LinkedList.pollFirst(); unshift() -> LinkedList.addFirst(); 
+21
Apr 23 '13 at 7:21
source share

Perhaps you want to look at the java.util.Stack class. It has push, pop methods. and implemented List interface.

for shift / unshift, you can reference the @Jon answer.

however, something from the ArrayList that you might need, arrayList does not sync. but stack. (subclass of Vector class). If you have a thread safe requirement, Stack might be better than ArrayList.

+14
Dec 09 '11 at 23:19
source share

The subtree-java library contains push (values), pop (), shift (), and unshift (values) methods.

Code example:

 import com.github.underscore.U: List<String> strings = Arrays.asList("one", "two", " three"); List<String> newStrings = U.push(strings, "four", "five"); // ["one", " two", "three", " four", "five"] String newPopString = U.pop(strings).fst(); // " three" String newShiftString = U.shift(strings).fst(); // "one" List<String> newUnshiftStrings = U.unshift(strings, "four", "five"); // ["four", " five", "one", " two", "three"] 
+1
Aug 01 '16 at 4:50
source share

Great answer from John .

I am lazy, although I do not like typing, so I created a simple cut and paste example for all other people who are like me. Enjoy it!

 import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<String> animals = new ArrayList<>(); animals.add("Lion"); animals.add("Tiger"); animals.add("Cat"); animals.add("Dog"); System.out.println(animals); // [Lion, Tiger, Cat, Dog] // add() -> push(): Add items to the end of an array animals.add("Elephant"); System.out.println(animals); // [Lion, Tiger, Cat, Dog, Elephant] // pop(): Remove an item from the end of an array animals.remove(animals.size() - 1); System.out.println(animals); // [Lion, Tiger, Cat, Dog] // unshift(): Add items to the beginning of an array animals.add(0, "Penguin"); System.out.println(animals); // [Penguin, Lion, Tiger, Cat, Dog] // shift(): Remove an item from the beginning of an array animals.remove(0); System.out.println(animals); // [Lion, Tiger, Cat, Dog] } } 
0
Dec 04 '18 at 11:10
source share



All Articles