Courses Practice Compete Upgrade to Pro Login Sign Up
All Blogs
Arrays in Java (With Examples and
Practice)
2024-08-07 hrishik85
Table of Contents
1. Introduction
2. Creating Arrays
3. Accessing Array Elements
4. Modifying Array Elements
5. Displaying Array Elements
6. Finding Array Length
7. Inserting Elements into an Array
8. Deleting Elements from an Array
9. Merging Arrays
10. Practice Problems
11. Conclusion
Introduction
Arrays allow you to store multiple items of the same type together, making it easier to manage
and manipulate data. Whether you're working with numbers, strings, or custom objects,
understanding arrays is crucial for becoming a proficient Java developer.
In this blog post, we'll explore the basics of Java arrays, from creation to manipulation, with
plenty of examples and practice opportunities along the way.
Creating Arrays
To create an array in Java, you need to specify two things: the type of elements it will hold and
the number of elements it can contain. Here's how you can create an array:
int[] numbers = new int[5];
This line creates an array called numbers that can hold five integers. The new keyword is used to
allocate memory for the array elements.
You can also create and initialize an array in one step:
int[] numbers = {1, 2, 3, 4, 5};
This creates an array with five elements, each initialized with the corresponding value.
Coding Task
Create an array of the first 5 positive integers and output "Done" to the console once the array
is defined.
Try it on CodeChef
Accessing Array Elements
Each element in an array has an index, starting from 0 for the first element. To access a specific
element, use the array name followed by the index in square brackets:
int[] num = {1, 100, 200};
System.out.println(num[1]); // Outputs: 100
Remember, the index of the last element is always one less than the array's length.
Coding Task
Write a program that outputs the 3rd element from a given array.
Try it on CodeChef
Modifying Array Elements
You can change the value of a specific element by referring to its index:
int[] numbers = {1, 2, 3, 4, 5};
numbers[2] = 10; // Changes the 3rd element to 10
System.out.println(numbers[2]); // Outputs: 10
Coding Task
Update the 3rd month in an array of month names to "Mar" and output the updated element.
Try it on CodeChef
Displaying Array Elements
To display multiple elements from an array, you can use a loop or directly access the elements
you want to show:
String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday"};
System.out.println(days[2]); // Outputs: Wednesday
System.out.println(days[3]); // Outputs: Thursday
Coding Task
Create a string array with the days of the week and output the last two elements on separate
lines.
Try it on CodeChef
Finding Array Length
The length property gives you the number of elements in an array:
String[] chocolate = {"Ferrero Rocher", "Alpenliebe", "Cadbury"};
System.out.println(chocolate.length); // Outputs: 3
Coding Task
Create an integer array with the elements 10, 20, 30, 40, 50, 60, and output the number of
elements in the array.
Try it on CodeChef
Inserting Elements into an Array
Inserting an element into an array requires shifting existing elements to make room. Here's a
step-by-step process:
1. Determine the position for insertion.
2. Shift elements to the right, starting from the last element.
3. Insert the new element into the empty position.
4. Update the array size if necessary.
int[] arr = {1, 2, 3, 4, 5};
int newElement = 10;
int position = 2;
// Shift elements
for (int i = arr.length - 1; i > position; i--) {
arr[i] = arr[i - 1];
}
// Insert new element
arr[position] = newElement;
Deleting Elements from an Array
Deleting an element involves removing it and shifting the remaining elements to fill the gap:
1. Determine the position of the element to delete.
2. Shift elements to the left, starting from the position after the deleted element.
3. Update the array size if necessary.
int[] arr = {1, 2, 3, 4, 5};
int position = 2;
// Shift elements
for (int i = position; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
// Update last element
arr[arr.length - 1] = 0; // Or any default value
Merging Arrays
To merge two arrays, create a new array large enough to hold all elements and copy them over:
int[] arr1 = {1, 2, 3};
int[] arr2 = {4, 5, 6};
int[] merged = new int[arr1.length + arr2.length];
// Copy elements from arr1
for (int i = 0; i < arr1.length; i++) {
merged[i] = arr1[i];
}
// Copy elements from arr2
for (int i = 0; i < arr2.length; i++) {
merged[arr1.length + i] = arr2[i];
}
Try it on CodeChef
Conclusion
We've covered the basics of creating, accessing, modifying, and manipulating arrays. With
practice, you'll become more comfortable using arrays in your Java programs.
Remember, the key to mastering arrays is practice. Try out the coding tasks and practice
problems, and don't hesitate to experiment with your own array manipulations. Happy coding!
We have curated a set of MCQ and coding problems on Arrays in Java. These problems will help
you in solidifying your knowledge of Java Arrays. Start solving these problems now!
You can also check our complete Learn Java course if you want to learn Java in the most fun and
engaging way possible.
Learning Courses Practice Paths Online Compilers Miscellaneous
Learn Python Practice Python Python online compiler Blogs
Learn JavaScript Practice JavaScript C online compiler CodeChef For Colleges
Learn C++ Practice C++ C++ online compiler Coding Contests
Learn C Practice C Java online compiler About Us
Learn Java Practice Java JavaScript online compiler Contact Us
More learning courses More practice paths More compilers Privacy Policy
www.codechef.com Follow Us