EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Java

Java Arrays

Operators are the foundation of any programming language, enabling us to perform various computations, logic evaluations, and functions. Java offers several types of operators that are categorized based on the functions they perform.

Array Initialization in C

Initialization in C involves assigning initial values to variables. When an array is declared or memory is allocated for it, its elements initially contain garbage values. To give these elements meaningful values, we initialize the array. There are several methods for initializing an array in C.

1. Array Initialization During Declaration :This method involves initializing the array at the time of its declaration. We use an initializer list, which is a set of values enclosed in curly braces { } and separated by commas, to assign values to the array.

Examples:

int[] numbers; // Declaring an integer array

This declares an integer array numbers. However, it does not allocate memory yet.

2. Creating an Array :Allocate memory using the new keyword:

numbers = new int[5]; // Creates an array of 5 integers

This initializes the array to hold 5 integers, with default values of 0.

3. Accessing Array Elements :You can access and modify array elements using their index (starting from 0):

numbers[0] = 10; // Setting the first element to 10
int firstElement = numbers[0]; // Accessing the first element

4. Changing Array Elements :To update an element, assign a new value:

numbers[0] = 20; // Changes the first element to 20

5. Array Length :You can get the length of an array using its length property:

int length = numbers.length; // Returns 5 for this array

6. Array Initialization :You can declare and initialize arrays in different ways:

int[] numbers = new int[5]; // Declaration + memory allocation
int[] otherNumbers = {1, 2, 3, 4, 5}; // Array literal initialization

7. Iterating Through Arrays :You can loop through arrays using a for loop or for-each loop:

for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}

for (int number : numbers) {
    System.out.println(number); // For-each loop
}

8. Multidimensional Arrays :Java supports multidimensional arrays, like 2D arrays (matrix):

int[][] matrix = new int[3][3]; // 2D array
matrix[0][0] = 1;

Example:

int[][] multiDimArray = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
}; // A 3x3 matrix

9. Array of Objects ;Arrays can also hold objects of a class:

Student[] students = new Student[3]; // Array of Student objects
students[0] = new Student("John", 1);

Output:

num1 = 20
num2 = 10
The product = 200

10. Passing Arrays to Methods :You can pass arrays to methods:

public class MultiplicationExample {
    public static void main(String[] args) {
        int num1 = 20, num2 = 10;
        int product = num1 * num2;
        System.out.println("The product = " + product);
    }
}

Calling the method:

printArray(numbers);

11. Returning Arrays from Methods :A method can return an array:

public static int[] getArray() {
    return new int[] {1, 2, 3};
}

12. Cloning Arrays :To clone an array (create a shallow copy):

int[] cloneArray = numbers.clone();

For multidimensional arrays, the cloning creates a shallow copy, meaning only the references are copied, not the actual elements.

13. Handling Array Exceptions :If you try to access an element outside the array size, Java throws an ArrayIndexOutOfBoundsException:

System.out.println(numbers[5]); // Throws an exception if the array has fewer elements
End of lesson.