← Back to Core Java Course | Chapter 5: Methods & Arrays | Lesson 8 of 10

Java Arrays परिचय

एक array boxes की एक row है जो एक जैसी तरह की value रखते हैं, हर एक 0 से शुरू होने वाले एक number के साथ। यह lockers की एक row जैसी है जिसे आप number से खोलते हैं।
Syntax
java
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

java
// 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);
	}
}

Initializing Arrays

Array literals आपको एक line में एक array declare, allocate, और populate करने देते हैं, जैसे int[] nums = {1, 2, 3};, जो array बनाने और हर index को अलग से assign करने से कहीं ज़्यादा concise है।

यह shorthand सिर्फ declaration के point पर काम करता है, बाद के एक assignment में नहीं।

उदाहरण: Initializing Arrays

java
// 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]);
	}
}

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

java
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
	}
}

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

java
// 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);
		}
	}
}

Array Properties

length field (एक method नहीं — कोई parentheses नहीं) बताता है कि एक array में कितने elements हैं, जो आप लगातार loop bounds में उपयोग करेंगे।

Java Arrays.copyOf() और System.arraycopy() भी प्रदान करता है array contents को एक नए array object में duplicate या resize करने के लिए।

उदाहरण: Array Properties

java
// 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]);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. arr[arr.length] access करना, जो ArrayIndexOutOfBoundsException throw करता है।
  2. Creation के बाद किसी array का size बदलने की कोशिश करना, क्योंकि यह fixed है।
  3. यह भूल जाना कि objects का एक array null से शुरू होता है, इसलिए एक element पर एक method call करना NullPointerException throw करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.