Ustaliłem, że Java ArrayList.addjest podobna do JavaScriptArray.push
Utknąłem na szukaniu ArrayListfunkcji podobnych do poniższych
Array.popArray.shiftArray.unshiftPochylam się w kierunkuArrayList.remove[At]
Odpowiedzi:
ArrayListjest wyjątkowy pod względem standardów nazewnictwa. Oto równoważności:
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
Zauważ, że unshiftnie usuwa elementu, ale zamiast tego dodaje go do listy. Zwróć również uwagę, że zachowania przypadków narożnych mogą być różne w Javie i JS, ponieważ każdy z nich ma swoje własne standardy.
.push?
Array.push -> ArrayList.add, a specjalnie poprosił o pop, shifti unshift. Czytając to jeszcze raz, dodam więcej wyjaśnień i dodam .pushw tym samym czasie.
Miałem z tym problemem jakiś czas temu i stwierdziłem, że java.util.LinkedListjest najlepszy dla mojego przypadku. Ma kilka metod z różnymi nazwami, ale robią to, co jest potrzebne:
push() -> LinkedList.addLast(); // Or just LinkedList.add();
pop() -> LinkedList.pollLast();
shift() -> LinkedList.pollFirst();
unshift() -> LinkedList.addFirst();
LinkeListdodaje metody, które byłyby bardzo nieefektywne na ArrayList do Listinterfejsu, to było to, co mnie mylić. Te metody pochodzą z interfejsów Dequei Queue, które implementuje, ale ArrayListtak nie jest.
może chcesz wziąć udział w java.util.Stackklasie look . ma metody push, pop. i zaimplementowany interfejs List.
w przypadku zmiany / cofnięcia zmiany możesz odwołać się do odpowiedzi @ Jona.
jednak coś z ArrayList, o które możesz chcieć się troszczyć, arrayList nie jest zsynchronizowana. ale Stack jest. (podklasa Vector). Jeśli masz wymaganie dotyczące bezpieczeństwa wątków, Stack może być lepszy niż ArrayList.
Świetna odpowiedź Jon .
Jestem jednak leniwy i nienawidzę pisania, więc stworzyłem prosty przykład wycinania i wklejania dla wszystkich innych ludzi, którzy są tacy jak ja. Cieszyć się!
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]
}
}
Biblioteka Underscore-java zawiera metody push (wartości), pop (), shift () i unshift (wartości).
Przykład kodu:
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"]