Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java howto ArrayList push, pop, shift, and unshift

Tags:

java

arraylist

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

I'm stuck on finding ArrayList functions similar to the following

  • Array.pop
  • Array.shift
  • Array.unshift I'm leaning toward ArrayList.remove[At]
like image 221
Jacksonkr Avatar asked Dec 09 '11 22:12

Jacksonkr


People also ask

How do you push data into an ArrayList?

The java. util. ArrayList. add(int index, E elemen) method inserts the specified element E at the specified position in this list.It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).

What is Pop Push shift and Unshift?

pop() removes the last element of an array. push() adds an element to the end of an array. shift() removes the first element. unshift() adds an element to the beginning of the array.

How do you push a list in Java?

LinkedList push() Method in Java LinkedList. push() method is used to push an element at the starting(top) of the stack represented by LinkedList. This is similar to the addFirst() method of LinkedList and simply inserts the element at the first position or top of the linked list.

What are the JavaScript array methods push pop and shift?

In this post, we'll learn about the JavaScript Array methods push, pop, shift, and unshift. These methods are used when you want to add or remove elements from the start or the end of an array. Use the push method to add an element to the end of an array. push returns the new length of the array.

Is it possible to shift/unshift a list in Java?

maybe you want to take a look java.util.Stack class. it has push, pop methods. and implemented List interface. for shift/unshift, you can reference @Jon's answer. however, something of ArrayList you may want to care about , arrayList is not synchronized. but Stack is. (sub-class of Vector).

What is unshift in JavaScript?

unshift returns the new length of the array. We've taken a look at the four most common array methods for manipulating the start and end of arrays, push, pop, shift, and unshift. Hopefully you learned something useful and enjoyed reading this post!

Should I use stack or ArrayList for shift/unshift?

for shift/unshift, you can reference @Jon's answer. however, something of ArrayList you may want to care about , arrayList is not synchronized. but Stack is. (sub-class of Vector). If you have thread-safe requirement, Stack may be better than ArrayList.


1 Answers

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.

like image 139
Jon Egeland Avatar answered Sep 20 '22 19:09

Jon Egeland