इस ट्यूटोरियल में, आप विभिन्न तरीकों के बारे में जानेंगे, जिनका उपयोग आप उदाहरणों की मदद से जावा में सरणियों (एक आयामी और दो-आयामी) की नकल करने के लिए कर सकते हैं।
जावा में, हम एक सरणी को दूसरे में कॉपी कर सकते हैं। कई तकनीकें हैं जिनका उपयोग आप जावा में सरणियों को कॉपी करने के लिए कर सकते हैं।
1. असाइनमेंट ऑपरेटर का उपयोग करके एरे को कॉपी करना
एक उदाहरण लेते हैं,
class Main ( public static void main(String() args) ( int () numbers = (1, 2, 3, 4, 5, 6); int () positiveNumbers = numbers; // copying arrays for (int number: positiveNumbers) ( System.out.print(number + ", "); ) ) )
आउटपुट :
1, 2, 3, 4, 5, 6
उपर्युक्त उदाहरण में, हमने असाइनमेंट ऑपरेटर ( =
) का उपयोग पॉजिटिव नॉटल्स नामक एक और ऐरे को संख्याओं को कॉपी करने के लिए किया है।
यह तकनीक सबसे आसान है और यह काम भी करती है। हालाँकि, इस तकनीक के साथ एक समस्या है। यदि हम एक सरणी के तत्वों को बदलते हैं, तो अन्य सरणियों के संबंधित तत्व भी बदलते हैं। उदाहरण के लिए,
class Main ( public static void main(String() args) ( int () numbers = (1, 2, 3, 4, 5, 6); int () positiveNumbers = numbers; // copying arrays // change value of first array numbers(0) = -1; // printing the second array for (int number: positiveNumbers) ( System.out.print(number + ", "); ) ) )
आउटपुट :
-1, 2, 3, 4, 5, 6
यहां, हम देख सकते हैं कि हमने संख्या सरणी का एक मान बदल दिया है। जब हम पॉज़िटिव न्यूट्रन्स ऐरे को प्रिंट करते हैं, तो हम देख सकते हैं कि समान मान भी बदल गया है।
ऐसा इसलिए है क्योंकि दोनों सरणियाँ समान सरणी ऑब्जेक्ट को संदर्भित करती हैं। इसकी वजह उथली नकल है। उथले प्रति के बारे में अधिक जानने के लिए, उथले प्रति पर जाएँ।
अब, सरणियों की प्रतिलिपि बनाते समय नई सरणी वस्तुएं बनाने के लिए, हमें उथली प्रति के बजाय गहरी प्रति चाहिए।
2. नकल करने के लिए लूप कंस्ट्रक्शन का उपयोग करना
आइए एक उदाहरण लेते हैं:
import java.util.Arrays; class Main ( public static void main(String() args) ( int () source = (1, 2, 3, 4, 5, 6); int () destination = new int(6); // iterate and copy elements from source to destination for (int i = 0; i < source.length; ++i) ( destination(i) = source(i); ) // converting array to string System.out.println(Arrays.toString(destination)); ) )
आउटपुट :
(1, 2, 3, 4, 5, 6)
उपरोक्त उदाहरण में, हमने for
स्रोत सरणी के प्रत्येक तत्व के माध्यम से लूप का उपयोग किया है । प्रत्येक पुनरावृत्ति में, हम स्रोत सरणी से गंतव्य सरणी तक तत्वों की प्रतिलिपि बना रहे हैं।
यहां, स्रोत और गंतव्य सरणी अलग-अलग ऑब्जेक्ट (गहरी प्रतिलिपि) को संदर्भित करते हैं। इसलिए, यदि एक सरणी के तत्वों को बदल दिया जाता है, तो किसी अन्य सरणी के संबंधित तत्व अपरिवर्तित होते हैं।
कथन पर ध्यान दें,
System.out.println(Arrays.toString(destination));
यहाँ, किसी स्ट्रिंग को एक स्ट्रिंग में बदलने के लिए toString () विधि का उपयोग किया जाता है। अधिक जानने के लिए ,String () विधि (आधिकारिक जावा प्रलेखन) पर जाएं।
3. सरणी () विधि का उपयोग करके सरणी को कॉपी करना
जावा में, सिस्टम क्लास में ऐरे arraycopy()
को कॉपी करने के लिए एक विधि होती है । यह विधि उपरोक्त दोनों की तुलना में सरणियों को कॉपी करने का एक बेहतर तरीका है।
arraycopy()
विधि आप गंतव्य सरणी स्रोत सरणी के एक निर्धारित हिस्से को कॉपी करने की अनुमति देता है। उदाहरण के लिए,
arraycopy(Object src, int srcPos,Object dest, int destPos, int length)
यहाँ,
- src - स्रोत सरणी जिसे आप कॉपी करना चाहते हैं
- srcPos - स्रोत सरणी में स्थिति (सूचकांक) शुरू करना
- गंतव्य - गंतव्य सरणी जहां तत्वों को स्रोत से कॉपी किया जाएगा
- डेस्टोस - गंतव्य सरणी में स्थिति (सूचकांक) शुरू करना
- लंबाई - कॉपी करने के लिए तत्वों की संख्या
आइए एक उदाहरण लेते हैं:
// To use Arrays.toString() method import java.util.Arrays; class Main ( public static void main(String() args) ( int() n1 = (2, 3, 12, 4, 12, -2); int() n3 = new int(5); // Creating n2 array of having length of n1 array int() n2 = new int(n1.length); // copying entire n1 array to n2 System.arraycopy(n1, 0, n2, 0, n1.length); System.out.println("n2 = " + Arrays.toString(n2)); // copying elements from index 2 on n1 array // copying element to index 1 of n3 array // 2 elements will be copied System.arraycopy(n1, 2, n3, 1, 2); System.out.println("n3 = " + Arrays.toString(n3)); ) )
आउटपुट :
n2 = (2, 3, 12, 4, 12, -2) n3 = (0, 12, 4, 0, 0)
उपरोक्त उदाहरण में, हमने arraycopy()
विधि का उपयोग किया है,
System.arraycopy(n1, 0, n2, 0, n1.length)
- n1 सरणी से संपूर्ण तत्वों को n2 सरणी में कॉपी किया जाता हैSystem.arraycopy(n1, 2, n3, 1, 2)
सूचकांक 2 से शुरू होने वाले एन 1 सरणी के 2 तत्वों को एन 3 सरणी के 1 से शुरू होने वाले सूचकांक में कॉपी किया जाता है
जैसा कि आप देख सकते हैं, एक इंट प्रकार सरणी के तत्वों का डिफ़ॉल्ट प्रारंभिक मान 0 है।
4. कॉपीऑफआरंगे () पद्धति का उपयोग करके एरे को कॉपी करना
We can also use the copyOfRange() method defined in Java Arrays class to copy arrays. For example,
// To use toString() and copyOfRange() method import java.util.Arrays; class ArraysCopy ( public static void main(String() args) ( int() source = (2, 3, 12, 4, 12, -2); // copying entire source array to destination int() destination1 = Arrays.copyOfRange(source, 0, source.length); System.out.println("destination1 = " + Arrays.toString(destination1)); // copying from index 2 to 5 (5 is not included) int() destination2 = Arrays.copyOfRange(source, 2, 5); System.out.println("destination2 = " + Arrays.toString(destination2)); ) )
Output
destination1 = (2, 3, 12, 4, 12, -2) destination2 = (12, 4, 12)
In the above example, notice the line,
int() destination1 = Arrays.copyOfRange(source, 0, source.length);
Here, we can see that we are creating the destination1 array and copying the source array to it at the same time. We are not creating the destination1 array before calling the copyOfRange()
method. To learn more about the method, visit Java copyOfRange.
5. Copying 2d Arrays Using Loop
Similar to the single-dimensional array, we can also copy the 2-dimensional array using the for
loop. For example,
import java.util.Arrays; class Main ( public static void main(String() args) ( int()() source = ( (1, 2, 3, 4), (5, 6), (0, 2, 42, -4, 5) ); int()() destination = new int(source.length)(); for (int i = 0; i < destination.length; ++i) ( // allocating space for each row of destination array destination(i) = new int(source(i).length); for (int j = 0; j < destination(i).length; ++j) ( destination(i)(j) = source(i)(j); ) ) // displaying destination array System.out.println(Arrays.deepToString(destination)); ) )
Output:
((1, 2, 3, 4), (5, 6), (0, 2, 42, -4, 5))
In the above program, notice the line,
System.out.println(Arrays.deepToString(destination);
यहां, इस deepToString()
पद्धति का उपयोग 2-आयामी सरणी का बेहतर प्रतिनिधित्व प्रदान करने के लिए किया जाता है। अधिक जानने के लिए, Java deepToString () पर जाएँ।
एरेस्कोपी () का उपयोग करते हुए 2d एरर कॉपी करना
उपरोक्त कोड को और सरल बनाने के लिए, हम आंतरिक लूप को System.arraycopy()
एकल-आयामी सरणी के मामले में बदल सकते हैं । उदाहरण के लिए,
import java.util.Arrays; class Main ( public static void main(String() args) ( int()() source = ( (1, 2, 3, 4), (5, 6), (0, 2, 42, -4, 5) ); int()() destination = new int(source.length)(); for (int i = 0; i < source.length; ++i) ( // allocating space for each row of destination array destination(i) = new int(source(i).length); System.arraycopy(source(i), 0, destination(i), 0, destination(i).length); ) // displaying destination array System.out.println(Arrays.deepToString(destination)); ) )
आउटपुट :
((1, 2, 3, 4), (5, 6), (0, 2, 42, -4, 5))
यहां, हम देख सकते हैं कि हम विधि के for
साथ आंतरिक लूप को बदलकर एक ही आउटपुट प्राप्त करते हैं arraycopy()
।