In this post, we are going to learn about what is arrays in Java, Syntax for array in Java, How they work, and how it helps us to store multiple elements. We will also go through some simple coding examples to help you understand the concept better.
What is an Array in Java?
An array in Java is a data structure that allows us to store multiple values in a single variable. So Instead of declaring separate variables for each value, we can just create an array to store them all together in a fixed-size list. But in array, we can store only similar type of elements that simply means If we have created an integer array then we are allowed to store only integer type of elements in it and If we try to store other than integer type elements like double or String then we will get an compile time error saying incompatible types.
In Arrays, we can store data like integers, floating-point numbers, strings, or even objects.
How to Declare an Array in Java?
To declare an array in Java, we need to specify the type of elements it will hold, followed by square brackets like this int[], and then the array name. Here is an example of how to declare an integer array:
int[] numbers;
The above line only declares an array of integers named numbers, Now we need to initialize an Array to specify its size.
How to Initialize an Array in Java?
Once we declare an array, we need to initialize it, that means creating the array with a specific size. We can do this by simply using the new keyword followed by the type of the array and the size in square brackets. Here's an example:
numbers = new int[5];
This line will create an array that can hold five integers. We can also declare and initialize an array in a single line as shown below:
int[] numbers = new int[5];
Now we can add, remove, access, or remove elements from an array. But, Remember array index starts from 0 (zero). That means the first element will get stored in 0th index, second will get stored in 1st index, 3rd will get stored in the 2nd index, and so on... As shown in the below figure.
The below example shows you how to store elements in arrays and how to access those elements one by one using a for loop.
public class ArrayExample {
public static void main(String[] args) {
// Declare and initialize an array
int[] numbers = new int[5];
// Assigning values to the array
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Print all the array values using for loop
for(int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}
OUTPUT:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
More Examples of Arrays:
1. Write a Java program to print all even numbers from the array.
In this program, we will create an array and add numbers to it. Then we will print only those numbers that are even. To print only even numbers from the array, we can use an if condition like if(numbers[i] % 2 == 0). This will filter only even numbers, as shown in the example below:
public class EvenArrayExample {
public static void main(String[] args) {
// Declare and initialize an array
int[] numbers = new int[5];
// Assigning values to the array
numbers[0] = 5;
numbers[1] = 10;
numbers[2] = 15;
numbers[3] = 20;
numbers[4] = 25;
// Print only even numbers in the array
for(int i = 0; i < numbers.length; i++) {
if(numbers[i]%2 == 0)
System.out.println(numbers[i]);
}
}
}
OUTPUT:
10
20
2. Write a Java program to print array elements in reverse order.
To print array elements in reverse order, we will use a for loop that iterates from the last element to the first element of an array.
public class ReverseArrayExample {
public static void main(String[] args) {
// Declare and initialize an array
int[] numbers = new int[5];
// Assigning values to the array
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Prints array elements in reverse order
for(int i = numbers.length-1; i >= 0 ; i--) {
System.out.print(numbers[i]+ " ");
}
}
}
OUTPUT:
50 40 30 20 10
3. Write a Java program to print max and min element from the array.
Below code will print the value of max and min element from the array:
public class MaxMinArrayExample {
public static void main(String[] args) {
// Declare and initialize an array
int[] numbers = new int[5];
// Assigning values to the array
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
//assume first element is both max and min
int max = numbers[0];
int min = numbers[0];
// Prints max and min number from array
for(int i = 1; i < numbers.length ; i++) {
if(numbers[i] > max){
max = numbers[i];
}
if(numbers[i] < min){
min = numbers[i];
}
}
System.out.println("Max element from array is: "+max);
System.out.println("Min element from array is: "+min);
}
}
OUTPUT:
Max element from array is: 50
Min element from array is: 10
Array Practice Questions For You:
Arrays are very important in interviews. If you are preparing for Java interviews, there is a high chance you will face multiple questions on arrays. So, here is a list of array practice questions you can try to solve to improve your problem-solving skills.
- Write a program to find the sum and average of all elements in an integer array.
- Write a program to copy elements from one array to another.
- Write a program to remove duplicate elements from the given array.
- Write a program to sort an array of integers in ascending or descending order.
- Write a program to find the second largest element in given array.
- Write a program to merge two arrays into a third array.
- Write a program to shift all elements of an array to the right by one position.
- Write a program to rotate the elements of an array to the left by a given number of positions.
- Write a program to check if one array is a subset of another array.
- Write a program to find all pairs of elements in an array whose sum is equal to a given number.
Thanks for reading this article. If you found it helpful, please share it with your friends and colleagues.
Happy Coding... 😊
No comments:
Post a Comment
Share your thoughts or ask questions below!...👇