Java - Arrays
Arrays are fundamental data structures that allow you to store and manipulate collections of data. We'll explore what arrays are, how to declare and initialize them, access elements, perform common operations, and demonstrate their usage with examples.
What are Arrays?
- Arrays are ordered collections of data.
- Elements in an array are of the same data type.
- Arrays have a fixed size determined at declaration.
One-Dimensional Array
Declaring and Initializing One-Dimensional Arrays
Syntax: data-type[] array-name = new data-type[size]
Example: Declaring and Initializing an Integer Array
class ArraySample {
public static void main(String[] args){
int[] numbers = new int[5]; //Array of integers with size 5
}
}
Example: Initializing an Array with Values
class ArraySample {
public static void main(String[] args){
int[] primeNumbers = {2, 3, 5, 7, 11}; //Array initialized with values
}
}
Accessing Array Elements:
Elements are accessed by their index, starting from 0.
Example: Accessing Array Elements
class OneDimension {
public static void main(String[] args){
int[] primeNumbers = {2, 3, 5, 7, 11}; //Array initialized with values
int firstPrime = primeNumbers[0]; // Accessing the first element (2)
int thirdPrime = primeNumbers[2]; // Accessing the third element (5)
System.out.println("The value of first element is " + firstPrime);
System.out.printlng("The value of third element is " + thirdPrime);
}
}
Modifying Array Elements
Elements can be modified by assigning new values.
Example: Finding the Length of an Array
class OneDimension {
public static void main(String[] args){
int[] primeNumbers = {2, 3, 5, 7, 11}; //Array initialized with values
primeNumbers[1] = 13; // Changing the second element to 13
System.out.println("The value of second element is "primeNumbers[1]);
}
}
Array Length
The length of an array is determined by the number of elements it contains.
Example: Finding the Length of an Array
class OneDimension {
public static void main(String[] args){
int[] primeNumbers = {2, 3, 5, 7, 11}; //Array initialized with values
int length = primeNumbers.length; // Length is 5
System.out.println("The length is " + length);
}
}
Iterating Through Arrays:
Common loop structure like for loops are used for iteration.
class Iteration {
public static void main(String[] args){
int[] primeNumbers = {2, 3, 5, 7, 11}; //Array initialized with values
for (int i = 0; i < primeNumbers.length; i++) {
System.out.printlng(primeNumbers[i]);
}
}
}
Two-Dimensional Arrays
Two-dimensional arrays are data structures that allow you to organize data in a grid or matrix-like structure. We'll cover what two-dimensional arrays are, how to declare and initialize them, access elements, perform common operations, and illustrate their usage with a variety of examples.
What are Two-Dimensional Arrays?
- Two-dimensional arrays are arrays of arrays.
- They are used to represent data in a grid or matrix format.
- Rows and columns define the dimensions of a 2D array.
Declaring and Initializing 2D Arrays
Syntax: data-type[][] array-name = new data-type[rows]>[columns]
Example: Declaring and Initializing an 2D Integer Array
class TwoDimension {
public static void main(String[] args){
int[][] matrix = new int[3][3]; // Creates a 3x3 integer array
}
}
Example: Initializing a 2D Array with Values
class TwoDimension {
public static void main(String[] args){
int[][] matrix = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
}; // Initializes a 3x3 matrix
}
}
Accessing Elements in a 2D Array:
Elements are accessed using two indices: row and column.
Example: Accessing Elements in a 2D Array
class TwoDimension {
public static void main(String[] args){
int[][] matrix = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
}; // Initializes a 3x3 matrix
int element = matrix[0][0]; // Accessing the element at row 0, column 0
System.out.println("Value of Element at Row 0, Column 0" + element);
}
}
Iterating Through 2D Arrays:
Nested loops are used to iterate through rows and columns..
class TwoDimension {
public static void main(String[] args){
int[][] matrix = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
}; // Initializes a 3x3 matrix
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println(); // Move to the next row
}
}
}
Arrays are versatile data structures that find applications in various programming scenarios. Understanding their declaration, initialization, and manipulation is essential for effective Java programming. Practice with arrays to sharpen your programming skills.
Basic Topics
- Introduction to Java
- Data Types and Variables
- User Input
- Conditional Statements
- Loops
- Methods
- Arrays