Array Manipulation in Java
Array manipulation refers to modifying, analyzing, or transforming an array to perform specific
operations. Common array manipulation tasks include:
1. Copying an array
2. Reversing an array
3. Finding the maximum and minimum values
4. Finding the sum and average of elements
1. Copying an Array
Copying an array means creating a duplicate array with the same elements. There are multiple
ways to copy an array in Java:
Method 1: Using a Loop (Manual Copying)
public class CopyArray {
public static void main(String[] args) {
int[] original = {1, 2, 3, 4, 5};
int[] copy = new int[[Link]];
for (int i = 0; i < [Link]; i++) {
copy[i] = original[i];
}
[Link]("Original Array: " +
[Link](original));
[Link]("Copied Array: " +
[Link](copy));
}
}
Method 2: Using [Link]()
public class CopyArraySystem {
public static void main(String[] args) {
int[] original = {10, 20, 30, 40, 50};
int[] copy = new int[[Link]];
[Link](original, 0, copy, 0, [Link]);
[Link]("Copied Array: " +
[Link](copy));
}
}
Method 3: Using [Link]()
import [Link];
public class CopyArrayMethod {
public static void main(String[] args) {
int[] original = {5, 10, 15, 20, 25};
int[] copy = [Link](original, [Link]);
[Link]("Copied Array: " + [Link](copy));
}
}
2. Reversing an Array
Reversing an array means swapping the first element with the last, the second with the second
last, and so on.
Method 1: Using a Loop (In-Place)
import [Link];
public class ReverseArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0, j = [Link] - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
[Link]("Reversed Array: " + [Link](arr));
}
}
Method 2: Using [Link]() (For Integer Objects)
import [Link];
import [Link];
public class ReverseArrayCollections {
public static void main(String[] args) {
Integer[] arr = {1, 2, 3, 4, 5};
[Link](arr, [Link]());
[Link]("Reversed Array: " + [Link](arr));
}
}
3. Finding the Maximum and Minimum Values in an Array
Method 1: Using a Loop
public class MinMaxArray {
public static void main(String[] args) {
int[] numbers = {5, 20, 8, 12, 3};
int max = numbers[0];
int min = numbers[0];
for (int i = 1; i < [Link]; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
if (numbers[i] < min) {
min = numbers[i];
}
}
[Link]("Maximum: " + max);
[Link]("Minimum: " + min);
}
}
Method 2: Using [Link]()
import [Link];
public class MinMaxStream {
public static void main(String[] args) {
int[] numbers = {10, 25, 3, 7, 40};
int max = [Link](numbers).max().getAsInt();
int min = [Link](numbers).min().getAsInt();
[Link]("Maximum: " + max);
[Link]("Minimum: " + min);
}
}
4. Finding the Sum and Average of Array Elements
Method 1: Using a Loop
public class SumAndAverage {
public static void main(String[] args) {
int[] numbers = {2, 4, 6, 8, 10};
int sum = 0;
for (int num : numbers) {
sum += num;
}
double average = (double) sum / [Link];
[Link]("Sum: " + sum);
[Link]("Average: " + average);
}
}
Method 2: Using [Link]()
import [Link];
public class SumAndAverageStream {
public static void main(String[] args) {
int[] numbers = {3, 6, 9, 12, 15};
int sum = [Link](numbers).sum();
double average = [Link](numbers).average().orElse(0);
[Link]("Sum: " + sum);
[Link]("Average: " + average);
}
}
Summary of Array Manipulation Methods
Operation Methods
Copying an Array for loop, [Link](), [Link]()
Reversing an Array for loop, [Link]() (for objects)
Finding Max & Min for loop, [Link]().max()/min()
Finding Sum & Average for loop, [Link]().sum()/average()