Arrays in Java
Arrays in Java
Arrays in Java
An array is a data structure that can hold multiple values of the same type. It's useful when you need to
store a collection of data, but you don't want to create a variable for each element.
Creating an Array
To create an array in Java, you need to:
1. Declare the array.
2. Instantiate the array.
3. Initialize the array elements.
Example:
// Step 1: Declare an array
Datatype arrayname[];
Datatype[] arrayname;
int[] myArray;
int rollno[];
// Step 2: Instantiate the array(Creation of Memory location)
Arrayname= new datatype[size];
Rollno= new int[30];
Name = new char[30];
myArray = new int[5];
// Step 3: Initialize the array elements
myArray[0] = 10;
myArray[1] = 20;
myArray[2] = 30;
myArray[3] = 40;
myArray[4] = 50;
You can also combine the declaration, instantiation, and initialization in one line
int[] myArray = {10, 20, 30, 40, 50};
int myArray[]={10,20,30,40,50};
Types of Arrays
1. One-Dimensional Arrays A one-dimensional array is a list of elements of the same type. It's the
simplest form of an array.
Example:
int[] numbers = {1, 2, 3, 4, 5};
int number[]={1,2,3,4,5}
2. Two-Dimensional Arrays A two-dimensional array is an array of arrays, creating a matrix-like
structure.
Example:
// Declare a 2D array
int[][] matrix = new int[3][3];
// Initialize the 2D array
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[0][2] = 3;
matrix[1][0] = 4;
matrix[1][1] = 5;
matrix[1][2] = 6;
matrix[2][0] = 7;
matrix[2][1] = 8;
matrix[2][2] = 9;
You can also initialize a 2D array directly:
1
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Strings in Java
Strings in Java are objects that represent sequences of characters. The String class is used to create and
manipulate strings.
Creating a String
You can create a string in several ways:
1. Using string literals:
2
• Strings are immutable objects used to represent sequences of characters, with many built-in
methods for manipulation.
• StringBuffer is a mutable sequence of characters, allowing in-place modification of the string.
Understanding these fundamental concepts and their respective methods is crucial for effective Java
programming.
--------------------------------------------------X-X-X-X-X-X-X-X-X-X-----------------------------------------------
Arrays in Java
An array is a container object that holds a fixed number of values of a single type. The length of an array
is established when the array is created. After creation, its length is fixed.
Types of Arrays
1. One-Dimensional Arrays
2. Two-Dimensional Arrays
1. One-Dimensional Arrays
A one-dimensional array is like a list of elements. It is the simplest form of an array.
Example and Explanation:
public class OneDimensionalArrayExample
{
public static void main(String[] args)
{
// Declare an array of integers
int[] array = new int[5];
// Initialize the array with values
array[0] = 10;
array[1] = 20;
array[2] = 30;
array[3] = 40;
array[4] = 50;
// Print the elements of the array
for (int i = 0; i < array.length; i++) {
System.out.println("Element at index " + i + ": " + array[i]);
}
}
}
Explanation:
• int[] array = new int[5]; declares an array of integers with a size of 5.
• array[0] = 10; initializes the first element of the array with the value 10.
• A for loop is used to iterate through the array and print each element.
2. Two-Dimensional Arrays
A two-dimensional array is like a table with rows and columns. It is often used to represent a matrix.
Example and Explanation:
public class TwoDimensionalArrayExample
{
public static void main(String[] args)
{
// Declare a 2D array of integers
int[][] array = new int[3][3];
// Initialize the 2D array with values
int value = 1;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
3
array[i][j] = value++;
}
}
// Print the elements of the 2D array
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println(); // Print a new line after each row
}
}
}
Explanation:
• int[][] array = new int[3][3]; declares a 2D array of integers with 3 rows and 3 columns.
• Nested for loops are used to initialize the array with values starting from 1.
• Another set of nested for loops is used to print the elements of the 2D array, displaying the array
as a matrix.
----------------------------------------X-X-X-X-X-X-X-X-X-X-X----------------------------------------------------
Example 1: Initialize and Print Array Elements
public class Main {
public static void main(String[] args) {
// Declare and initialize an array of 5 integers
int[] array = {10, 20, 30, 40, 50};
In Java, the length of an array is determined using the length property. Here's a simple explanation and an
example demonstrating how to get the length of both 1D and 2D arrays.
Example: Getting the Length of 1D and 2D Arrays
public class Main {
public static void main(String[] args) {
// Declare and initialize a 1D array
int[] oneDArray = {1, 2, 3, 4, 5};
// Get and print the length of the 1D array
System.out.println("Length of the 1D array: " + oneDArray.length);
// Declare and initialize a 2D array
int[][] twoDArray = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Get and print the number of rows in the 2D array
System.out.println("Number of rows in the 2D array: " + twoDArray.length);
// Get and print the number of columns in each row of the 2D array
for (int i = 0; i < twoDArray.length; i++) {
System.out.println("Number of columns in row " + i + ": " + twoDArray[i].length);
}
}
}
Output
Length of the 1D array: 5
Number of rows in the 2D array: 3
Number of columns in row 0: 3
Number of columns in row 1: 3
Number of columns in row 2: 3
8
Explanation
1. 1D Array Length:
o The length property of the array oneDArray returns the number of elements in the 1D
array.
o oneDArray.length is 5 because the array contains 5 elements.
2. 2D Array Length:
o For a 2D array, length gives the number of rows in the array.
o twoDArray.length is 3 because there are 3 rows in the 2D array.
o To get the number of columns in each row, we use twoDArray[i].length, where i is the
index of the row.
-------------------------------------------X-X-X-X-X-X-X-X-X-X-X----------------------------------------------
14