Sum of Elements in 1D and 2D Arrays in Java
In the realm of programming, dealing with arrays is a fundamental skill. Today, we’ll explore a simple yet essential task: finding the sum of elements in both one-dimensional (1D) and two-dimensional (2D) arrays using Java.
1. Summing Elements in a 1D Array:
A one-dimensional array is a linear collection of elements. To find the sum of all elements in a 1D array, we can use a straightforward loop to iterate through each element and accumulate their values.
public class OneDimensionalArraySum {
public static void main(String[] args) {
// Declare and initialize a 1D array
//Change the value and see the magic
int[] numbers = {5, 10, 15, 20, 25};
// Calculate the sum of elements
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
// Print the result
System.out.println("Sum of elements in the 1D array: " + sum);
}
}Output:
Sum of elements in the 1D array: 752. Summing Elements in a 2D Array:
A two-dimensional array is essentially an array of arrays. Summing its elements involves nested loops, one for rows and another for columns.
public class TwoDimensionalArraySum {
public static void main(String[] args) {
// Declare and initialize a 2D array
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Calculate the sum of elements
int sum = 0;
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
sum += matrix[row][col];
}
}
// Print the result
System.out.println("Sum of elements in the 2D array: " + sum);
}
}Output:
Sum of elements in the 2D array: 453. Key Takeaways:
- For 1D arrays, a simple loop iterating through each element suffices.
- 2D arrays require nested loops for both rows and columns.
- Understand the array structure and use appropriate loops for traversal.
- Arrays in Java are zero-indexed, so adjust your loop conditions accordingly.
- Summing elements is a foundational skill applicable in various real-world scenarios.
Please use the Java Array: Guide to 1D and 2D Array with User Input to make the program more versatile taking user input.
You can program to find Average of Elements in 1D and 2D Arrays using Java
In conclusion, mastering the manipulation of arrays is crucial for any Java programmer. Whether dealing with 1D or 2D arrays, the principles of traversal and summation remain consistent. As you delve deeper into programming, these skills will prove invaluable in solving more complex problems. Happy coding!
Latest Post:
- Convert Between int[] and Integer[]Title: Convert Between int[] and Integer[] In this guide, you’ll learn how to convert a Java int[] array into an Integer[] array, and how to… Read more: Convert Between int[] and Integer[]
- Convert Between Array and String in JavaTitle: Convert Between Array and String in Java Understanding the Problem In this guide, you will learn how to convert a Java array into a… Read more: Convert Between Array and String in Java
- Convert an Array to a Set in JavaTitle: Convert an Array to a Set in Java In this guide, you’ll learn how to convert a Java array into a Set, so you… Read more: Convert an Array to a Set in Java
- Convert Between Array and List in JavaTitle: Convert Between Array and List in Java In this guide, you’ll learn how to convert a Java array into a List and back again,… Read more: Convert Between Array and List in Java
- Check if an Array Contains Repeated PairsTitle: Check if an Array Contains Repeated Pairs In this guide, you’ll learn how to check whether a Java array contains repeated pairs of equal… Read more: Check if an Array Contains Repeated Pairs
- Check if an Array is SymmetricTitle: Check if an Array is Symmetric In this guide, you’ll learn how to check if an array is symmetric in Java. We’ll walk through… Read more: Check if an Array is Symmetric

![Convert Between int[] and Integer[]](https://dailyjavaconcept.com/wp-content/uploads/2026/11/featured_convert-between-int-and-integer-218x150.png)

