Array
Array
2. Initialization of Array
arrayName = new dataType[size];
~ Example:
numbers = new int[5];
3. Declaration + Initialization
dataType[] arrayName = new dataType[size];
~ Example:
int[] marks = new int[3];
int[] marks = { 85, 90, 95 };
TYPES OF ARRAY
1. Single-Dimensional Array: A single list of
elements.
2. Multi-Dimensional Array: Arrays with more
than one dimension (like 2D arrays).
1. Single-Dimensional Array
A single-dimensional array is like a list of elements
of the same data type.
Syntax
dataType[] arrayName = new dataType[size];
Example:
int[] numbers = new int[5];
numbers[0] = 10;
numbers[1] = 20;
System.out.println(numbers[0]); // Output: 10
2. Multi-Dimensional Array
A multi-dimensional array is an array of arrays.
It can represent data in a matrix form (e.g., 2D
arrays).
Syntax
dataType[][] arrayName = new dataType[rows][columns];
Example
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
PROBLEMS:
Output: Smallest: 1
Output: Largest: 9
for (int i : a) {
if (i % 2 == 0)
even++;
else
odd++;
}
Output : Even = 2
Odd = 3