Array in Java
In Java, an array is a collection of elements of the same type, stored in contiguous memory locations, and each element can be accessed using an index. The index starts from 0, meaning the first element is at index 0, the second at index 1, and so on.
Array Declaration in Java
You can declare arrays in C# using the following syntax:
int[] array1; // Declares an array of integers
double[] array2; // Declares an array of doubles
boolean[] array3; // Declares an array of Booleans
Initializing Arrays in Java Once declared, you can initialize arrays with a specific size or directly with elements:
// Array with a defined size but no elements yet
int[] array1 = new int[5]; // array1 can hold 5 integers, all initialized to 0
// Array initialized with specific elements
int[] array2 = new int[] { 4, 5, 1, 7, 3 }; // array2 has 5 elements
boolean[] array3 = { true, false, true, false, true }; // array3 has 5 elements
// Simplified initialization
int[] array2 = { 4, 5, 1, 7, 3 }; // Same as above
Understanding Array Declaration and Initialization
int[] array1 = new int[5];
This declares an integer array named array1 with 5 elements, initialized to 0 by default.
int[] array2 = new int[] { 4, 5, 1, 7, 3 };
This declares an integer array array2 with 5 specific elements: 4, 5, 1, 7, 3.
boolean[] array3 = { true, false, true, false, true };
This declares a boolean array array3 with 5 specific elements: true, false, true, false, true.
Example: Creating and Accessing an Array
Let’s create a simple integer array with values and demonstrate how to access a specific element by its index.
Scenario: Create an array with values {2, 4, 10, 5, 15, 3} and print the 3rd element (index 2).
public class ArrayExample {
public static void main(String[] args) {
// Initializing an integer array
int[] numbers = { 2, 4, 10, 5, 15, 3 };
// Accessing and printing the 3rd element (at index 2)
System.out.println(“The third element: ” + numbers[2]);
}
}
Output :
The third element: 10
Explanation:
int[] numbers = { 2, 4, 10, 5, 15, 3 };
Here, we are creating an array named numbers with 6 elements: 2, 4, 10, 5, 15, 3.
System.out.println(“The third element: ” + numbers[2]);
We access the 3rd element in the array by using index 2 (since array indexing starts from 0). So, numbers[2] gives 10, which is displayed in the console
Output:
The third element: 10
Task:
1. Single-Dimensional Array: Create a single-dimensional array of integers with 5 elements. Initialize the array with values from 1 to 5. Print all elements of the array.
2. String Array: Create a single-dimensional array of strings with 3 elements. Initialize the array with the names of three fruits. Print all elements of the array.
3. Double Array: Create a single-dimensional array of doubles with 4 elements. Initialize the array with any 4 double values. Print the sum of all elements in the array.
4. Boolean Array: Create a single-dimensional array of booleans with 3 elements. Initialize the array with alternating true and false values. Print all elements of the array.
A data structure to store multiple values of the same type in a single variable.
int[] numbers;
Visit the training page and follow the step-by-step lessons with examples.
Yes, it includes examples for single-dimensional, multi-dimensional, and object arrays.
No, this training focuses on free learning without certification.
Yes, the training covers modern Java syntax and practices for arrays.
The training covers array declaration, initialization, multi-dimensional arrays, and arrays of objects.
Basic Java knowledge is helpful but not mandatory as the training starts from fundamentals.