Java Arrays परिचय
In this page:
dataType[] arrayName = new dataType[size];
dataType[] arrayName = {value1, value2, value3};
arrayName[index] = value;
Declaring and Creating Arrays
एक array elements का एक fixed-size, ordered collection है जो सभी एक जैसा declared type share करते हैं, और इसका size इसके बनने के moment पर lock हो जाता है — एक ArrayList के विपरीत, आप इसे बाद में बढ़ा या सिकोड़ नहीं सकते।
यह fixed size वह है जो array access को बेहद तेज़ बनाता है: JVM किसी भी element का memory offset सीधे इसके index से compute कर सकता है।
उदाहरण: Declaring and Creating Arrays
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare numbers and set it to new int[5]
int[] numbers = new int[5];
// Print a line to the console
System.out.println(numbers.length);
}
}
Login to try C/C++/Java/PHP code in the editor
Initializing Arrays
Array literals आपको एक line में एक array declare, allocate, और populate करने देते हैं, जैसे int[] nums = {1, 2, 3};, जो array बनाने और हर index को अलग से assign करने से कहीं ज़्यादा concise है।
यह shorthand सिर्फ declaration के point पर काम करता है, बाद के एक assignment में नहीं।
उदाहरण: Initializing Arrays
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare nums and set it to {1, 2, 3}
int[] nums = {1, 2, 3};
// Print a line to the console
System.out.println(nums[1]);
}
}
Login to try C/C++/Java/PHP code in the editor
Accessing Array Elements
Array indices 0 से शुरू होते हैं, इसलिए length 5 वाले एक array में valid indices 0 से 4 तक हैं; index 5 (या कोई negative index) मांगना compile होने में fail होने के बजाय runtime पर एक ArrayIndexOutOfBoundsException throw करता है।
यह off-by-one mistake arrays के साथ beginners को मिलने वाले सबसे आम bugs में से एक है।
उदाहरण: Accessing Array Elements
public class Main {
public static void main(String[] args) {
int[] nums = {10, 20, 30, 40, 50};
System.out.println(nums[0]);
System.out.println(nums[4]);
// nums[5] would throw ArrayIndexOutOfBoundsException
}
}
Login to try C/C++/Java/PHP code in the editor
Iterating over Arrays
एक standard for loop आपको index देता है अगर आपको इसकी ज़रूरत हो (मान लें, neighboring elements compare करने के लिए), जबकि एक enhanced for-each loop simpler और safer है जब आपको बस हर value बारी-बारी से चाहिए position track किए बिना।
For-each एक off-by-one indexing bug की possibility से भी पूरी तरह बचता है।
उदाहरण: Iterating over Arrays
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare nums and set it to {1, 2, 3}
int[] nums = {1, 2, 3};
// Loop: repeat while the condition holds
for (int i = 0; i < nums.length; i++) {
// Print a line to the console
System.out.println(nums[i]);
}
for (int n : nums) {
// Print a line to the console
System.out.println(n);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Array Properties
length field (एक method नहीं — कोई parentheses नहीं) बताता है कि एक array में कितने elements हैं, जो आप लगातार loop bounds में उपयोग करेंगे।
Java Arrays.copyOf() और System.arraycopy() भी प्रदान करता है array contents को एक नए array object में duplicate या resize करने के लिए।
उदाहरण: Array Properties
// Import java.util.Arrays so it can be used by its short name
import java.util.Arrays;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare nums and set it to {1, 2, 3}
int[] nums = {1, 2, 3};
// Print a line to the console
System.out.println(nums.length);
// Declare copy and set it to Arrays.copyOf(nums, 3)
int[] copy = Arrays.copyOf(nums, 3);
// Print a line to the console
System.out.println(copy[0]);
}
}
Login to try C/C++/Java/PHP code in the editor
arr[arr.length]access करना, जोArrayIndexOutOfBoundsExceptionthrow करता है।- Creation के बाद किसी array का size बदलने की कोशिश करना, क्योंकि यह fixed है।
- यह भूल जाना कि objects का एक array
nullसे शुरू होता है, इसलिए एक element पर एक method call करनाNullPointerExceptionthrow करता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: