Java Array Tutorial: Store and Process Values
This tutorial will guide you through the basics of working with arrays in Java. Arrays are fundamental data structures for storing collections of elements of the same type. We’ll cover creating arrays, initializing them, accessing their elements, and performing basic operations.
Example 1: Creating and Initializing an Integer Array
Let’s start by creating a simple integer array and initializing it with some values. Arrays in Java are zero-indexed, meaning the first element is at index 0.
import java.util.Arrays;
public class ArrayExample1 {
public static void main(String[] args) {
// Declare and initialize an integer array
int[] numbers = {10, 20, 30, 40, 50};
// Print the array
System.out.println("Array: " + Arrays.toString(numbers));
// Accessing elements:
System.out.println("First element: " + numbers[0]);
System.out.println("Third element: " + numbers[2]);
}
}
Explanation:
- We import the `java.util.Arrays` class to use the `toString()` method for convenient array printing.
- We declare an integer array named `numbers` using the `int[]` data type.
- We initialize the array directly with values enclosed in curly braces `{}`.
- `Arrays.toString(numbers)` converts the array to a string representation for easy printing.
- We access individual elements using their index within square brackets `[]`. Remember, the first element is at index 0.
Common Mistake: Trying to access an element beyond the array’s bounds (e.g., `numbers[5]` when the array has only 5 elements) will result in an `ArrayIndexOutOfBoundsException`.
Correction: Always check the array’s size before accessing an element to avoid this error.
Output:
Array: [10, 20, 30, 40, 50]
First element: 10
Third element: 30
Example 2: Calculating the Sum of Array Elements
Now, let’s write a program to calculate the sum of all elements in an integer array.
import java.util.Arrays;
public class ArrayExample2 {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
// Iterate through the array and add each element to the sum
for (int number : numbers) {
sum += number;
}
System.out.println("Sum of the array elements: " + sum);
}
}
Explanation:
- We declare an integer array `numbers`.
- We initialize a variable `sum` to 0 to store the cumulative sum.
- We use a `for-each` loop to iterate through each element in the `numbers` array.
- In each iteration, the current element (`number`) is added to the `sum`.
- Finally, we print the calculated `sum`.
Output:
Sum of the array elements: 15
Example 3: Processing an Array of Strings
Let’s extend our knowledge to work with an array of strings. This example demonstrates how to find the longest string in the array.
public class ArrayExample3 {
public static void main(String[] args) {
String[] words = {"apple", "banana", "kiwi", "orange"};
String longestWord = "";
int maxLength = 0;
for (String word : words) {
if (word.length() > maxLength) {
maxLength = word.length();
longestWord = word;
}
}
System.out.println("Longest word: " + longestWord);
}
}
Explanation:
- We declare a string array `words`.
- We initialize `longestWord` to an empty string and `maxLength` to 0.
- We use a `for-each` loop to iterate through the `words` array.
- Inside the loop, we check if the length of the current `word` is greater than `maxLength`.
- If it is, we update `maxLength` and `longestWord` accordingly.
- Finally, we print the `longestWord`.
Output:
Longest word: orange
Key Takeaways:
- Arrays are powerful for storing and manipulating collections of data.
- Zero-based indexing is crucial in Java.
- Use loops to iterate through arrays and perform calculations or operations on their elements.
- Consider the size of the array to avoid `ArrayIndexOutOfBoundsException`.



Leave a Reply