Array in Java – Types, Syntax & Examples जावा में ऐरे
In Java, an array is a collection of similar data types stored in contiguous memory locations. Each element in an array is accessed using an index, which starts from 0
and goes up to arraySize - 1
. The array name acts as the base address for the array. Arrays are also known as subscripted variables in programming. Java arrays are of two main types:
जावा में, ऐरे (Array) एक ही प्रकार के डेटा एलेमेंट्स का समूह होता है जिसे कंप्यूटर मेमोरी में लगातार (contiguous) बाइट्स पर स्टोर किया जाता है। प्रत्येक एलिमेंट को इंडेक्स के माध्यम से एक्सेस किया जाता है जो 0 से arraySize - 1
तक होता है। ऐरे का नाम ही उसका बेस एड्रेस होता है। इन्हें सबस्क्रिप्टेड वेरिएबल्स भी कहा जाता है। जावा ऐरे के दो मुख्य प्रकार होते हैं:
🔸 a) One-Dimensional Array (एकविमीय ऐरे – 1D)
A 1D array stores elements in a single linear list format. It uses one subscript ([]
), hence the name 1D array. All elements are of the same type and stored in consecutive memory locations.
1D ऐरे का उपयोग डेटा को एक लिस्ट के रूप में स्टोर करने के लिए किया जाता है। इसमें केवल एक सबस्क्रिप्ट ([]
) होता है। यह एक ही प्रकार के डेटा को संग्रहित करता है और सभी एलिमेंट्स लगातार क्रम में स्टोर होते हैं।
✅ Syntax (प्रारूप):
data_type[] array_name = new data_type[size]; // Declaration
data_type[] array_name = {value0, value1, ..., valuen}; // Initialization
📘 Example:
int[] empID = new int[5];
int[] empID = new int[] {2, 4, 6, 8, 10};// Accessing array elementsSystem.out.println(empID[0]); // Output: 2System.out.println(empID[1]); // Output: 4
यहाँ empID
ऐरे 5 इन्टिजर वैल्यूज़ स्टोर करता है जिन्हें इंडेक्स द्वारा एक्सेस किया जाता है।
🔹 b) Multi-Dimensional Array (बहुविमीय ऐरे – 2D, 3D...)
Java allows arrays with two or more dimensions using multiple subscripts like [][]
, [][][]
, etc. The most commonly used is the 2D array, which is used to represent tables, matrices, or tabular records with rows and columns.
जावा में आप एक से अधिक सबस्क्रिप्ट []
का उपयोग करके 2D, 3D, 4D... nD ऐरे बना सकते हैं। इन सभी को मिलाकर मल्टीडायमेंशनल ऐरे कहा जाता है। सामान्यतः 2D ऐरे का प्रयोग सारणीबद्ध डेटा जैसे तालिकाएं, रिकॉर्ड्स या मैट्रिक्स को स्टोर करने के लिए किया जाता है।
✅ Syntax (प्रारूप):
data_type[][] array_name = new data_type[rows][columns];
📘 Example:
int[][] marks = new int[3][5]; // 3 rows and 5 columns
इस उदाहरण में marks
एक 2D ऐरे है जिसमें 3 पंक्तियाँ (rows) और 5 स्तम्भ (columns) हैं।
🔺 Note:
कभी भी ऐरे को डिक्लेयर करते समय उसकी साइज न दें जैसे -
int[] arr = new int[];
— यह कंपाइल टाइम एरर देगा।
📏 Array Length vs String Length
In Java, you can use the .length
variable to find the size of an array. But for strings, you use the .length()
method.
जावा में ऐरे की लंबाई पता करने के लिए .length
वेरिएबल का उपयोग किया जाता है, जबकि स्ट्रिंग की लंबाई जानने के लिए .length()
मेथड का उपयोग किया जाता है।
🧪 Program 1: Find Length of an Array
public class ArrayTest {
public static void main(String[] args) {int[] rollNo = new int[] {10, 20, 30};System.out.println("Size of Array rollNo is = " + rollNo.length);}}
📤 Output:
Size of Array rollNo is = 3
🧪 Program 2: Print Elements Using Loop
public class ArrayTest {
public static void main(String[] args) {int[] empId = new int[] {10, 20, 30};for (int i = 0; i < empId.length; i++) {System.out.println(empId[i]);}}}
📤 Output:
10
2030
✅ सारांश तालिका (Summary Table)
ऐरे का प्रकार | उपयोग | एक्सेस |
---|---|---|
1D ऐरे | लिस्ट डेटा | एक इंडेक्स से |
2D ऐरे | तालिका/मैट्रिक्स | दो इंडेक्स से (पंक्ति और स्तम्भ) |
length | ऐरे की साइज जानने के लिए | वेरिएबल |
length() | स्ट्रिंग की लंबाई जानने के लिए | मेथड |
Comments
Post a Comment