← Back to Core Java Course | Chapter 11: Collections | Lesson 4 of 17

Java का ArrayList

एक ArrayList एक list है जो items जोड़ने या हटाने पर बढ़ती और सिकुड़ती है, एक stretchy shopping list जैसी। आप किसी भी item तक इसकी position से पहुंच सकते हैं।
Syntax
java
ArrayList<Type> list = new ArrayList<>();
list.add(item);
list.get(index);
list.remove(index);
list.size();

Introduction to ArrayList

ArrayList List interface का एक resizable array implementation है — एक plain array के विपरीत, यह elements जोड़ने पर अपने आप बढ़ता है, इसलिए आपको final size पहले से जानने की ज़रूरत नहीं।

उदाहरण: Introduction to ArrayList

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<>(); // grows automatically
		list.add("a");
		list.add("b");
		System.out.println(list);
	}
}

ArrayList Operations

एक ArrayList के आखिर में जोड़ना तेज़ है (amortized O(1)), लेकिन बीच से insert या remove करना धीमा है (O(n)) क्योंकि हर बाद वाले element को list को contiguous रखने के लिए shift करना पड़ता है।

उदाहरण: ArrayList Operations

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<>();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add(1, "inserted"); // slower: shifts elements after index 1
		System.out.println(list);
	}
}

Iterating over an ArrayList

आप एक ArrayList को एक standard for-each loop, एक explicit Iterator, या एक index-based for loop से iterate कर सकते हैं जब आपको current position चाहिए — हर एक अलग situations को सूट करता है इस आधार पर कि आपको mid-iteration list modify करनी है या नहीं।

उदाहरण: Iterating over an ArrayList

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<>();
		list.add("a");
		list.add("b");
		for (String item : list) { // for-each
			System.out.println(item);
		}
		for (int i = 0; i < list.size(); i++) { // index-based
			System.out.println(list.get(i));
		}
	}
}

Common ArrayList Methods

Common ArrayList methods में add(), get(index), remove(), contains(), और size() शामिल हैं — साथ मिलकर वे typical list manipulation के लिए लगभग हर चीज़ cover करते हैं बिना custom logic लिखे।

उदाहरण: Common ArrayList Methods

java
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in list
		ArrayList<String> list = new ArrayList<>();
		list.add("a");
		list.add("b");
		// Print a line to the console
		System.out.println(list.get(0));
		// Print a line to the console
		System.out.println(list.contains("b"));
		System.out.println(list.size());
		list.remove("a");
		System.out.println(list);
	}
}

ArrayList vs Standard Arrays

ArrayList एक plain array की fixed size और थोड़ी बेहतर raw performance को dynamic resizing और एक कहीं richer built-in API से trade करता है — एक plain array सिर्फ तब उपयोग करें जब size सच में fixed हो और performance critical हो।

उदाहरण: ArrayList vs Standard Arrays

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		int[] fixedArray = new int[3]; // fixed size
		ArrayList<Integer> dynamicList = new ArrayList<>(); // resizes as needed
		dynamicList.add(1);
		dynamicList.add(2);
		System.out.println(fixedArray.length);
		System.out.println(dynamicList.size());
	}
}
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. मौजूद न होने वाली एक index पर get() call करना, जो IndexOutOfBoundsException throw करता है।
  2. एक for-each loop में items हटाना, जो ConcurrentModificationException throw करता है।
  3. एक List<Integer> पर remove(1) उपयोग करना और index से हटाना जब आपका मतलब value से था।

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.