Introduction to ArrayList: Dynamic Resizable Arrays in Java Collections
Introduction to ArrayList: Dynamic Resizable Arrays in Java Collections
ArrayList क्या है?
Java में ArrayList एक बहुत ही popular class है जो java.util package का हिस्सा है। ये class dynamic array की तरह काम करती है, यानी इसका size automatically बढ़ या घट सकता है। जब आपको पहले से नहीं पता कि कितने elements store करने हैं, तब ArrayList सबसे best option होती है।
Normal array में हमें size पहले से define करना पड़ता है, लेकिन ArrayList में ऐसा नहीं करना पड़ता। इसमें जब elements बढ़ते हैं, तो ये अपने size को automatically adjust कर लेती है। इसलिए इसे dynamic resizable array भी कहा जाता है।
ArrayList की मुख्य विशेषताएँ (Features of ArrayList)
- Dynamic Size: ArrayList का size जरूरत के अनुसार बढ़ता या घटता है।
- Ordered Collection: इसमें elements उसी order में store होते हैं जिस order में उन्हें insert किया जाता है।
- Duplicates Allowed: ArrayList duplicate elements को allow करती है।
- Index Based Access: Elements को index के जरिए access किया जा सकता है, जैसे normal array में होता है।
- Fast Read Operation: Read operation (get) बहुत तेज होता है क्योंकि ये internally array की तरह काम करती है।
ArrayList का Syntax
ArrayList को use करने के लिए आपको java.util.ArrayList import करनी होती है।
import java.util.ArrayList;
ArrayList<Type> list = new ArrayList<Type>();
यहाँ Type उस data type को बताता है जिसे आप store करना चाहते हैं, जैसे Integer, String आदि।
Example:
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Rahul");
names.add("Ankit");
names.add("Suman");
System.out.println(names);
}
}
Output:
[Rahul, Ankit, Suman]
ArrayList का Internal Working
ArrayList internally एक simple array का use करती है। जब array में जगह कम पड़ जाती है, तो ये नया बड़ा array बनाकर पुराने elements को उसमें copy कर देती है।
By default ArrayList की initial capacity 10 होती है। जब capacity पूरी भर जाती है, तो ये अपने size को 1.5 गुना बढ़ा देती है (नया size = पुराना size * 1.5 + 1)।
ArrayList के Important Methods
ArrayList में कई useful methods होते हैं जो elements को add, remove या access करने में काम आते हैं। नीचे कुछ common methods दिए गए हैं:
| Method | Description |
|---|---|
| add(E e) | ArrayList में element जोड़ता है |
| add(int index, E e) | Specified index पर element insert करता है |
| get(int index) | Specified index का element return करता है |
| set(int index, E e) | Specified index पर मौजूद element को replace करता है |
| remove(int index) | Specified index का element delete करता है |
| size() | ArrayList में मौजूद total elements की संख्या बताता है |
| clear() | पूरी ArrayList को empty कर देता है |
| contains(Object o) | Check करता है कि element मौजूद है या नहीं |
| isEmpty() | Check करता है कि ArrayList empty है या नहीं |
Example Program of ArrayList
import java.util.*;
public class StudentList {
public static void main(String[] args) {
ArrayList<String> students = new ArrayList<>();
students.add("Amit");
students.add("Rohit");
students.add("Sneha");
System.out.println("All Students: " + students);
students.remove("Rohit");
System.out.println("After Removal: " + students);
System.out.println("Total Students: " + students.size());
}
}
ArrayList Traversal (Iteration)
ArrayList को iterate करने के लिए हम कई तरीके use कर सकते हैं:
1. Using for loop
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
2. Using for-each loop
for(String name : list) {
System.out.println(name);
}
3. Using Iterator
Iterator<String> itr = list.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
ArrayList vs Array
आइए एक comparison table देखते हैं जो Array और ArrayList के बीच का अंतर बताती है:
| Feature | Array | ArrayList |
|---|---|---|
| Size | Fixed | Dynamic (Auto resize) |
| Performance | Faster for primitive data | Slower comparatively |
| Storage Type | Primitive and Objects | Only Objects |
| Flexibility | Less Flexible | Highly Flexible |
| Length Access | array.length | list.size() |
ArrayList Performance Analysis
- Access (get): O(1) — बहुत तेज क्योंकि index-based access है।
- Insertion (add): Average O(1), लेकिन कभी-कभी resizing की वजह से O(n)।
- Removal: O(n) — क्योंकि remove करने के बाद बाकी elements को shift करना पड़ता है।
ArrayList और Java Collections Framework
ArrayList, Java Collections Framework का हिस्सा है और ये List Interface को implement करती है। इसका मतलब है कि ArrayList में वो सारे features हैं जो एक list में होने चाहिए, जैसे order, duplicates, index access आदि।
इसके अलावा ArrayList, AbstractList class को extend करती है। इसलिए इसके अंदर कई built-in methods पहले से defined रहते हैं।
Generics के साथ ArrayList
Java 1.5 के बाद से ArrayList में Generics use किए जा सकते हैं। इससे compile time पर type safety मिलती है — यानी आप गलत type के data को add नहीं कर सकते।
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
// numbers.add("String"); // Error: type mismatch
ArrayList को Array में Convert करना
कई बार हमें ArrayList को simple array में बदलना पड़ता है। इसके लिए toArray() method use किया जाता है।
ArrayList<String> names = new ArrayList<>();
names.add("Amit");
names.add("Rohit");
String[] arr = names.toArray(new String[0]);
for(String s : arr) {
System.out.println(s);
}
ArrayList का उपयोग (Use Cases)
- जब आपको frequently read operation करने हों।
- जब data का size बार-बार बदलता रहता हो।
- जब आपको index-based access चाहिए।
- जब duplicates allow करने हों।
ArrayList की सीमाएँ (Limitations)
- Insertion और Deletion operations slow होते हैं क्योंकि shifting करनी पड़ती है।
- Thread-safe नहीं है (अगर multiple threads एक ही time पर use करें तो problem आ सकती है)।
- Memory consumption ज्यादा होती है क्योंकि capacity बढ़ाने पर नया array बनाना पड़ता है।
ArrayList Synchronization
By default, ArrayList synchronized नहीं होती। अगर आपको इसे multi-thread environment में use करना है तो आप इसे Collections.synchronizedList() से thread-safe बना सकते हैं।
List<String> syncList = Collections.synchronizedList(new ArrayList<>());
ArrayList vs LinkedList
| Feature | ArrayList | LinkedList |
|---|---|---|
| Storage | Dynamic Array | Doubly Linked Nodes |
| Access Speed | Fast (O(1)) | Slow (O(n)) |
| Insertion/Deletion | Slow (Shifting required) | Fast (No shifting) |
| Memory | Less | More |
Exam-Oriented Notes (Quick Revision)
- ArrayList java.util package में होती है।
- ArrayList List Interface को implement करती है।
- ArrayList का size dynamic होता है।
- Index-based access provide करती है।
- Duplicates elements को allow करती है।
- Initial capacity: 10
- Performance: Access fast, Insertion slow
- Not thread-safe by default
- Can be synchronized using Collections.synchronizedList()
- Generics के साथ type safety मिलती है।