मैंने निर्धारित किया है कि एक जावा ArrayList.addएक जावास्क्रिप्ट के समान हैArray.push
मैं ArrayListनिम्नलिखित के समान कार्य खोजने पर अटका हुआ हूं
Array.popArray.shiftArray.unshiftमैं झुक रहा हूँArrayList.remove[At]
जवाबों:
ArrayListअपने नामकरण मानकों में अद्वितीय है। यहां ये समतुल्यताएं हैं:
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
ध्यान दें कि एक तत्व unshiftको नहीं हटाता है, बल्कि सूची में एक जोड़ता है । यह भी ध्यान दें कि कोने-मामले के व्यवहार जावा और जेएस के बीच भिन्न होने की संभावना है, क्योंकि वे प्रत्येक के अपने मानक हैं।
.push?
Array.push -> ArrayList.add, और विशेष रूप के बारे में पूछा pop, shiftऔर unshift। इसे फिर से पढ़ना, मैं .pushएक ही समय में और अधिक स्पष्टीकरण जोड़ने और जोड़ने जा रहा हूं ।
मैं कुछ समय पहले इस समस्या का सामना कर रहा था और मैंने पाया java.util.LinkedListकि यह मेरे मामले के लिए सबसे अच्छा है। इसकी कई विधियाँ हैं, अलग-अलग प्रार्थनाओं के साथ, लेकिन वे वही कर रहे हैं जिसकी आवश्यकता है:
push() -> LinkedList.addLast(); // Or just LinkedList.add();
pop() -> LinkedList.pollLast();
shift() -> LinkedList.pollFirst();
unshift() -> LinkedList.addFirst();
LinkeListतरीकों, जिस पर बहुत अक्षम हो जाएगा कहते हैं ArrayList करने के लिए Listइंटरफ़ेस, यह क्या मुझे उलझन में था। इस तरीके से आते हैं Dequeऔर Queueइंटरफेस जो इसे लागू ArrayListकरता है , लेकिन नहीं करता है।
शायद आप एक नज़र java.util.Stackवर्ग लेना चाहते हैं । इसमें पुश, पॉप विधियाँ हैं। और कार्यान्वित सूची इंटरफ़ेस।
शिफ्ट / अनशिफ्ट के लिए, आप @ जॉन के उत्तर का संदर्भ ले सकते हैं।
हालाँकि, ArrayList के बारे में कुछ आप परवाह करना चाहते हैं, arrayList सिंक्रनाइज़ नहीं है । लेकिन स्टैक है। (वेक्टर का उप-वर्ग)। यदि आपके पास थ्रेड-सुरक्षित आवश्यकता है, तो स्टैक ArrayList से बेहतर हो सकता है।
जॉन ने शानदार जवाब दिया ।
मैं हालांकि आलसी हूं और मुझे टाइपिंग से नफरत है, इसलिए मैंने उन सभी अन्य लोगों के लिए एक सरल कट और पेस्ट उदाहरण बनाया जो मेरे जैसे हैं। का आनंद लें!
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]
}
}
अंडरस्कोर-जावा लाइब्रेरी में विधियां पुश (मान), पॉप (), शिफ्ट () और अनशिफ्ट (मान) शामिल हैं।
कोड उदाहरण:
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"]