सबलिस्ट () विधि क्या है?
कलेक्शंस फ्रेमवर्क जावा एपीआई में एक बहुत ही लोकप्रिय घटक है। संग्रह ढांचे में सूची इंटरफ़ेस और ArrayList वर्ग संभवतः सबसे महत्वपूर्ण उपकरण हैं। उपसूची सूची इंटरफ़ेस में एक विधि है जो आपको मौजूदा सूची के एक हिस्से से एक नई सूची बनाने देती है। हालाँकि, यह नई बनाई गई सूची मूल सूची के संदर्भ में केवल एक दृश्य है।
ArrayList ऑब्जेक्ट पर सबलिस्ट विधि का उदाहरण।
हम देशों की एक ArrayList की घोषणा कर रहे हैं। फिर हम दूसरे और चौथे तत्वों के बीच के हिस्से को वापस करने का प्रयास करते हैं।
import java.util.*;
public class Main {
public static void main(String[] args) {
// create an ArrayList
ArrayList list = new ArrayList();
// add values to ArrayList
list.add("USA");
list.add("UK");
list.add("France");
list.add("Germany");
list.add("Russia");
System.out.println("List of the countries:" + list);
//Return the subList : 1 inclusive and 3 exclusive
ArrayList new_list = new ArrayList(list.subList(1, 3));
System.out.println("The subList of the list: "+new_list);
}
}
उपरोक्त कोड का आउटपुट होगा
देशों की सूची: [यूएसए, यूके, फ्रांस, जर्मनी, रूस] सूची की उपसूची: [यूके, फ्रांस]
एक ArrayList में, पहले तत्व का सूचकांक मान 0 है। इसलिए, दूसरे और चौथे तत्व के सूचकांक मान क्रमशः 1 और 3 हैं। इसलिए, हम sublist() पद्धति को list.subList(1, 3) के रूप में आमंत्रित करते हैं । हालाँकि, याद रखें कि सबलिस्ट विधि इस मामले में चौथा तत्व ("जर्मनी") toIndex को छोड़कर भाग लौटाती है। इस प्रकार यह केवल "यूके" और "फ्रांस" का उत्पादन करेगा । चूंकि लौटाया गया आउटपुट एक सूची ही है, आप किसी भी सूची विधियों को सीधे उस पर कॉल कर सकते हैं। तो क्या होगा यदि हम दोनों पैरामीटर के लिए एक ही इंडेक्स का उपयोग करें? क्या वह इंडेक्स लौटाई गई सूची में शामिल या बहिष्कृत किया जाएगा? चलो पता करते हैं।
//execute subList() method with the same argument for both parameters.
ArrayList new_list2 = new ArrayList(list.subList(3, 3));
System.out.println("The subList of the list: "+new_list2);
आउटपुट है
सूची की उपसूची: [ ]
आउटपुट एक खाली सूची है। भले ही इंडेक्स से चौथा तत्व चुना जाता है, लेकिन सबलिस्ट () विधि इसे हटा देगी क्योंकि यह इंडेक्स भी है।
लिंक्डलिस्ट ऑब्जेक्ट पर सबलिस्ट विधि का उदाहरण।
इस उदाहरण में, हम लिंक्डलिस्ट तत्व पर सबलिस्ट विधि का उपयोग करेंगे। दोबारा, यह इंडेक्स (समावेशी) और इंडेक्स (अनन्य) से निर्दिष्ट इंडेक्स के बीच सूची वापस कर देगा । याद रखें कि हमने कहा था कि सबलिस्ट () विधि द्वारा लौटाई गई सूची केवल एक दृश्य है जिसमें मूल सूची का संदर्भ है। यदि आप उपसूची में कोई परिवर्तन करते हैं, तो यह मूल सूची को भी प्रभावित करेगा। हम इस उदाहरण में उसका भी परीक्षण करेंगे।
import java.util.LinkedList;
import java.util.Iterator;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Create a LinkedList
LinkedList linkedlist = new LinkedList();
// Add elements to LinkedList
for(int i = 0; i<7; i++){
linkedlist.add("Node "+ (i+1));
}
// Displaying LinkedList elements
System.out.println("Elements of the LinkedList:");
Iterator it= linkedlist.iterator();
while(it.hasNext()){
System.out.print(it.next()+ " ");
}
// invoke subList() method on the linkedList
List sublist = linkedlist.subList(2,5);
// Displaying SubList elements
System.out.println("\nElements of the sublist:");
Iterator subit= sublist.iterator();
while(subit.hasNext()){
System.out.print(subit.next()+" ");
}
/* The changes you made to the sublist will affect the original LinkedList
* Let’s take this example - We
* will remove the element "Node 4" from the sublist.
* Then we will print the original LinkedList.
* Node 4 will not be in the original LinkedList too.
*/
sublist.remove("Node 4");
System.out.println("\nElements of the LinkedList LinkedList After removing Node 4:");
Iterator it2= linkedlist.iterator();
while(it2.hasNext()){
System.out.print(it2.next()+" ");
}
}
}
आउटपुट इस तरह दिखेगा:
लिंक्डलिस्ट के तत्व: नोड 1 नोड 2 नोड 3 नोड 4 नोड 5 नोड 6 नोड 7 सबलिस्ट के तत्व: नोड 3 नोड 4 नोड 5 लिंक्डलिस्ट के तत्व नोड 4 को हटाने के बाद लिंक्डलिस्ट: नोड 1 नोड 2 नोड 3 नोड 5 नोड 6 नोड 7
क्या होगा यदि इंडेक्स सबलिस्ट () में बाध्य नहीं हैं?
सबलिस्ट विधि दो प्रकार के अपवाद लौटाती है । आइए उन पर एक नजर डालते हैं। एक स्थिति पर विचार करें यदि निर्दिष्ट इंडेक्स सूची तत्व की सीमा से बाहर हैं (इंडेक्स <0 || से इंडेक्स> आकार) । फिर यह एक IndexOutOfBoundExecption फेंक देगा ।
//using subList() method with fromIndex <0
ArrayList new_list2 = new ArrayList(list.subList(-1, 3));
System.out.println("Portion of the list: "+new_list2);
Exception in thread "main" java.lang.IndexOutOfBoundsException: fromIndex = -1
// using subList() method with toIndex > size
ArrayList new_list2 = new ArrayList(list.subList(3, 6));
System.out.println("Portion of the list: "+new_list2);
Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 6
साथ ही, यदि FromIndex toIndex (fromIndex> toIndex) से अधिक है , तो subList() विधि एक IllegalArgumentException त्रुटि फेंकता है।
//If fromIndex > toIndex
ArrayList new_list2 = new ArrayList(list.subList(5, 3));
System.out.println("Portion of the list: "+new_list2);
Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(5) > toIndex(3)
निष्कर्ष
इस लेख में, हमने सबलिस्ट पद्धति और इसका उपयोग करने के तरीके पर चर्चा की। उपसूची() विधि स्पष्ट श्रेणी संचालन की आवश्यकता को समाप्त करती है (यह एक प्रकार का संचालन है जो आमतौर पर सरणियों के लिए मौजूद होता है)। याद रखने वाली सबसे महत्वपूर्ण बात यह है कि सबलिस्ट विधि एक नया उदाहरण नहीं लौटाती है, लेकिन मूल सूची के संदर्भ में एक दृश्य है। इसलिए, एक ही सूची में सबलिस्ट पद्धति का अधिक उपयोग करने से आपके जावा एप्लिकेशन में थ्रेड अटक सकता है।
अधिक पढ़ना: |
---|
GO TO FULL VERSION