Sitemap
boorge

boorge

Software Engineer

Basic Array Programs in Java

3 min readDec 11, 2024

--

Arrays are a fundamental part of Java, used to store multiple values in a single variable. Here are a few examples to get you started.

Press enter or click to view image in full size

Example 1: Declaring and Initializing an Array

Program: Declare an array and initialize it with values.

Steps:

  1. Declare the array.
  2. Initialize the array with values.
  3. Print the array elements.
  4. Print the array elements.
public class Main {
public static void main(String[] args) {
// Step 1: Declare the array
int[] numbers;
// Step 2: Initialize the array with values
numbers = new int[]{1, 2, 3, 4, 5};
// Step 3: Print the array elements
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}

Example 2: Finding the Largest Element in an Array

Program: Find the largest element in an array.

Steps:

  1. Declare and initialize the array.
  2. Assume the first element is the largest.
  3. Compare each element with the current largest and update if needed.
  4. Print the largest element.
public class Main {
public static void main(String[] args) {
// Step 1: Declare and initialize the array
int[] numbers = {10, 20, 30, 40, 50};
// Step 2: Assume the first element is the largest
int largest = numbers[0];
// Step 3: Compare each element with the current largest and update if needed
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
// Step 4: Print the largest element
System.out.println("Largest element: " + largest);
}
}

Example 3: Sum of All Elements in an Array

Program: Calculate the sum of all elements in an array.

Get boorge’s stories in your inbox

Join Medium for free to get updates from this writer.

Steps:

  1. Declare and initialize the array.
  2. Initialize a sum variable to 0.
  3. Iterate through the array and add each element to the sum.
  4. Print the sum.
public class Main {
public static void main(String[] args) {
// Step 1: Declare and initialize the array
int[] numbers = {5, 10, 15, 20, 25};
// Step 2: Initialize a sum variable to 0
int sum = 0;
// Step 3: Iterate through the array and add each element to the sum
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
// Step 4: Print the sum
System.out.println("Sum of all elements: " + sum);
}
}

Example 4: Reversing an Array

Program: Reverse the elements of an array.

Steps:

  1. Declare and initialize the array.
  2. Create a new array to hold the reversed elements.
  3. Fill the new array with elements from the original array in reverse order.
  4. Print the reversed array.
public class Main {
public static void main(String[] args) {
// Step 1: Declare and initialize the array
int[] numbers = {1, 2, 3, 4, 5};
// Step 2: Create a new array to hold the reversed elements
int[] reversed = new int[numbers.length];
// Step 3: Fill the new array with elements from the original array in reverse order
for (int i = 0; i < numbers.length; i++) {
reversed[i] = numbers[numbers.length - 1 - i];
}
// Step 4: Print the reversed array
for (int i = 0; i < reversed.length; i++) {
System.out.println(reversed[i]);
}
}
}

Example 5: Sorting an Array

Program: Sorting an array in Ascending Order.

import java.util.Arrays;

public class SortArray {
public static void main(String[] args) {
int[] array = {50, 40, 30, 20, 10}; // Initialize the array
Arrays.sort(array); // Sort the array in ascending order

System.out.println("Sorted Array: " + Arrays.toString(array)); // Print the sorted array
}
}

These examples should help you understand the basics of working with arrays in Java.

--

--