list ki sarri kii sari step steep explain like add addAll remove etc.
- User
list ki sarri kii sari step steep explain like add addAll remove etc.
Assistant
हाँ, बिलकुल!
java.util.ListJava Collections Framework का एक बहुत ही महत्वपूर्ण Interface है। यह एक Ordered Collection है, जिसका अर्थ है कि यह elements के insertion order को बनाए रखता है और आप elements को उनके index (position) के आधार पर access कर सकते हैं। यह duplicate elements को भी allow करता है।ListInterface को कई classes implement करती हैं, जिनमें सबसे आम हैं:ArrayList: Internally एक dynamic array का उपयोग करता है। Random access (जैसेget(index)) बहुत तेज़ होता है, लेकिन बीच में elements को add या remove करना (क्योंकि shifting की ज़रूरत होती है) धीमा हो सकता है।LinkedList: Internally एक doubly linked list का उपयोग करता है। Elements को add या remove करना (खासकर सिरों पर) तेज़ होता है, लेकिन index के आधार पर access (get(index)) धीमा हो सकता है क्योंकि इसे list को traverse करना पड़ता है।Vector:ArrayListके समान है, लेकिन यह thread-safe (synchronized) है। Performance के लिहाज़ से यह धीमा हो सकता है, इसलिए मल्टी-थ्रेडेड एनवायरनमेंट में भीArrayListऔरCollections.synchronizedList()का उपयोग ज़्यादा पसंद किया जाता है।
आइए,
ListInterface के सभी महत्वपूर्ण methods को step-by-step और उदाहरण के साथ समझते हैं:हम उदाहरण के लिए
ArrayListका उपयोग करेंगे, लेकिन ये methodsListinterface के हिस्से हैं, इसलिए वेLinkedListया किसी भी अन्यListimplementation पर भी लागू होते हैं।import java.util.ArrayList; import java.util.List; import java.util.Iterator; import java.util.Collection; // For addAll, removeAll, etc. public class ListMethodsExplanation { public static void main(String[] args) { // Step 1: List का निर्माण (Creating a List) System.out.println("--- Step 1: List का निर्माण ---"); List<String> fruits = new ArrayList<>(); System.out.println("एक खाली List 'fruits' बनाई गई: " + fruits); // Output: [] System.out.println("\n--- Step 2: Elements को List में जोड़ना (Adding Elements) ---"); // 2.1: add(E element) // उद्देश्य: List के अंत में एक element जोड़ता है। // Return Value: boolean (हमेशा true, क्योंकि List हमेशा add कर सकता है) // Working: यह element को List में आखिरी position पर रखता है। System.out.println("2.1: add(E element)"); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Cherry"); System.out.println("Elements जोड़ने के बाद 'fruits': " + fruits); // Output: [Apple, Banana, Cherry] // 2.2: add(int index, E element) // उद्देश्य: specified index पर एक element जोड़ता है। existing elements को right में shift करता है। // Return Value: void // Working: यह element को उस index पर डालता है, और उस index पर जो भी element था और उसके बाद के सभी elements // एक position आगे (right) खिसक जाते हैं। // Exception: IndexOutOfBoundsException यदि index वैध नहीं है (index < 0 या index > size()) System.out.println("\n2.2: add(int index, E element)"); fruits.add(1, "Orange"); // index 1 पर Orange जोड़ो System.out.println("Index 1 पर 'Orange' जोड़ने के बाद 'fruits': " + fruits); // Output: [Apple, Orange, Banana, Cherry] // fruits.add(5, "Grape"); // Uncomment this to see IndexOutOfBoundsException // 2.3: addAll(Collection<? extends E> c) // उद्देश्य: एक और Collection के सभी elements को List के अंत में जोड़ता है। // Return Value: boolean (यदि List add करने के परिणामस्वरूप modify हुआ हो तो true) // Working: यह दिए गए Collection के सभी elements को current List के अंत में एक-एक करके जोड़ता है। System.out.println("\n2.3: addAll(Collection<? extends E> c)"); List<String> tropicalFruits = new ArrayList<>(); tropicalFruits.add("Mango"); tropicalFruits.add("Pineapple"); fruits.addAll(tropicalFruits); System.out.println("दूसरे List 'tropicalFruits' को जोड़ने के बाद 'fruits': " + fruits); // Output: [Apple, Orange, Banana, Cherry, Mango, Pineapple] // 2.4: addAll(int index, Collection<? extends E> c) // उद्देश्य: specified index पर एक और Collection के सभी elements जोड़ता है। // Return Value: boolean (यदि List add करने के परिणामस्वरूप modify हुआ हो तो true) // Working: यह दिए गए Collection के सभी elements को current List के उस index पर जोड़ता है, // और उस index से शुरू होने वाले सभी original elements आगे (right) खिसक जाते हैं। // Exception: IndexOutOfBoundsException यदि index वैध नहीं है। System.println("\n2.4: addAll(int index, Collection<? extends E> c)"); List<String> citrusFruits = new ArrayList<>(); citrusFruits.add("Lemon"); citrusFruits.add("Lime"); fruits.addAll(2, citrusFruits); // index 2 पर Lemon और Lime जोड़ो System.out.println("Index 2 पर 'citrusFruits' जोड़ने के बाद 'fruits': " + fruits); // Output: [Apple, Orange, Lemon, Lime, Banana, Cherry, Mango, Pineapple] System.out.println("\n--- Step 3: Elements को List से हटाना (Removing Elements) ---"); // 3.1: remove(Object o) // उद्देश्य: List से specified element की पहली occurrence को हटाता है। // Return Value: boolean (यदि element successfully हटाया गया हो तो true) // Working: यह List को शुरू से अंत तक scan करता है और object के बराबर पहले element को हटाता है। // उसके बाद के elements left में shift होते हैं। System.out.println("3.1: remove(Object o)"); boolean removedApple = fruits.remove("Apple"); System.out.println("'Apple' हटाया गया? " + removedApple + ", 'fruits' अब: " + fruits); // Output: true, [Orange, Lemon, Lime, Banana, Cherry, Mango, Pineapple] boolean removedGrape = fruits.remove("Grape"); // जो मौजूद नहीं है System.out.println("'Grape' हटाया गया? " + removedGrape + ", 'fruits' अब: " + fruits); // Output: false, [Orange, Lemon, Lime, Banana, Cherry, Mango, Pineapple] // 3.2: remove(int index) // उद्देश्य: specified index पर element को हटाता है। // Return Value: E (हटाया गया element) // Working: यह उस index पर मौजूद element को हटाता है। उसके बाद के elements left में shift होते हैं। // Exception: IndexOutOfBoundsException यदि index वैध नहीं है। System.out.println("\n3.2: remove(int index)"); String removedFruit = fruits.remove(0); // index 0 पर 'Orange' था System.out.println("Index 0 से '" + removedFruit + "' हटाया गया, 'fruits' अब: " + fruits); // Output: Orange, [Lemon, Lime, Banana, Cherry, Mango, Pineapple] // fruits.remove(10); // Uncomment this to see IndexOutOfBoundsException // 3.3: removeAll(Collection<?> c) // उद्देश्य: List से उन सभी elements को हटाता है जो specified Collection में भी मौजूद हैं। // Return Value: boolean (यदि List modify हुआ हो तो true) // Working: यह List के elements को Iteratively हटाता है जो दिए गए Collection में हैं। यह Set Difference के समान है। System.out.println("\n3.3: removeAll(Collection<?> c)"); List<String> toRemove = new ArrayList<>(); toRemove.add("Lime"); toRemove.add("Cherry"); fruits.removeAll(toRemove); System.out.println("से 'Lime' और 'Cherry' हटाने के बाद 'fruits': " + fruits); // Output: [Lemon, Banana, Mango, Pineapple] // 3.4: clear() // उद्देश्य: List से सभी elements को हटाता है। List खाली हो जाती है। // Return Value: void // Working: List को empty state में लाता है। System.out.println("\n3.4: clear()"); List<String> tempFruits = new ArrayList<>(fruits); // fruits की एक copy बना लेते हैं tempFruits.clear(); System.out.println("'tempFruits' को clear करने के बाद: " + tempFruits); // Output: [] System.out.println("Original 'fruits' अभी भी: " + fruits); // Output: [Lemon, Banana, Mango, Pineapple] (clear नहीं किया गया) // 3.5: retainAll(Collection<?> c) // उद्देश्य: List से उन सभी elements को हटाता है जो specified Collection में *नहीं* मौजूद हैं। // यानी, List में केवल वही elements बचते हैं जो specified Collection में भी हैं। // Return Value: boolean (यदि List modify हुआ हो तो true) // Working: यह Set Intersection के समान है। System.out.println("\n3.5: retainAll(Collection<?> c)"); List<String> toRetain = new ArrayList<>(); toRetain.add("Banana"); toRetain.add("Mango"); toRetain.add("Grapes"); // यह मौजूद नहीं है, इसलिए इसका कोई प्रभाव नहीं होगा fruits.retainAll(toRetain); System.out.println("'Banana' और 'Mango' को retain करने के बाद 'fruits': " + fruits); // Output: [Banana, Mango] System.out.println("\n--- Step 4: Elements को Access/Modify करना (Accessing/Modifying Elements) ---"); // List को फिर से भरें for demonstration fruits.clear(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Cherry"); fruits.add("Date"); fruits.add("Elderberry"); System.out.println("वर्तमान 'fruits' List: " + fruits); // Output: [Apple, Banana, Cherry, Date, Elderberry] // 4.1: get(int index) // उद्देश्य: specified index पर element को retrieve करता है। // Return Value: E (index पर मौजूद element) // Working: सीधे उस index पर मौजूद element को देता है। // Exception: IndexOutOfBoundsException यदि index वैध नहीं है। System.out.println("\n4.1: get(int index)"); String firstFruit = fruits.get(0); String thirdFruit = fruits.get(2); System.out.println("Index 0 पर: " + firstFruit); // Output: Apple System.out.println("Index 2 पर: " + thirdFruit); // Output: Cherry // 4.2: set(int index, E element) // उद्देश्य: specified index पर element को नए element से replace करता है। // Return Value: E (replaced किया गया पुराना element) // Working: यह पुराने element को हटाकर नए element को उसी position पर रखता है। List का size नहीं बदलता। // Exception: IndexOutOfBoundsException यदि index वैध नहीं है। System.out.println("\n4.2: set(int index, E element)"); String oldFruit = fruits.set(1, "Grape"); // Banana को Grape से replace करो System.out.println("Index 1 पर 'Banana' को 'Grape' से बदला गया। पुराना फल था: " + oldFruit); // Output: Banana System.out.println("'fruits' अब: " + fruits); // Output: [Apple, Grape, Cherry, Date, Elderberry] System.out.println("\n--- Step 5: List की जानकारी प्राप्त करना (Querying List Information) ---"); // 5.1: size() // उद्देश्य: List में elements की संख्या लौटाता है। // Return Value: int (elements की संख्या) System.out.println("5.1: size()"); int size = fruits.size(); System.out.println("'fruits' List का size: " + size); // Output: 5 // 5.2: isEmpty() // उद्देश्य: जांचता है कि List खाली है या नहीं। // Return Value: boolean (यदि List में कोई element नहीं है तो true) System.out.println("\n5.2: isEmpty()"); boolean empty = fruits.isEmpty(); System.out.println("'fruits' List खाली है? " + empty); // Output: false List<String> emptyList = new ArrayList<>(); System.out.println("एक खाली List खाली है? " + emptyList.isEmpty()); // Output: true // 5.3: contains(Object o) // उद्देश्य: जांचता है कि specified element List में मौजूद है या नहीं। // Return Value: boolean (यदि element मौजूद है तो true) // Working: equals() method का उपयोग करके elements की तुलना करता है। System.out.println("\n5.3: contains(Object o)"); boolean hasCherry = fruits.contains("Cherry"); System.out.println("'fruits' में 'Cherry' है? " + hasCherry); // Output: true boolean hasKiwi = fruits.contains("Kiwi"); System.out.println("'fruits' में 'Kiwi' है? " + hasKiwi); // Output: false // 5.4: containsAll(Collection<?> c) // उद्देश्य: जांचता है कि specified Collection के सभी elements List में मौजूद हैं या नहीं। // Return Value: boolean (यदि सभी elements मौजूद हैं तो true) System.out.println("\n5.4: containsAll(Collection<?> c)"); List<String> subListCheck = new ArrayList<>(); subListCheck.add("Apple"); subListCheck.add("Date"); boolean allPresent = fruits.containsAll(subListCheck); System.out.println("'fruits' में 'Apple' और 'Date' दोनों हैं? " + allPresent); // Output: true List<String> subListCheck2 = new ArrayList<>(); subListCheck2.add("Apple"); subListCheck2.add("Kiwi"); boolean allPresent2 = fruits.containsAll(subListCheck2); System.out.println("'fruits' में 'Apple' और 'Kiwi' दोनों हैं? " + allPresent2); // Output: false (क्योंकि Kiwi नहीं है) // 5.5: indexOf(Object o) // उद्देश्य: specified element की पहली occurrence का index लौटाता है। // Return Value: int (index, या -1 यदि element नहीं मिला) System.out.println("\n5.5: indexOf(Object o)"); int indexCherry = fruits.indexOf("Cherry"); System.out.println("'Cherry' का पहला index: " + indexCherry); // Output: 2 int indexKiwi = fruits.indexOf("Kiwi"); System.out.println("'Kiwi' का पहला index: " + indexKiwi); // Output: -1 // 5.6: lastIndexOf(Object o) // उद्देश्य: specified element की आखिरी occurrence का index लौटाता है। // Return Value: int (index, या -1 यदि element नहीं मिला) System.out.println("\n5.6: lastIndexOf(Object o)"); fruits.add("Apple"); // एक और Apple जोड़ो System.out.println("List में एक और 'Apple' जोड़ने के बाद: " + fruits); int lastIndexApple = fruits.lastIndexOf("Apple"); System.out.println("'Apple' का आखिरी index: " + lastIndexApple); // Output: 5 int lastIndexGrape = fruits.lastIndexOf("Grape"); System.out.println("'Grape' का आखिरी index: " + lastIndexGrape); // Output: 1 System.out.println("\n--- Step 6: Sublist और Array Conversion ---"); // 6.1: subList(int fromIndex, int toIndex) // उद्देश्य: List के एक हिस्से का "view" लौटाता है। (यह एक नया List नहीं बनाता, बल्कि original List का एक हिस्सा होता है) // Return Value: List<E> (sub-list) // Working: fromIndex (inclusive) से toIndex (exclusive) तक के elements को दिखाता है। // Sub-list में कोई भी बदलाव original List को भी प्रभावित करता है, और vice-versa। // Exception: IndexOutOfBoundsException, IllegalArgumentException (यदि fromIndex > toIndex) System.out.println("6.1: subList(int fromIndex, int toIndex)"); List<String> middleFruits = fruits.subList(1, 4); // Index 1, 2, 3 System.out.println("Sub-list (indices 1 to 3): " + middleFruits); // Output: [Grape, Cherry, Date] // Sub-list में बदलाव Original List को प्रभावित करता है middleFruits.remove("Cherry"); System.out.println("Sub-list से 'Cherry' हटाने के बाद: " + middleFruits); // Output: [Grape, Date] System.out.println("Original 'fruits' List भी बदल गई: " + fruits); // Output: [Apple, Grape, Date, Elderberry, Apple] // 6.2: toArray() // उद्देश्य: List के सभी elements को एक Object array के रूप में लौटाता है। // Return Value: Object[] System.out.println("\n6.2: toArray()"); Object[] fruitArray = fruits.toArray(); System.out.print("List को Object Array में बदला: "); for (Object fruit : fruitArray) { System.out.print(fruit + " "); } System.out.println(); // Output: Apple Grape Date Elderberry Apple // 6.3: toArray(T[] a) // उद्देश्य: List के सभी elements को specified type के array के रूप में लौटाता है। // Return Value: T[] (specified type का array) // Working: यह सुनिश्चित करता है कि array सही type का हो। यदि प्रदान किया गया array छोटा है, तो एक नया array बनाया जाता है। System.out.println("\n6.3: toArray(T[] a)"); String[] stringFruitArray = fruits.toArray(new String[0]); // Size 0 देने से एक नया array सही size का बन जाता है System.out.print("List को String Array में बदला: "); for (String fruit : stringFruitArray) { System.out.print(fruit + " "); } System.out.println(); // Output: Apple Grape Date Elderberry Apple System.out.println("\n--- Step 7: List को Iterate करना (Iterating through a List) ---"); // List को iterate करने के कई तरीके हैं: // 7.1: Enhanced for-loop (सबसे common और readable) System.out.println("7.1: Enhanced for-loop:"); for (String fruit : fruits) { System.out.println("Fruit: " + fruit); } // 7.2: Traditional for-loop (index के साथ) System.out.println("\n7.2: Traditional for-loop (by index):"); for (int i = 0; i < fruits.size(); i++) { System.out.println("Fruit at index " + i + ": " + fruits.get(i)); } // 7.3: Using Iterator (elements को remove करने के लिए उपयोगी) System.out.println("\n7.3: Using Iterator:"); Iterator<String> iterator = fruits.iterator(); while (iterator.hasNext()) { String fruit = iterator.next(); System.out.println("Iterated Fruit: " + fruit); // Example of removing during iteration (safe with Iterator) if (fruit.equals("Apple")) { iterator.remove(); // Removes the current element safely } } System.out.println("After iterating and removing 'Apple's: " + fruits); // Output: [Grape, Date, Elderberry] // 7.4: Using forEach() with Lambda Expression (Java 8+) System.out.println("\n7.4: Using forEach() with Lambda (Java 8+):"); fruits.forEach(fruit -> System.out.println("Lambda Fruit: " + fruit)); System.out.println("\n--- सभी महत्वपूर्ण List methods कवर किए गए! ---"); } }सारांश (Summary):
java.util.Listएक versatile और व्यापक रूप से उपयोग किया जाने वाला interface है जो ordered collections को handle करने के लिए एक मजबूत foundation प्रदान करता है।- Ordered (क्रमबद्ध): Elements insertion order में रहते हैं।
- Indexed (इंडेक्स्ड): Elements को उनके numeric index (0 से शुरू) द्वारा एक्सेस किया जा सकता है।
- Allows Duplicates (डुप्लीकेट्स की अनुमति): एक ही element को List में कई बार स्टोर किया जा सकता है।
ऊपर दिए गए methods के साथ, आप List में elements को add, remove, modify, और query कर सकते हैं, जिससे यह Java में data को store और manage करने के लिए एक fundamental data structure बन जाता है।
ArrayListऔरLinkedListके बीच चयन आपके specific use case (random access बनाम frequent insertions/deletions) पर निर्भर करता है।- User
in dart
Assistant
हाँ, बिलकुल! Java
Listकी तरह ही, Dart में भीListएक बहुत ही fundamental collection type है। यह ordered (क्रमबद्ध) और index-based (इंडेक्स-आधारित) है, जिसका मतलब है कि आप elements को उनके position के अनुसार access कर सकते हैं। DartListduplicate elements को भी allow करता है।Dart में,
Listdynamically growable (अपने आप बढ़ सकता है) होता है, जब तक कि आप इसे explicit रूप से fixed-length न बना दें।आइए, Dart
Listके सभी महत्वपूर्ण methods और properties को step-by-step और उदाहरण के साथ समझते हैं:void main() { // Step 1: List का निर्माण (Creating a List) print("--- Step 1: List का निर्माण ---"); // Dart में List को initialize करने के कई तरीके हैं। // सबसे आम तरीका: growable list (default) List<String> fruits = []; // या List<String> fruits = <String>[]; print("एक खाली List 'fruits' बनाई गई: $fruits"); // Output: [] // आप सीधे elements के साथ भी बना सकते हैं: List<int> numbers = [10, 20, 30]; print("Elements के साथ बनाई गई List 'numbers': $numbers"); // Output: [10, 20, 30] // Fixed-length List (कम उपयोग किया जाता है जब तक कि आप size को पहले से न जानते हों) // List<String> fixedFruits = List.filled(3, 'Empty'); // print("Fixed-length List 'fixedFruits': $fixedFruits"); // Output: [Empty, Empty, Empty] // fixedFruits.add('New'); // Error: Cannot add to a fixed-length list print("\n--- Step 2: Elements को List में जोड़ना (Adding Elements) ---"); // 2.1: add(E element) // उद्देश्य: List के अंत में एक element जोड़ता है। // Return Value: void // Working: यह element को List में आखिरी position पर रखता है और List का size एक से बढ़ाता है। print("2.1: add(E element)"); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Cherry"); print("Elements जोड़ने के बाद 'fruits': $fruits"); // Output: [Apple, Banana, Cherry] // 2.2: insert(int index, E element) // उद्देश्य: specified index पर एक element जोड़ता है। existing elements को right में shift करता है। // Return Value: void // Working: यह element को उस index पर डालता है, और उस index पर जो भी element था और उसके बाद के सभी elements // एक position आगे (right) खिसक जाते हैं। // Exception: RangeError यदि index वैध नहीं है (index < 0 या index > length) print("\n2.2: insert(int index, E element)"); fruits.insert(1, "Orange"); // index 1 पर Orange जोड़ो print("Index 1 पर 'Orange' जोड़ने के बाद 'fruits': $fruits"); // Output: [Apple, Orange, Banana, Cherry] // fruits.insert(5, "Grape"); // Uncomment this to see RangeError // 2.3: addAll(Iterable<E> iterable) // उद्देश्य: एक और Iterable (जैसे List) के सभी elements को List के अंत में जोड़ता है। // Return Value: void // Working: यह दिए गए Iterable के सभी elements को current List के अंत में एक-एक करके जोड़ता है। print("\n2.3: addAll(Iterable<E> iterable)"); List<String> tropicalFruits = ["Mango", "Pineapple"]; fruits.addAll(tropicalFruits); print("दूसरे List 'tropicalFruits' को जोड़ने के बाद 'fruits': $fruits"); // Output: [Apple, Orange, Banana, Cherry, Mango, Pineapple] // 2.4: insertAll(int index, Iterable<E> iterable) // उद्देश्य: specified index पर एक और Iterable के सभी elements जोड़ता है। // Return Value: void // Working: यह दिए गए Iterable के सभी elements को current List के उस index पर जोड़ता है, // और उस index से शुरू होने वाले सभी original elements आगे (right) खिसक जाते हैं। // Exception: RangeError यदि index वैध नहीं है। print("\n2.4: insertAll(int index, Iterable<E> iterable)"); List<String> citrusFruits = ["Lemon", "Lime"]; fruits.insertAll(2, citrusFruits); // index 2 पर Lemon और Lime जोड़ो print("Index 2 पर 'citrusFruits' जोड़ने के बाद 'fruits': $fruits"); // Output: [Apple, Orange, Lemon, Lime, Banana, Cherry, Mango, Pineapple] print("\n--- Step 3: Elements को List से हटाना (Removing Elements) ---"); // 3.1: remove(Object? value) // उद्देश्य: List से specified element की पहली occurrence को हटाता है। // Return Value: bool (यदि element successfully हटाया गया हो तो true) // Working: यह List को शुरू से अंत तक scan करता है और object के बराबर पहले element को हटाता है। // उसके बाद के elements left में shift होते हैं। print("3.1: remove(Object? value)"); bool removedApple = fruits.remove("Apple"); print("'Apple' हटाया गया? $removedApple, 'fruits' अब: $fruits"); // Output: true, [Orange, Lemon, Lime, Banana, Cherry, Mango, Pineapple] bool removedGrape = fruits.remove("Grape"); // जो मौजूद नहीं है print("'Grape' हटाया गया? $removedGrape, 'fruits' अब: $fruits"); // Output: false, [Orange, Lemon, Lime, Banana, Cherry, Mango, Pineapple] // 3.2: removeAt(int index) // उद्देश्य: specified index पर element को हटाता है। // Return Value: E (हटाया गया element) // Working: यह उस index पर मौजूद element को हटाता है। उसके बाद के elements left में shift होते हैं। // Exception: RangeError यदि index वैध नहीं है। print("\n3.2: removeAt(int index)"); String removedFruit = fruits.removeAt(0); // index 0 पर 'Orange' था print("Index 0 से '$removedFruit' हटाया गया, 'fruits' अब: $fruits"); // Output: Orange, [Lemon, Lime, Banana, Cherry, Mango, Pineapple] // fruits.removeAt(10); // Uncomment this to see RangeError // 3.3: removeWhere(bool test(E element)) // उद्देश्य: List से उन सभी elements को हटाता है जो दिए गए test function के लिए true return करते हैं। // Return Value: void // Working: यह एक efficient तरीका है elements को conditionaly हटाने का। print("\n3.3: removeWhere(bool test(E element))"); // 'Lime' को हटाएँ fruits.removeWhere((fruit) => fruit == "Lime"); print("'Lime' हटाने के बाद 'fruits': $fruits"); // Output: [Lemon, Banana, Cherry, Mango, Pineapple] // उन सभी फलों को हटाएँ जिनमें 'e' है fruits.removeWhere((fruit) => fruit.contains('e')); print("'e' वाले फल हटाने के बाद 'fruits': $fruits"); // Output: [Banana, Mango, Pineapple] // 3.4: retainWhere(bool test(E element)) // उद्देश्य: List से उन सभी elements को हटाता है जो दिए गए test function के लिए *false* return करते हैं। // यानी, केवल वही elements बचते हैं जो test function के लिए true return करते हैं। // Return Value: void // Working: यह `removeWhere` का उल्टा है। print("\n3.4: retainWhere(bool test(E element))"); List<String> tempFruits = ["Apple", "Banana", "Cherry", "Mango", "Date"]; print("Original 'tempFruits': $tempFruits"); tempFruits.retainWhere((fruit) => fruit.startsWith('B') || fruit.startsWith('M')); print("'B' या 'M' से शुरू होने वाले फलों को retain करने के बाद 'tempFruits': $tempFruits"); // Output: [Banana, Mango] // 3.5: clear() // उद्देश्य: List से सभी elements को हटाता है। List खाली हो जाती है। // Return Value: void // Working: List को empty state में लाता है। print("\n3.5: clear()"); fruits.clear(); print("'fruits' को clear करने के बाद: $fruits"); // Output: [] print("\n--- Step 4: Elements को Access/Modify करना (Accessing/Modifying Elements) ---"); // List को फिर से भरें for demonstration fruits.addAll(["Apple", "Banana", "Cherry", "Date", "Elderberry"]); print("वर्तमान 'fruits' List: $fruits"); // Output: [Apple, Banana, Cherry, Date, Elderberry] // 4.1: Accessing elements using the [] operator (get) // उद्देश्य: specified index पर element को retrieve करता है। // Return Value: E (index पर मौजूद element) // Working: सीधे उस index पर मौजूद element को देता है। // Exception: RangeError यदि index वैध नहीं है। print("\n4.1: Accessing elements using [] operator"); String firstFruit = fruits[0]; String thirdFruit = fruits[2]; print("Index 0 पर: $firstFruit"); // Output: Apple print("Index 2 पर: $thirdFruit"); // Output: Cherry // 4.2: Modifying elements using the []= operator (set) // उद्देश्य: specified index पर element को नए element से replace करता है। // Return Value: void (Dart में operator के रूप में होता है, सीधे कोई value return नहीं करता) // Working: यह पुराने element को हटाकर नए element को उसी position पर रखता है। List का length नहीं बदलता। // Exception: RangeError यदि index वैध नहीं है। print("\n4.2: Modifying elements using []= operator"); String oldFruitValue = fruits[1]; // पुरानी value retrieve करें अगर ज़रूरत हो fruits[1] = "Grape"; // Banana को Grape से replace करो print("Index 1 पर '$oldFruitValue' को 'Grape' से बदला गया। 'fruits' अब: $fruits"); // Output: [Apple, Grape, Cherry, Date, Elderberry] print("\n--- Step 5: List की जानकारी प्राप्त करना (Querying List Information) ---"); // 5.1: length property // उद्देश्य: List में elements की संख्या लौटाता है। (यह एक property है, method नहीं) // Return Value: int (elements की संख्या) print("5.1: length property"); int size = fruits.length; print("'fruits' List का length: $size"); // Output: 5 // 5.2: isEmpty and isNotEmpty properties // उद्देश्य: जांचता है कि List खाली है या नहीं। // Return Value: bool (यदि List में कोई element नहीं है तो true) print("\n5.2: isEmpty and isNotEmpty properties"); print("'fruits' List खाली है? ${fruits.isEmpty}"); // Output: false List<String> emptyList = []; print("एक खाली List खाली है? ${emptyList.isEmpty}"); // Output: true print("'fruits' List खाली नहीं है? ${fruits.isNotEmpty}"); // Output: true // 5.3: contains(Object? element) // उद्देश्य: जांचता है कि specified element List में मौजूद है या नहीं। // Return Value: bool (यदि element मौजूद है तो true) // Working: equals() method का उपयोग करके elements की तुलना करता है। print("\n5.3: contains(Object? element)"); print("'fruits' में 'Cherry' है? ${fruits.contains("Cherry")}"); // Output: true print("'fruits' में 'Kiwi' है? ${fruits.contains("Kiwi")}"); // Output: false // 5.4: indexOf(Object? element, [int start = 0]) // उद्देश्य: specified element की पहली occurrence का index लौटाता है। आप optional start index दे सकते हैं। // Return Value: int (index, या -1 यदि element नहीं मिला) print("\n5.4: indexOf(Object? element)"); print("'Cherry' का पहला index: ${fruits.indexOf("Cherry")}"); // Output: 2 print("'Kiwi' का पहला index: ${fruits.indexOf("Kiwi")}"); // Output: -1 // 5.5: lastIndexOf(Object? element, [int? start]) // उद्देश्य: specified element की आखिरी occurrence का index लौटाता है। // Return Value: int (index, या -1 यदि element नहीं मिला) print("\n5.5: lastIndexOf(Object? element)"); fruits.add("Apple"); // एक और Apple जोड़ो print("List में एक और 'Apple' जोड़ने के बाद: $fruits"); print("'Apple' का आखिरी index: ${fruits.lastIndexOf("Apple")}"); // Output: 5 print("'Grape' का आखिरी index: ${fruits.lastIndexOf("Grape")}"); // Output: 1 print("\n--- Step 6: Sublist और List Conversion ---"); // 6.1: sublist(int start, [int? end]) // उद्देश्य: List के एक हिस्से का *नया* List लौटाता है। (यह original List का view नहीं होता, बल्कि एक अलग copy होता है) // Return Value: List<E> (sub-list) // Working: start index (inclusive) से end index (exclusive) तक के elements को शामिल करता है। // यदि end नहीं दिया गया है, तो यह List के अंत तक जाता है। // Exception: RangeError (यदि indices अमान्य हैं) print("6.1: sublist(int start, [int? end])"); List<String> middleFruits = fruits.sublist(1, 4); // Index 1, 2, 3 print("Sub-list (indices 1 to 3): $middleFruits"); // Output: [Grape, Cherry, Date] // Dart में sublist एक copy लौटाता है, Java के List.subList() की तरह view नहीं। // इसलिए, sub-list में बदलाव original List को प्रभावित नहीं करते। middleFruits.remove("Cherry"); print("Sub-list से 'Cherry' हटाने के बाद: $middleFruits"); // Output: [Grape, Date] print("Original 'fruits' List अभी भी वही है: $fruits"); // Output: [Apple, Grape, Cherry, Date, Elderberry, Apple] // 6.2: toList() // उद्देश्य: किसी भी Iterable को List में बदलता है। खासकर अगर आप एक Iterable से एक mutable List copy बनाना चाहते हैं। // Return Value: List<E> print("\n6.2: toList()"); List<String> fruitsCopy = fruits.toList(); // fruits की एक पूरी copy बनाता है print("Fruits की copy 'fruitsCopy': $fruitsCopy"); // Output: [Apple, Grape, Cherry, Date, Elderberry, Apple] fruitsCopy.add("Kiwi"); print("Copy में 'Kiwi' जोड़ने के बाद 'fruitsCopy': $fruitsCopy"); print("Original 'fruits' List अभी भी: $fruits"); // Original unaffected // 6.3: join([String separator = '']) // उद्देश्य: List के सभी elements को एक string में जोड़ता है, एक optional separator के साथ। // Return Value: String print("\n6.3: join([String separator = ''])"); String allFruitsString = fruits.join(", "); print("सभी फलों की string: $allFruitsString"); // Output: Apple, Grape, Cherry, Date, Elderberry, Apple print("\n--- Step 7: List को Iterate करना (Iterating through a List) ---"); // 7.1: For-in loop (सबसे common और readable) print("7.1: For-in loop:"); for (String fruit in fruits) { print("Fruit: $fruit"); } // 7.2: Traditional for-loop (index के साथ) print("\n7.2: Traditional for-loop (by index):"); for (int i = 0; i < fruits.length; i++) { print("Fruit at index $i: ${fruits[i]}"); } // 7.3: using forEach() with a function or lambda expression print("\n7.3: Using forEach() with Lambda:"); fruits.forEach((fruit) { print("ForEach Fruit: $fruit"); }); // Shorter syntax: // fruits.forEach(print); // This also works for simple printing print("\n--- सभी महत्वपूर्ण List methods और properties कवर किए गए! ---"); }Java
Listऔर DartListके बीच मुख्य अंतर:-
Creation (निर्माण):
- Java:
new ArrayList<String>()याnew LinkedList<String>()। - Dart:
[]या<String>[](growable by default),List.filled()for fixed-length.
- Java:
-
Size/Length (आकार/लंबाई):
- Java:
list.size()(method). - Dart:
list.length(property).
- Java:
-
Adding at Index (इंडेक्स पर जोड़ना):
- Java:
list.add(index, element)। - Dart:
list.insert(index, element)।
- Java:
-
Adding Multiple at Index (इंडेक्स पर कई जोड़ना):
- Java:
list.addAll(index, collection)। - Dart:
list.insertAll(index, iterable)।
- Java:
-
Removing at Index (इंडेक्स से हटाना):
- Java:
list.remove(index)। - Dart:
list.removeAt(index)।
- Java:
-
Conditional Removal/Retention (शर्तों के आधार पर हटाना/रखना):
- Java: सीधे
Listinterface में नहीं,Iterator.remove()या Java 8 Streams (filter()) का उपयोग करना पड़ता है। - Dart:
removeWhere()औरretainWhere()methods बहुत powerful और convenient हैं।
- Java: सीधे
-
Accessing/Modifying Elements (एलिमेंट्स को एक्सेस/मॉडिफाई करना):
- Java:
list.get(index)औरlist.set(index, element)। - Dart:
list[index](access) औरlist[index] = value(modify) ऑपरेटर।
- Java:
-
Sublist Behavior (सबलिस्ट का व्यवहार):
- Java:
subList()original List का एक "view" (नया List नहीं) लौटाता है। सबलिस्ट में बदलाव original List को प्रभावित करते हैं। - Dart:
sublist()हमेशा एक नया List copy लौटाता है। सबलिस्ट में बदलाव original List को प्रभावित नहीं करते। यदि आपको Java की तरह view चाहिए, तो Dart में ऐसा सीधे List से नहीं होता, आपको customized solutions की आवश्यकता हो सकती है।
- Java:
-
Array Conversion (एरे रूपांतरण):
- Java:
toArray()methods। - Dart:
toList()(किसी भी Iterable को List में बदलने के लिए) औरjoin()(List को String में बदलने के लिए)।
- Java:
Dart
Listmodern और ergonomic API के साथ आता है जो इसे Dart applications में data management के लिए एक शक्तिशाली और flexible choice बनाता है।-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.