0% found this document useful (0 votes)
2 views4 pages

Array Manipulation in Java

The document provides an overview of array manipulation in Java, detailing common tasks such as copying, reversing, finding maximum and minimum values, and calculating the sum and average of array elements. It includes multiple methods for each task, such as using loops and built-in Java methods like System.arraycopy() and Arrays.stream(). The summary table at the end consolidates the various methods for each operation.

Uploaded by

joashodchas09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Array Manipulation in Java

The document provides an overview of array manipulation in Java, detailing common tasks such as copying, reversing, finding maximum and minimum values, and calculating the sum and average of array elements. It includes multiple methods for each task, such as using loops and built-in Java methods like System.arraycopy() and Arrays.stream(). The summary table at the end consolidates the various methods for each operation.

Uploaded by

joashodchas09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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[original.length];

for (int i = 0; i < original.length; i++) {


copy[i] = original[i];
}

System.out.println("Original Array: " +


java.util.Arrays.toString(original));
System.out.println("Copied Array: " +
java.util.Arrays.toString(copy));
}
}

Method 2: Using System.arraycopy()


public class CopyArraySystem {
public static void main(String[] args) {
int[] original = {10, 20, 30, 40, 50};
int[] copy = new int[original.length];

System.arraycopy(original, 0, copy, 0, original.length);

System.out.println("Copied Array: " +


java.util.Arrays.toString(copy));
}
}
Method 3: Using Arrays.copyOf()
import java.util.Arrays;

public class CopyArrayMethod {


public static void main(String[] args) {
int[] original = {5, 10, 15, 20, 25};
int[] copy = Arrays.copyOf(original, original.length);

System.out.println("Copied Array: " + Arrays.toString(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 java.util.Arrays;

public class ReverseArray {


public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};

for (int i = 0, j = arr.length - 1; i < j; i++, j--) {


int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

System.out.println("Reversed Array: " + Arrays.toString(arr));


}
}

Method 2: Using Collections.reverse() (For Integer Objects)


import java.util.Arrays;
import java.util.Collections;

public class ReverseArrayCollections {


public static void main(String[] args) {
Integer[] arr = {1, 2, 3, 4, 5};

Arrays.sort(arr, Collections.reverseOrder());

System.out.println("Reversed Array: " + Arrays.toString(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 < numbers.length; i++) {


if (numbers[i] > max) {
max = numbers[i];
}
if (numbers[i] < min) {
min = numbers[i];
}
}

System.out.println("Maximum: " + max);


System.out.println("Minimum: " + min);
}
}

Method 2: Using Arrays.stream()


import java.util.Arrays;

public class MinMaxStream {


public static void main(String[] args) {
int[] numbers = {10, 25, 3, 7, 40};

int max = Arrays.stream(numbers).max().getAsInt();


int min = Arrays.stream(numbers).min().getAsInt();

System.out.println("Maximum: " + max);


System.out.println("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 / numbers.length;

System.out.println("Sum: " + sum);


System.out.println("Average: " + average);
}
}

Method 2: Using Arrays.stream()


import java.util.Arrays;

public class SumAndAverageStream {


public static void main(String[] args) {
int[] numbers = {3, 6, 9, 12, 15};

int sum = Arrays.stream(numbers).sum();


double average = Arrays.stream(numbers).average().orElse(0);

System.out.println("Sum: " + sum);


System.out.println("Average: " + average);
}
}

Summary of Array Manipulation Methods


Operation Methods
Copying an Array for loop, System.arraycopy(), Arrays.copyOf()
Reversing an Array for loop, Collections.reverseOrder() (for objects)
Finding Max & Min for loop, Arrays.stream().max()/min()
Finding Sum & Average for loop, Arrays.stream().sum()/average()

You might also like