PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम List Interface Implementations: ArrayList vs LinkedList (ArrayList (डायनामिक एरे) बनाम LinkedList (डबली लिंक्ड)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: ArrayList and LinkedList are the two flagship non-synchronized implementations of java.util.List, differing fundamentally in memory layout and algorithmic complexity. ArrayList backs elements with a contiguous resizable array for O(1) random access, whereas LinkedList uses a doubly-linked node chain implementing both List and Deque for O(1) endpoint edits.
अध्याय सार: ArrayList और LinkedList, java.util.List के दो प्रमुख गैर-सिंक्रोनाइज़्ड इम्प्लीमेंटेशन हैं जिनकी मेमोरी संरचना और टाइम कॉम्प्लेक्सिटी भिन्न होती है। ArrayList O(1) रैंडम एक्सेस के लिए रिसाइज़ेबल एरे का उपयोग करता है, जबकि LinkedList List और Deque दोनों इंटरफ़ेस इम्प्लीमेंट करते हुए डबली लिंक्ड नोड्स का प्रयोग करता है।
List Interface Implementations: ArrayList vs LinkedList: Detailed Notes & Architecture (ArrayList (डायनामिक एरे) बनाम LinkedList (डबली लिंक्ड))
1. Internal Data Structures: Dynamic Array vs Doubly Linked List (आंतरिक डेटा संरचना: डायनामिक एरे बनाम डबली लिंक्ड लिस्ट)
[EN] ArrayList internally stores object references inside a contiguous Object[] array (transient Object[] elementData) that dynamically grows as elements are added. When constructed with the no-argument constructor in Java 8+, ArrayList starts with an empty shared array and allocates the default capacity of 10 only upon adding the first element (lazy allocation). By contrast, LinkedList maintains independent Node<E> objects scattered in heap memory, where each node holds three fields: the element reference (item), a backward pointer (prev), and a forward pointer (next), anchored by first and last references. Both classes preserve insertion order, allow duplicate elements, and permit multiple null entries.
[HI] ArrayList आंतरिक रूप से ऑब्जेक्ट रेफरेंस को एक सतत Object[] एरे (elementData) में संग्रहीत करता है जो आवश्यकतानुसार बढ़ता है। जावा 8+ में डिफ़ॉल्ट कंस्ट्रक्टर से ArrayList खाली एरे से शुरू होता है और पहला एलिमेंट जोड़ने पर 10 की डिफ़ॉल्ट कैपेसिटी आवंटित (lazy allocation) करता है। इसके विपरीत, LinkedList हीप मेमोरी में स्वतंत्र Node<E> ऑब्जेक्ट्स बनाता है, जिनमें प्रत्येक नोड तीन फील्ड रखता है: डेटा (item), पिछला पॉइंटर (prev) और अगला पॉइंटर (next)। ये दोनों क्लासेस इंसर्शन ऑर्डर बनाए रखती हैं तथा डुप्लिकेट व एकाधिक null मानों की अनुमति देती हैं।
2. Time Complexity Comparison: Random Access vs Insertions/Deletions (टाइम कॉम्प्लेक्सिटी तुलना: रैंडम एक्सेस बनाम इंसर्शन/डिलीशन)
[EN] ArrayList implements the java.util.RandomAccess marker interface, allowing get(int index) and set(int index, E) to execute in constant O(1) time via direct array offset calculation. However, inserting or removing elements at index 0 or in the middle of an ArrayList requires shifting subsequent elements via System.arraycopy(), resulting in O(n) worst-case time, while appending at the end is amortized O(1). LinkedList does NOT implement RandomAccess, so get(index) traverses linearly from the closer end (head or tail), taking O(n) time. Yet LinkedList achieves O(1) insertion and deletion at both head and tail (addFirst, removeLast), and O(1) structural edit when already positioned at a node via a ListIterator (whereas remove(index) still takes O(n) to reach that index).
[HI] ArrayList, java.util.RandomAccess मार्कर इंटरफ़ेस को इम्प्लीमेंट करता है, जिससे get(index) और set(index, E) सीधे इंडेक्स गणना के कारण O(1) समय में पूरे होते हैं। लेकिन ArrayList के शुरुआत या मध्य में एलिमेंट जोड़ने/हटाने पर System.arraycopy() से तत्वों को खिसकाना पड़ता है, जिसमें O(n) समय लगता है, जबकि अंत में जोड़ना औसत O(1) है। LinkedList, RandomAccess इम्प्लीमेंट नहीं करता, इसलिए get(index) को नजदीकी सिरे से ट्रैवर्स करने में O(n) समय लगता है। इसके बावजूद LinkedList दोनों सिरों (addFirst/removeLast) पर तथा पहले से ListIterator द्वारा प्राप्त स्थिति पर O(1) में बदलाव कर सकता है।
3. Capacity Growth (1.5x), Memory Overhead, and Interfaces (कैपेसिटी ग्रोथ (1.5x), मेमोरी ओवरहेड और इंटरफ़ेस अंतर)
[EN] When an ArrayList's backing array fills up, grow() allocates a new array with roughly 1.5 times the previous capacity using the bitwise formula: newCapacity = oldCapacity + (oldCapacity >> 1). Developers can avoid repeated reallocations using ensureCapacity(int minCapacity) before bulk inserts, or release unused trailing slots with trimToSize(). LinkedList requires no resizing or contiguous block, but consumes significantly more memory per element because every item incurs object header overhead plus prev and next reference pointers. Importantly, LinkedList implements both List<E> and Deque<E> (extending Queue<E>), whereas ArrayList implements only List<E> alongside RandomAccess, Cloneable, and Serializable.
[HI] जब ArrayList का आंतरिक एरे भर जाता है, तो grow() मेथड लगभग 1.5 गुना बड़ा नया एरे बनाता है (newCapacity = oldCapacity + (oldCapacity >> 1))। अनावश्यक री-अलोकेशन से बचने के लिए पहले से ensureCapacity() और अतिरिक्त स्थान मुक्त करने के लिए trimToSize() का उपयोग किया जा सकता है। LinkedList को रीसाइज़िंग की आवश्यकता नहीं होती, किंतु प्रत्येक नोड में ऑब्जेक्ट हेडर और दो अतिरिक्त पॉइंटर्स (prev/next) के कारण यह अधिक मेमोरी घेरता है। LinkedList, List<E> और Deque<E> (Queue सहित) दोनों को इम्प्लीमेंट करता है, जबकि ArrayList केवल List<E> व RandomAccess आदि इम्प्लीमेंट करता है।
4. Choosing Between ArrayList, LinkedList, and ArrayDeque (ArrayList, LinkedList और ArrayDeque में सही विकल्प का चयन)
[EN] In real-world Java applications, ArrayList outperforms LinkedList for nearly all List workloads due to CPU cache locality of contiguous arrays and lower garbage collection pressure. Even for middle insertions, System.arraycopy() on primitives/references often runs faster than pointer chasing across scattered heap nodes in a LinkedList. Choose LinkedList specifically when frequent O(1) insertions/deletions at both ends are combined with Queue/Deque semantics or cursor-based ListIterator edits, or when predictable per-operation latency matters more than amortized array resizing. Note that if only FIFO queue or LIFO stack operations are needed without List indexing, java.util.ArrayDeque is generally faster and more memory-efficient than LinkedList.
[HI] व्यावहारिक जावा प्रोग्रामिंग में, CPU कैश लोकैलिटी और कम गार्बेज कलेक्शन ओवरहेड के कारण लगभग सभी सामान्य List कार्यों में ArrayList, LinkedList से तेज़ प्रदर्शन करता है। मध्य में बदलाव के समय भी सतत मेमोरी में System.arraycopy() अक्सर बिखरे हुए नोड्स की पॉइंटर ट्रैवर्सल से अधिक तेज साबित होता है। LinkedList का चयन तभी करें जब दोनों सिरों पर लगातार O(1) जोड़ने/हटाने के साथ List व Deque दोनों की आवश्यकता हो या ListIterator के साथ बार-बार संपादन करना हो। यदि केवल Queue या Stack संचालन ही आवश्यक हो (इंडेक्स एक्सेस नहीं), तो LinkedList की तुलना में ArrayDeque अधिक तेज़ और मेमोरी-कुशल होता है।
Syntax, Formula & Technical Example (List Interface Implementations: ArrayList vs LinkedList)
import java.util.*;
public class ListComparisonDemo {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<>(20);
arr.add("Java"); arr.add("Python");
LinkedList<String> ll = new LinkedList<>(arr);
ll.addFirst("C++");
System.out.println(arr.get(1) + " | Head: " + ll.peekFirst());
}
}
List Interface Implementations: ArrayList vs LinkedList Quick Reference & Comparison Table
| Class / Feature | Technical Specification | Key Exam Fact |
|---|---|---|
| ArrayList Backing | Contiguous resizable Object[] array | Implements RandomAccess; O(1) get(index) |
| LinkedList Backing | Doubly-linked Node (prev, item, next) | Implements List & Deque; O(n) get(index) |
| ArrayList Growth | oldCapacity + (oldCapacity >> 1) | Expands ~1.5x; default initial capacity 10 |
| LinkedList Head Edit | addFirst() / removeFirst() in O(1) | Zero array shifting; higher per-node RAM |
List Interface Implementations: ArrayList vs LinkedList Top 20 Bilingual MCQs with Answers
Q1. Which marker interface does ArrayList implement that LinkedList does NOT?
(ArrayList कौन-सा मार्कर इंटरफ़ेस इम्प्लीमेंट करता है जिसे LinkedList इम्प्लीमेंट नहीं करता?)
(A) java.lang.Iterable
(B) java.lang.Cloneable
(C) java.io.Serializable
(D) java.util.RandomAccess
Correct Answer: (D)
[EN] ArrayList implements RandomAccess to signal O(1) indexed access to algorithms like Collections.binarySearch(), whereas LinkedList does not.
[HI] ArrayList, RandomAccess मार्कर इंटरफ़ेस इम्प्लीमेंट करता है जो O(1) इंडेक्स एक्सेस को दर्शाता है, जबकि LinkedList इसे इम्प्लीमेंट नहीं करता।
Q2. What is the default initial capacity allocated when the first element is added to 'new ArrayList<>()'?
('new ArrayList<>()' में पहला एलिमेंट जोड़ने पर डिफ़ॉल्ट प्रारंभिक कैपेसिटी कितनी आवंटित होती है?)
(A) 16
(B) 10
(C) 8
(D) 32
Correct Answer: (B)
[EN] ArrayList allocates a default capacity of 10 on the first add() call (lazy initialization since Java 8).
[HI] Java 8+ में बिना क्षमता बताए बनाए गए ArrayList में पहला एलिमेंट जोड़ने पर डिफ़ॉल्ट कैपेसिटी 10 आवंटित होती है।
Q3. By approximately what factor does ArrayList grow its internal array capacity when it becomes full?
(जब ArrayList का आंतरिक एरे भर जाता है, तो उसकी कैपेसिटी लगभग कितने गुना बढ़ जाती है?)
(A) 1.25 times
(B) 1.5 times (oldCapacity + oldCapacity / 2)
(C) 2 times (doubles)
(D) Fixed increment of 10
Correct Answer: (B)
[EN] ArrayList calculates newCapacity = oldCapacity + (oldCapacity >> 1), which equals 1.5x growth.
[HI] ArrayList में newCapacity = oldCapacity + (oldCapacity >> 1) सूत्र से कैपेसिटी लगभग 1.5 गुना बढ़ती है।
Q4. Which two major collections interfaces are BOTH implemented by java.util.LinkedList?
(java.util.LinkedList द्वारा निम्नलिखित में से कौन से दो प्रमुख इंटरफ़ेस इम्प्लीमेंट किए जाते हैं?)
(A) List and Set
(B) Map and Deque
(C) Set and Queue
(D) List and Deque (which extends Queue)
Correct Answer: (D)
[EN] LinkedList implements both java.util.List and java.util.Deque (thereby also implementing Queue).
[HI] LinkedList, List और Deque (जो Queue को एक्सटेंड करता है) दोनों इंटरफ़ेस इम्प्लीमेंट करता है।
Q5. What is the worst-case time complexity of calling list.get(index) on an ArrayList versus a LinkedList of size n?
(n आकार की ArrayList बनाम LinkedList पर list.get(index) कॉल करने की वर्स्ट-केस टाइम कॉम्प्लेक्सिटी क्या है?)
(A) O(1) for ArrayList and O(n) for LinkedList
(B) O(1) for both ArrayList and LinkedList
(C) O(log n) for both ArrayList and LinkedList
(D) O(n) for ArrayList and O(1) for LinkedList
Correct Answer: (A)
[EN] ArrayList computes array base + index in O(1), while LinkedList must walk nodes sequentially in O(n).
[HI] ArrayList में इंडेक्स एक्सेस O(1) में होता है, जबकि LinkedList को नोड्स ट्रैवर्स करने में O(n) समय लगता है।
Q6. What is the time complexity of calling remove(0) on an ArrayList versus removeFirst() on a LinkedList?
(ArrayList पर remove(0) बनाम LinkedList पर removeFirst() कॉल करने की टाइम कॉम्प्लेक्सिटी क्या है?)
(A) O(n) for ArrayList and O(1) for LinkedList
(B) O(1) for both
(C) O(1) for ArrayList and O(n) for LinkedList
(D) O(n) for both
Correct Answer: (A)
[EN] Removing index 0 in ArrayList shifts n-1 elements left (O(n)), whereas LinkedList simply updates the head reference in O(1).
[HI] ArrayList में index 0 हटाने पर शेष तत्व खिसकाने में O(n) लगता है, जबकि LinkedList में केवल हेड पॉइंटर बदलने में O(1) समय लगता है।
Q7. Which ArrayList method shrinks its internal elementData capacity down to the current list size to save memory?
(मेमोरी बचाने के लिए ArrayList की आंतरिक कैपेसिटी को उसके वर्तमान size के बराबर कम करने वाला मेथड कौन-सा है?)
(A) trimToSize()
(B) compact()
(C) ensureCapacity()
(D) shrinkToFit()
Correct Answer: (A)
[EN] ArrayList.trimToSize() reallocates the backing array to the exact size() of the list.
[HI] ArrayList.trimToSize() आंतरिक एरे के आकार को वर्तमान एलिमेंट्स की संख्या (size) के बराबर संकुचित कर देता है।
Q8. How many pointer references does each internal Node<E> object in Java's LinkedList hold?
(जावा की LinkedList के प्रत्येक आंतरिक Node<E> ऑब्जेक्ट में कितने पॉइंटर रेफरेंस होते हैं?)
(A) One (item only)
(B) Three (item, next, and prev)
(C) Two (item and next)
(D) Four (item, next, prev, index)
Correct Answer: (B)
[EN] Java's LinkedList is doubly linked, so each Node stores item, next, and prev.
[HI] जावा की LinkedList डबली लिंक्ड होती है, इसलिए प्रत्येक नोड में तीन रेफरेंस (item, next और prev) होते हैं।
Q9. How does Collections.binarySearch(list, key) behave differently when passed a LinkedList instead of an ArrayList?
(ArrayList के बजाय LinkedList पास करने पर Collections.binarySearch(list, key) किस प्रकार कार्य करता है?)
(A) It switches to an iterator-based linear-traversal search (indexedBinarySearch vs iteratorBinarySearch) because LinkedList lacks RandomAccess
(B) It throws UnsupportedOperationException on LinkedList
(C) It runs in O(1) time on LinkedList
(D) It converts LinkedList to HashSet internally
Correct Answer: (A)
[EN] Collections.binarySearch inspects 'list instanceof RandomAccess'; for non-RandomAccess lists like LinkedList, traversing takes O(n) node steps plus O(log n) comparisons.
[HI] RandomAccess न होने के कारण LinkedList पर binarySearch इटरेशन आधारित विधि अपनाता है जिससे नोड ट्रैवर्सल में O(n) समय लग जाता है।
Q10. Which method pre-allocates internal array capacity in ArrayList to minimize resizing overhead before adding 10,000 items?
(10,000 एलिमेंट्स जोड़ने से पहले रीसाइज़िंग ओवरहेड से बचने हेतु ArrayList में आंतरिक क्षमता पूर्व-आवंटित करने का मेथड कौन-सा है?)
(A) ensureCapacity(10000)
(B) setSize(10000)
(C) reserve(10000)
(D) trimToSize(10000)
Correct Answer: (A)
[EN] Calling ensureCapacity(minCapacity) grows the internal array once upfront if needed.
[HI] ensureCapacity(minCapacity) पहले ही आवश्यक आंतरिक एरे कैपेसिटी आवंटित कर देता है।
Q11. Suppose 'ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); list.remove(1);'. What remains in list?
(मान लें 'ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); list.remove(1);' चलाया जाए, तो लिस्ट में क्या बचेगा?)
(A) Throws IndexOutOfBoundsException
(B) [20]
(C) [10, 20]
(D) [10]
Correct Answer: (D)
[EN] remove(int index) is invoked for primitive int 1, removing the element at index 1 (value 20) and leaving [10].
[HI] प्रिमिटिव int 1 देने पर remove(int index) ओवरलोड चलता है, जो इंडेक्स 1 के मान (20) को हटा देता है और [10] बचता है।
Q12. In a LinkedList, how does get(int index) optimize traversal internally?
(LinkedList के अंदर get(int index) आंतरिक रूप से ट्रैवर्सल को कैसे ऑप्टिमाइज़ करता है?)
(A) It always starts from the head node only
(B) It checks whether index < (size >> 1) and traverses forward from first or backward from last accordingly
(C) It uses binary search on node addresses
(D) It maintains a hidden hash table of node indices
Correct Answer: (B)
[EN] Because LinkedList has both first and last pointers, node(index) starts from the closer half of the list, doing at most n/2 steps.
[HI] LinkedList में first और last दोनों पॉइंटर्स होने के कारण यह index की तुलना size/2 से करके नजदीकी सिरे से ट्रैवर्सल शुरू करता है।
Q13. Which class should generally be preferred over LinkedList when you only need a FIFO queue or LIFO stack in single-threaded code?
(सिंगल-थ्रेडेड कोड में यदि केवल FIFO क्यू या LIFO स्टैक चाहिए, तो LinkedList के बजाय किस क्लास को प्राथमिकता देनी चाहिए?)
(A) HashSet
(B) Vector
(C) ArrayDeque
(D) TreeSet
Correct Answer: (C)
[EN] ArrayDeque avoids per-node object allocations and pointer overhead, delivering faster Queue/Deque performance than LinkedList.
[HI] ArrayDeque में प्रत्येक नोड के लिए अलग ऑब्जेक्ट और पॉइंटर ओवरहेड नहीं होता, इसलिए यह LinkedList से तेज़ Queue/Stack प्रदर्शन देता है।
Q14. Which of the following is NOT true about ArrayList and LinkedList?
(ArrayList और LinkedList के बारे में निम्नलिखित में से कौन-सा कथन सत्य नहीं है?)
(A) LinkedList stores elements in contiguous memory addresses for faster CPU caching
(B) Neither ArrayList nor LinkedList is synchronized by default
(C) Both ArrayList and LinkedList allow duplicate and null elements
(D) Both classes return fail-fast iterators
Correct Answer: (A)
[EN] ArrayList uses contiguous memory; LinkedList nodes are scattered across heap memory and suffer poor cache locality.
[HI] ArrayList सतत मेमोरी का उपयोग करता है, जबकि LinkedList के नोड्स हीप मेमोरी में बिखरे होते हैं।
Q15. What happens when 'new ArrayList<>(-5)' is executed in Java?
(जावा में 'new ArrayList<>(-5)' निष्पादित करने पर क्या होता है?)
(A) Creates a zero-capacity ArrayList
(B) Compile-time error
(C) Creates an ArrayList with default capacity 10
(D) Throws IllegalArgumentException
Correct Answer: (D)
[EN] Passing a negative initialCapacity to ArrayList's constructor explicitly throws IllegalArgumentException.
[HI] ArrayList कंस्ट्रक्टर में ऋणात्मक कैपेसिटी पास करने पर रनटाइम पर IllegalArgumentException उत्पन्न होता है।
Q16. When using an ArrayList vs LinkedList, why does ArrayList consume less memory per element?
(ArrayList बनाम LinkedList की तुलना में ArrayList प्रति एलिमेंट कम मेमोरी क्यों घेरता है?)
(A) ArrayList stores only the element reference in an array slot without per-node object headers or prev/next pointers
(B) LinkedList duplicates every element internally for safety
(C) ArrayList compresses element values in binary form
(D) ArrayList stores primitives directly without wrapper objects
Correct Answer: (A)
[EN] Each LinkedList Node object requires an object header plus 3 references (item, prev, next), whereas ArrayList holds one reference per slot.
[HI] LinkedList के प्रत्येक नोड को ऑब्जेक्ट हेडर तथा item, prev, next रेफरेंस की आवश्यकता होती है, जबकि ArrayList केवल एरे स्लॉट में एक रेफरेंस रखता है।
Q17. Within List Interface Implementations: ArrayList vs LinkedList, which class / feature directly maps to: "Contiguous resizable Object[] array"?
(List Interface Implementations: ArrayList vs LinkedList में कौन सा Class / Feature सीधे "Contiguous resizable Object[] array" से संबंधित है?)
(A) ArrayList Growth
(B) LinkedList Backing
(C) ArrayList Backing
(D) LinkedList Head Edit
Correct Answer: (C)
[EN] In List Interface Implementations: ArrayList vs LinkedList, 'ArrayList Backing' handles 'Contiguous resizable Object[] array' (Implements RandomAccess; O(1) get(index)).
[HI] List Interface Implementations: ArrayList vs LinkedList में 'ArrayList Backing' का कार्य/विवरण 'Contiguous resizable Object[] array' (Implements RandomAccess; O(1) get(index)) है।
Q18. Which option accurately describes 'LinkedList Backing' in List Interface Implementations: ArrayList vs LinkedList?
(List Interface Implementations: ArrayList vs LinkedList के संदर्भ में 'LinkedList Backing' की सही विशेषता कौन सी है?)
(A) Contiguous resizable Object[] array
(B) oldCapacity + (oldCapacity >> 1)
(C) addFirst() / removeFirst() in O(1)
(D) Doubly-linked Node (prev, item, next)
Correct Answer: (D)
[EN] 'LinkedList Backing' provides 'Doubly-linked Node (prev, item, next)' — key point: Implements List & Deque; O(n) get(index).
[HI] 'LinkedList Backing' का मुख्य विवरण 'Doubly-linked Node (prev, item, next)' है (Implements List & Deque; O(n) get(index))।
Q19. In List Interface Implementations: ArrayList vs LinkedList, identify the correct parameter for the specification: "oldCapacity + (oldCapacity >> 1)".
(List Interface Implementations: ArrayList vs LinkedList में "oldCapacity + (oldCapacity >> 1)" विवरण किस विकल्प के लिए सही है?)
(A) LinkedList Head Edit
(B) ArrayList Backing
(C) ArrayList Growth
(D) LinkedList Backing
Correct Answer: (C)
[EN] 'ArrayList Growth' corresponds to 'oldCapacity + (oldCapacity >> 1)' (Expands ~1.5x; default initial capacity 10).
[HI] 'ArrayList Growth' का संबंध 'oldCapacity + (oldCapacity >> 1)' (Expands ~1.5x; default initial capacity 10) से है।
Q20. Considering List Interface Implementations: ArrayList vs LinkedList, what key behavior defines 'LinkedList Head Edit'?
(List Interface Implementations: ArrayList vs LinkedList में 'LinkedList Head Edit' की मुख्य भूमिका या मान क्या है?)
(A) Doubly-linked Node (prev, item, next)
(B) Contiguous resizable Object[] array
(C) addFirst() / removeFirst() in O(1)
(D) oldCapacity + (oldCapacity >> 1)
Correct Answer: (C)
[EN] 'LinkedList Head Edit' represents 'addFirst() / removeFirst() in O(1)' (Zero array shifting; higher per-node RAM).
[HI] 'LinkedList Head Edit' का अर्थ/मान 'addFirst() / removeFirst() in O(1)' है (Zero array shifting; higher per-node RAM)।
List Interface Implementations: ArrayList vs LinkedList — Exam FAQs
Q1: Is ArrayList always faster than LinkedList for inserting elements in the middle?
क्या मध्य में एलिमेंट जोड़ने के लिए हमेशा ArrayList, LinkedList से तेज़ होता है?
[EN] If you already hold a ListIterator at the target position, LinkedList inserts in O(1) pointer updates; however, if inserting by index (add(idx, val)), LinkedList still pays O(n) traversal while ArrayList shifts contiguous memory very quickly via native System.arraycopy().
[HI] यदि आपके पास पहले से उस स्थान पर ListIterator है तो LinkedList O(1) में इंसर्ट करता है, लेकिन इंडेक्स से जोड़ने पर LinkedList को नोड खोजने में O(n) लगता है और कई बार ArrayList का System.arraycopy() अधिक तेज रहता है।
Q2: How do you make an ArrayList or LinkedList thread-safe?
हम ArrayList या LinkedList को थ्रेड-सेफ (thread-safe) कैसे बना सकते हैं?
[EN] Wrap it with Collections.synchronizedList(new ArrayList<>()) (and synchronize manually while iterating) or use CopyOnWriteArrayList for read-heavy concurrent workloads.
[HI] इसके लिए Collections.synchronizedList(...) रैपर का उपयोग करें (इटरेशन पर मैनुअल synchronized ब्लॉक सहित) या अधिक रीड वाले कंकरेंट कार्यों के लिए CopyOnWriteArrayList चुनें।
Q3: Does LinkedList have a capacity() method like ArrayList or Vector?
क्या LinkedList में Vector की तरह कोई capacity() मेथड होता है?
[EN] No; LinkedList allocates nodes dynamically one by one on demand, so it has no fixed backing capacity concept or capacity() method.
[HI] नहीं; LinkedList आवश्यकता के अनुसार एक-एक नोड बनाता है, इसलिए इसमें पूर्व-निर्धारित कैपेसिटी की अवधारणा नहीं होती।
Post a Comment
Post a Comment