java howto ArrayList push, pop, shift, and unshift

Jacksonkr picture Jacksonkr · Dec 9, 2011 · Viewed 176.7k times · Source

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]

Answer

Jon Egeland picture Jon Egeland · Dec 9, 2011

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.