BASIC ARRAYS IN JAVA
INTRODUCTION TO ARRAYS IN JAVA
• Arrays: fundamental data structures in Java
• Store multiple elements of the same data type
• Efficient way to handle collections of data
• Two main types: one-dimensional and two-dimensional arrays
ONE-DIMENSIONAL ARRAYS: BASICS
• Also known as linear arrays
• Single row of elements
• Each element accessible by a unique index
Declaration syntax:
dataType[] arrayName;
or
dataType arrayName[];
Example:
int[] numbers;
int numbers[];
INITIALIZING ONE-DIMENSIONAL ARRAYS
Three common methods:
• Using the new keyword:
int[] numbers = new int[5]; // Creates an array of 5 integers
• Initializing with values:
int[] numbers = {1, 2, 3, 4, 5};
• Declaring and then initializing:
int[] numbers;
numbers = new int[]{1, 2, 3, 4, 5};
ACCESSING AND MODIFYING ONE-DIMENSIONAL ARRAYS
• Elements accessed using index (starts at 0)
Example:
int[] numbers = {10, 20, 30, 40, 50};
[Link](numbers[0]); // Outputs: 10
numbers[2] = 35; // Modifies the third element
• Use [Link] to get the size of the array
WORKING WITH ONE-DIMENSIONAL ARRAYS: EXAMPLE
TWO-DIMENSIONAL ARRAYS: BASICS
• Array of arrays or table with rows and columns
• Useful for grid-like structures or matrices
Declaration syntax:
dataType[][] arrayName;
or
dataType arrayName[][];
Example:
int[ ][ ] matrix;
int matrix[ ][ ];
INITIALIZING TWO-DIMENSIONAL ARRAYS
Three common methods:
• Using the new keyword:
int[][] matrix = new int[3][4]; // Creates a 3x4 matrix
• Initializing with values:
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
• Declaring and then initializing:
int[][] matrix;
matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
ACCESSING AND MODIFYING TWO-DIMENSIONAL ARRAYS
• Elements accessed using two indices: row and column
• Both indices start at 0
Example:
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
[Link](matrix[1][2]); // Outputs: 6
matrix[0][1] = 10; // Modifies the element in the first row, second column
WORKING WITH
TWO-DIMENSIONAL ARRAYS:
COMPARISON: ONE-DIMENSIONAL VS TWO-DIMENSIONAL
ARRAYS
Structure:
• One-dimensional: Linear (single row)
• Two-dimensional: Tabular (rows and columns)
Declaration:
• One-dimensional: dataType[ ] arrayName;
• Two-dimensional: dataType[ ][ ] arrayName;
COMPARISON: ONE-DIMENSIONAL VS TWO-DIMENSIONAL
ARRAYS (CONT.)
Initialization:
• One-dimensional: int[] numbers = {1, 2, 3, 4, 5};
• Two-dimensional: int[][] matrix = {{1, 2, 3}, {4, 5, 6}};
Accessing elements:
• One-dimensional: element = array[index];
• Two-dimensional: element = array[row][column];
COMPARISON: ONE-DIMENSIONAL VS TWO-DIMENSIONAL
ARRAYS (CONT.)
Iteration:
• One-dimensional: Single loop
for (int i = 0; i < [Link]; i++) {
// Process array[i]
}
• Two-dimensional: Nested loops
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < array[i].length; j++) {
// Process array[i][j]
}
}
BEST PRACTICES AND TIPS
• Always check array bounds to avoid ArrayIndexOutOfBoundsException
• Use enhanced for-loop (for-each) when possible for cleaner code
• Consider using ArrayList for dynamic sizing
• Be cautious with large arrays to avoid memory issues
• Use meaningful names for arrays and their indices
• Comment your code, especially for complex array manipulations