Array परिचय
In this page:
array = [value1, value2, value3]
element = array[index]
array[index] = new_value
length = len(array)
What is an Array?
एक array एक fixed-size collection है जो contiguous memory में एक जैसे type की कई values store करता है, इसलिए हर value इसकी position उपयोग करके तुरंत मिल सकती है। ज़्यादातर languages में पहला index 0 है, मतलब 5 elements का एक array 0 से 4 तक valid indexes रखता है।
उदाहरण: What is an Array?
#include <iostream>
using namespace std;
int main() {
int scores[4] = {90, 85, 78, 92}; // fixed-size, same type, contiguous
cout << "First element (index 0): " << scores[0] << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int[] scores = {90, 85, 78, 92}; // fixed-size, same type, contiguous
System.out.println("First element (index 0): " + scores[0]);
}
}
scores = [90, 85, 78, 92] # a list; conceptually contiguous, indexed from 0
print("First element (index 0):", scores[0])
#include <stdio.h>
int main() {
int scores[4] = {90, 85, 78, 92}; /* fixed-size, same type, contiguous */
printf("First element (index 0): %d\n", scores[0]);
return 0;
}
Login to try C/C++/Java code in the editor
Array Declaration
एक array declare करना program को बताता है कि यह किस type की value रखेगा और पहले से इसे कितने elements के लिए space reserve करना है। चूंकि एक normal array के लिए creation पर वह size fixed है, बाद में इसे resize करने का आमतौर पर मतलब है एक बिल्कुल नया, बड़ा array बनाना और पुराना data copy करना।
उदाहरण: Array Declaration
#include <iostream>
using namespace std;
int main() {
int numbers[5]; // reserves space for 5 ints upfront, size fixed at creation
for (int i = 0; i < 5; i++) numbers[i] = i * 10;
cout << "numbers[3]: " << numbers[3] << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int[] numbers = new int[5]; // reserves space for 5 ints, size fixed at creation
for (int i = 0; i < 5; i++) numbers[i] = i * 10;
System.out.println("numbers[3]: " + numbers[3]);
}
}
numbers = [0] * 5 # pre-sized list (Python lists can still grow, unlike a real array)
for i in range(5):
numbers[i] = i * 10
print("numbers[3]:", numbers[3])
#include <stdio.h>
int main() {
int numbers[5]; /* reserves space for 5 ints, size fixed at creation */
for (int i = 0; i < 5; i++) numbers[i] = i * 10;
printf("numbers[3]: %d\n", numbers[3]);
return 0;
}
Login to try C/C++/Java code in the editor
Indexing
एक index एक specific element access करने के लिए उपयोग होने वाली numeric position है, और चूंकि arrays memory में elements contiguously store करते हैं, किसी भी valid index पर सीधे jump करना एक O(1) operation है। Valid range (0 से size−1) से बाहर एक index access करना एक out-of-bounds error का कारण बनता है।
उदाहरण: Indexing
#include <iostream>
using namespace std;
int main() {
int arr[] = {100, 200, 300, 400};
cout << "arr[0]: " << arr[0] << endl;
cout << "arr[2]: " << arr[2] << " (O(1) direct jump)" << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int[] arr = {100, 200, 300, 400};
System.out.println("arr[0]: " + arr[0]);
System.out.println("arr[2]: " + arr[2] + " (O(1) direct jump)");
}
}
arr = [100, 200, 300, 400]
print("arr[0]:", arr[0])
print("arr[2]:", arr[2], "(O(1) direct jump)")
#include <stdio.h>
int main() {
int arr[] = {100, 200, 300, 400};
printf("arr[0]: %d\n", arr[0]);
printf("arr[2]: %d (O(1) direct jump)\n", arr[2]);
return 0;
}
Login to try C/C++/Java code in the editor
Array Size
किसी array का size पहले से जानना सही loops लिखने के लिए ज़रूरी है: यह आपको बिल्कुल बताता है कि iterating कहां रोकनी है और आपको गलती से आखिरी valid element से आगे पढ़ने या लिखने से बचने में मदद करता है, जो beginners को मिलने वाले सबसे आम bugs में से एक है।
उदाहरण: Array Size
#include <iostream>
using namespace std;
int main() {
int arr[] = {4, 8, 15, 16, 23};
int size = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < size; i++) cout << arr[i] << " "; // stop exactly at size
cout << endl << "Size: " << size << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int[] arr = {4, 8, 15, 16, 23};
int size = arr.length;
for (int i = 0; i < size; i++) System.out.print(arr[i] + " ");
System.out.println("\nSize: " + size);
}
}
arr = [4, 8, 15, 16, 23]
size = len(arr)
for i in range(size):
print(arr[i], end=" ")
print("\nSize:", size)
#include <stdio.h>
int main() {
int arr[] = {4, 8, 15, 16, 23};
int size = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\nSize: %d\n", size);
return 0;
}
Login to try C/C++/Java code in the editor
Basic Array Practice
Arrays similar values की lists store करने के लिए natural fit हैं, जैसे किसी class के exam marks, एक shopping cart की prices, या ages का एक set। maximum value ढूंढना या total compute करना जैसे simple tasks से शुरू करना वह muscle memory बनाता है जो आपको ज़्यादा advanced array algorithms के लिए चाहिए होगी।
उदाहरण: Basic Array Practice
#include <iostream>
using namespace std;
int main() {
int marks[] = {88, 92, 75, 60, 99};
int n = 5, maxVal = marks[0], total = 0;
for (int i = 0; i < n; i++) {
total += marks[i];
if (marks[i] > maxVal) maxVal = marks[i];
}
cout << "Max: " << maxVal << ", Total: " << total << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
int[] marks = {88, 92, 75, 60, 99};
int maxVal = marks[0], total = 0;
for (int m : marks) {
total += m;
if (m > maxVal) maxVal = m;
}
System.out.println("Max: " + maxVal + ", Total: " + total);
}
}
marks = [88, 92, 75, 60, 99]
print("Max:", max(marks), ", Total:", sum(marks))
#include <stdio.h>
int main() {
int marks[] = {88, 92, 75, 60, 99};
int n = 5, maxVal = marks[0], total = 0;
for (int i = 0; i < n; i++) {
total += marks[i];
if (marks[i] > maxVal) maxVal = marks[i];
}
printf("Max: %d, Total: %d\n", maxVal, total);
return 0;
}
Login to try C/C++/Java code in the editor
nsize वाले array के लिएarr[n]access करना, जब valid indexes0सेn - 1हैं और आखिर से आगे जाना C++ में undefined behavior है।- यह मान लेना कि एक array बढ़ सकता है, जब एक fixed-size array इसके declare होने के बाद resize नहीं हो सकता।
- एक uninitialized array पढ़ना जैसे
int numbers[5];, जिसमें garbage values हैं, zeros नहीं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: