← Back to DSA Course | Chapter 2: Arrays | Lesson 1 of 8

Array परिचय

एक array same-size lockers की एक row जैसा है, हर एक अपने नंबर के साथ। अगर आपको उसका नंबर पता हो तो आप सीधे किसी भी locker पर जा सकते हैं।
Syntax
markup
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;
}

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

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

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

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;
}
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. n size वाले array के लिए arr[n] access करना, जब valid indexes 0 से n - 1 हैं और आखिर से आगे जाना C++ में undefined behavior है।
  2. यह मान लेना कि एक array बढ़ सकता है, जब एक fixed-size array इसके declare होने के बाद resize नहीं हो सकता।
  3. एक uninitialized array पढ़ना जैसे int numbers[5];, जिसमें garbage values हैं, zeros नहीं।

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.