B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Java Arrays | Login Technologies

Java Arrays

What is an Array?
An array in Java is a container object that holds a fixed number of values of a single type. The length of an array is established when it is created and cannot be changed.

Array क्या है?
Java में Array एक container object होता है जो एक ही प्रकार के fixed संख्या में values को रखता है। Array की length बनाते समय तय होती है और बाद में बदली नहीं जा सकती।

Why use Arrays?
Arrays allow you to store multiple values in a single variable, instead of declaring separate variables for each value.

Arrays क्यों उपयोग करें?
Arrays से आप कई values को एक ही variable में स्टोर कर सकते हैं, बजाय हर value के लिए अलग variable बनाने के।

Advantages of Arrays:

  • Store multiple values of the same type easily.
  • Access elements using index efficiently.
  • Useful for iterative processing using loops.
  • Memory is contiguous, making access faster.

Arrays के फायदे:

  • एक ही प्रकार के कई values आसानी से स्टोर कर सकते हैं।
  • Index से elements तक तेज़ी से पहुँच।
  • Loops के साथ आसानी से process कर सकते हैं।
  • मेमोरी continuous होती है, इसलिए एक्सेस फास्ट होता है।

Syntax to Declare and Initialize Array:
dataType[] arrayName = new dataType[size];
or
dataType[] arrayName = {value1, value2, ..., valueN};

Array Declare और Initialize करने का Syntax:
dataType[] arrayName = new dataType[size];
या
dataType[] arrayName = {value1, value2, ..., valueN};

Example 1: Print Array Elements
public class ArrayExample1 {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};
        for(int i = 0; i < numbers.length; i++) {
            System.out.println("Element at index " + i + " : " + numbers[i]);
        }
    }
}

Output:

Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50

Prints all elements of the array.

Array के सभी elements print करता है।

Example 2: Sum of Array Elements
public class ArrayExample2 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        int sum = 0;
        for(int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        System.out.println("Sum = " + sum);
    }
}

Output: Sum = 10

Calculates and prints the sum of elements.

Elements का योग निकालता है और print करता है।

Example 3: Find Maximum Element
public class ArrayExample3 {
    public static void main(String[] args) {
        int[] arr = {12, 34, 56, 23, 45};
        int max = arr[0];
        for(int i = 1; i < arr.length; i++) {
            if(arr[i] > max) {
                max = arr[i];
            }
        }
        System.out.println("Maximum = " + max);
    }
}

Output: Maximum = 56

Finds and prints the maximum value.

Array में सबसे बड़ा element print करता है।

Example 4: Find Minimum Element
public class ArrayExample4 {
    public static void main(String[] args) {
        int[] arr = {12, 34, 56, 23, 45};
        int min = arr[0];
        for(int i = 1; i < arr.length; i++) {
            if(arr[i] < min) {
                min = arr[i];
            }
        }
        System.out.println("Minimum = " + min);
    }
}

Output: Minimum = 12

Finds and prints the minimum value.

Array में सबसे छोटा element print करता है।

Example 5: Search Element in Array
public class ArrayExample5 {
    public static void main(String[] args) {
        int[] arr = {5, 10, 15, 20, 25};
        int target = 15;
        boolean found = false;
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] == target) {
                found = true;
                break;
            }
        }
        if(found) {
            System.out.println("Element " + target + " found.");
        } else {
            System.out.println("Element not found.");
        }
    }
}

Output: Element 15 found.

Searches for an element and prints if found or not.

Element खोजता है और बताता है कि मिला या नहीं।

Example 6: Copy Array Elements
public class ArrayExample6 {
    public static void main(String[] args) {
        int[] source = {10, 20, 30};
        int[] dest = new int[source.length];
        for(int i = 0; i < source.length; i++) {
            dest[i] = source[i];
        }
        for(int i = 0; i < dest.length; i++) {
            System.out.print(dest[i] + " ");
        }
    }
}

Output: 10 20 30

Copies elements from one array to another and prints the destination array.

एक array के elements दूसरे array में copy करता है और print करता है।