Skip to content
Advertisement

java howto ArrayList push, pop, shift, and unshift

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]

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement