java howto ArrayList push, pop, shift, and unshift

前端 未结 5 472
我在风中等你
我在风中等你 2020-12-12 17:19

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

I\'m stuck on finding ArrayList functions sim

相关标签:
5条回答
  • 2020-12-12 17:19

    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). If you have thread-safe requirement, Stack may be better than ArrayList.

    0 讨论(0)
  • 2020-12-12 17:34

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

    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"]
    
    0 讨论(0)
  • 2020-12-12 17:39

    I was facing with this problem some time ago and I found java.util.LinkedList is best for my case. It has several methods, with different namings, but they're doing what is needed:

    push()    -> LinkedList.addLast(); // Or just LinkedList.add();
    pop()     -> LinkedList.pollLast();
    shift()   -> LinkedList.pollFirst();
    unshift() -> LinkedList.addFirst();
    
    0 讨论(0)
  • 2020-12-12 17:39

    Great Answer by Jon.

    I'm lazy though and I hate typing, so I created a simple cut and paste example for all the other people who are like me. Enjoy!

    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]
    
            // remove() -> pop(): Remove an item from the end of an array
            animals.remove(animals.size() - 1);
            System.out.println(animals); // [Lion, Tiger, Cat, Dog]
    
            // add(0,"xyz") -> unshift(): Add items to the beginning of an array
            animals.add(0, "Penguin");
            System.out.println(animals); // [Penguin, Lion, Tiger, Cat, Dog]
    
            // remove(0) -> shift(): Remove an item from the beginning of an array
            animals.remove(0);
            System.out.println(animals); // [Lion, Tiger, Cat, Dog]
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-12 17:41

    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.

    0 讨论(0)
提交回复
热议问题