इस ट्यूटोरियल में, हम जावा में सरणियों के साथ काम करना सीखेंगे। हम उदाहरणों की सहायता से सरणी तत्वों को घोषित करना, आरंभ करना और एक्सेस करना सीखेंगे।
कंप्यूटर प्रोग्रामिंग में, एक सरणी समान प्रकार के डेटा का एक संग्रह है। उदाहरण के लिए, यदि हम 100 लोगों के नाम संग्रहीत करना चाहते हैं तो हम स्ट्रिंग प्रकार की एक सरणी बना सकते हैं जो 100 नामों को संग्रहीत कर सकती है।
String() array = new String(100);
जावा सरणी में मानों की संख्या निश्चित है। यही है, उपरोक्त सरणी 100 से अधिक तत्वों को संग्रहीत नहीं कर सकती है।
कैसे जावा में एक सरणी घोषित करने के लिए?
जावा में, यहां बताया गया है कि हम ऐरे को कैसे घोषित कर सकते हैं।
dataType() arrayName;
- डेटाप्रकार - ऐसा लगता है कि आदिम डेटा प्रकार हो सकता है
int
,char
,double
,byte
, आदि या जावा वस्तुओं - arrayName - यह एक पहचानकर्ता है
उदाहरण के लिए,
double() data;
यहां, डेटा एक सरणी है जो प्रकार के मूल्यों को पकड़ सकता है double
।
लेकिन, कितने तत्व इस पकड़ को बना सकते हैं?
अच्छा प्रश्न! उन तत्वों की संख्या को परिभाषित करने के लिए जो एक सरणी पकड़ सकते हैं, हमें जावा में सरणी के लिए मेमोरी आवंटित करना होगा। उदाहरण के लिए,
// declare an array double() data; // allocate memory data = new Double(10);
यहां, सरणी 10 तत्वों को संग्रहीत कर सकती है । हम यह भी कह सकते हैं कि सरणी का आकार या लंबाई 10 है।
जावा में, हम एक एकल स्टेटमेंट में एक सरणी की मेमोरी को घोषित और आवंटित कर सकते हैं। उदाहरण के लिए,
double() data = new double(10);
जावा में एरियर कैसे आरम्भ करें?
जावा में, हम घोषणा के दौरान सरणियों को आरंभ कर सकते हैं। उदाहरण के लिए,
//declare and initialize and array int() age = (12, 4, 5, 2, 5);
यहाँ, हमने उम्र नाम की एक सरणी बनाई है और इसे घुंघराले कोष्ठक के अंदर मानों के साथ आरंभीकृत किया है।
ध्यान दें कि हमने सरणी का आकार प्रदान नहीं किया है। इस स्थिति में, जावा कंपाइलर सरणी (यानी 5) में तत्वों की संख्या की गणना करके स्वचालित रूप से आकार को निर्दिष्ट करता है।
जावा सरणी में, प्रत्येक मेमोरी लोकेशन एक नंबर से जुड़ा होता है। संख्या को एक सरणी सूचकांक के रूप में जाना जाता है। हम इंडेक्स नंबर का उपयोग करके जावा में एरेज़ को इनिशियलाइज़ भी कर सकते हैं। उदाहरण के लिए,
// declare an array int() age = new int(5); // initialize array age(0) = 12; age(1) = 4; age(2) = 5;…

नोट :
- सरणी सूचकांकों की शुरुआत हमेशा 0. से होती है, अर्थात, सरणी का पहला तत्व इंडेक्स 0 पर है।
- यदि किसी सरणी का आकार n है, तो सरणी का अंतिम तत्व इंडेक्स n-1 पर होगा।
जावा में एक ऐरे के तत्वों को कैसे एक्सेस करें?
हम अनुक्रमणिका संख्या का उपयोग करके एक सरणी के तत्व तक पहुंच सकते हैं। किसी ऐरे के तत्वों को एक्सेस करने के लिए यहाँ सिंटैक्स है,
// access array elements array(index)
Let's see an example of accessing array elements using index numbers.
Example: Access Array Elements
class Main ( public static void main(String() args) ( // create an array int() age = (12, 4, 5, 2, 5); // access each array elements System.out.println("Accessing Elements of Array:"); System.out.println("First Element: " + age(0)); System.out.println("Second Element: " + age(1)); System.out.println("Third Element: " + age(2)); System.out.println("Fourth Element: " + age(3)); System.out.println("Fifth Element: " + age(4)); ) )
Output
Accessing Elements of Array: First Element: 12 Second Element: 4 Third Element: 5 Fourth Element: 2 Fifth Element: 5
In the above example, notice that we are using the index number to access each element of the array.
We can use loops to access all the elements of the array at once.
Looping Through Array Elements
In Java, we can also loop through each element of the array. For example,
Example: Using For Loop
class Main ( public static void main(String() args) ( // create an array int() age = (12, 4, 5); // loop through the array // using for loop System.out.println("Using for Loop:"); for(int i = 0; i < age.length; i++) ( System.out.println(age(i)); ) ) )
Output
Using for Loop: 12 4 5
In the above example, we are using the for Loop in Java to iterate through each element of the array. Notice the expression inside the loop,
age.length
Here, we are using the length
property of the array to get the size of the array.
We can also use the for-each loop to iterate through the elements of an array. For example,
Example: Using the for-each Loop
class Main ( public static void main(String() args) ( // create an array int() age = (12, 4, 5); // loop through the array // using for loop System.out.println("Using for-each Loop:"); for(int a : age) ( System.out.println(a); ) ) )
Output
Using for-each Loop: 12 4 5
Example: Compute Sum and Average of Array Elements
class Main ( public static void main(String() args) ( int() numbers = (2, -9, 0, 5, 12, -25, 22, 9, 8, 12); int sum = 0; Double average; // access all elements using for each loop // add each element in sum for (int number: numbers) ( sum += number; ) // get the total number of elements int arrayLength = numbers.length; // calculate the average // convert the average from int to double average = ((double)sum / (double)arrayLength); System.out.println("Sum = " + sum); System.out.println("Average = " + average); ) )
Output:
Sum = 36 Average = 3.6
In the above example, we have created an array of named numbers. We have used the for… each
loop to access each element of the array.
Inside the loop, we are calculating the sum of each element. Notice the line,
int arrayLength = number.length;
Here, we are using the length attribute of the array to calculate the size of the array. We then calculate the average using:
average = ((double)sum / (double)arrayLength);
As you can see, we are converting the int
value into double
. This is called type casting in Java. To learn more about typecasting, visit Java Type Casting.
Multidimensional Arrays
अब तक हमने जिन एररों का उल्लेख किया है, उन्हें एक आयामी सारणी कहा जाता है। हालांकि, हम जावा में बहुआयामी सरणियों की घोषणा कर सकते हैं।
एक बहुआयामी सरणी सरणियों का एक सरणी है। अर्थात्, बहुआयामी सरणी का प्रत्येक तत्व एक सरणी है। उदाहरण के लिए,
double()() matrix = ((1.2, 4.3, 4.0), (4.1, -1.1) );
यहाँ, हमने एक बहुआयामी सारणी बनाई है जिसका नाम है मैट्रिक्स। यह एक 2-आयामी सरणी है। अधिक जानने के लिए, जावा बहुआयामी सरणी पर जाएँ।
अनुशंसित रीडिंग
- जावा कॉपी ऐरे
- जावा प्रोग्राम एअर प्रिंट करने के लिए
- जावा प्रोग्राम दो एरेट्स को समेटने के लिए
- जावा अर्रे लिस्ट एरे और अर्रे टू अर्रे लिस्ट
- जावा डायनामिक ऐरे