Array Manipulation in java

Array Manipulation in java

Array manipulation in Java programming involves multiple array operations applied to Java user-defined single- and multi-dimensional array data types. Such as adding two array elements, deleting array elements, updating array elements, or searching for them within an existing array. Remember, once an array structure is created in a Java program, the size of the array data type automatically becomes constant or fixed. Therefore, you may need to use additional mechanisms or supporting data structure functions to apply certain array manipulation operations. Consequently, you can use and manipulate as many arrays as you need.

Array Manipulation in java

Accessing and Updating Array Elements in a Java Program.

Java programmers can directly access and, if necessary, modify any array element by using the created array index location in the program. Array elements declared in Java programs always start at index 0.

Example of Accessing and Updating Array Elements.

int[] integers = {9, 8, 2, 5, 6, 11};

// Accessing an existing array element in Java

int integer = integers[3]; // Result – 5

// Updating an existing array element in Java with a new value

integers[3] = 7; // The third element (5) is replaced with an integer 7

Calculating Array Length in Java Programs.

Java programmers can find the actual storage size of a user-defined array by using the array’s length property.

int[] integers = {9, 8, 2, 5, 6, 11};

int size = integers.length; // Result – 6

Iterating with a Loop in Java User-Defined Arrays.

Java programmers can loop through the elements of an array data type from start to end by applying a traditional for loop or a modern for loop (also known as a “for-each” loop).

Example of using a regular for loop in Java.

int[] integers = {9, 8, 2, 5, 6, 11};

for (int p = 0; p < integers.length; p++) {

System.out.println(integers[p]);

}

Example of using a better for loop in Java.

for (int integer : integers) {

System.out.println(integers);

}

Finding the current element in a Java array.

Java programmers can find a specific array element value in an existing array by looping within a program or using the Java language’s built-in Array class (from java.util).

Example of a manual search in a Java program.

public class Main

{

int[] integers = {9, 8, 2, 5, 6, 11};

int find = 11;

boolean found = false;

for (int p = 0; p < integers.length; p++) {

if (integers[p] == find) {

found = true;

System.out.println(“Array element found ” + find + ” at array index location at ” + p);

break;

}

}

if (!found) {

System.out.println(“Your search array element not found”);

}

Example of using Arrays.binarySearch() to find.

Java programmers can use the binarySearch() method in array programs to sort and find existing array data elements.

import java.util.Arrays;

int[] integers = {9, 8, 2, 5, 6, 11};

int find = 6;

int index = Arrays.binarySearch(integers, find);

if (index >= 0) {

System.out.println(“Search array element found at index location – ” + index);

} else {

System.out.println(“Search array element not found”);

}

Sorting an array in a Java program.

Java programmers can preview the order of a user-defined storage array by using the Arrays.sort() function method in an array program.

Example of sorting a Java array in ascending order.

import java.util.Arrays;

int[] integers = {9, 8, 2, 5, 6, 11};

Arrays.sort(integers); // This sorts the array data elements in ascending order

System.out.println(Arrays.toString(integers)); // Result – [2, 5, 6, 8, 9, 11]

Example of sorting array data in descending order.

import java.util.Arrays;

import java.util.Collections;

Integer[] integers = {9, 8, 2, 5, 6, 11}; // Using Integers instead of int to sort the array elements in descending order

Arrays.sort(integers, Collections.reverseOrder()); // This sorts the array data type in descending order

System.out.println(Arrays.toString(integers)); // Result – [11, 9, 8, 6, 5, 2]

Reversing a User-Defined Array in Java.

In a Java program, array elements can be reversed by using a custom loop or by using the Collections.reverse() function method for lists. Here, Java programmers convert their array to a list.

Example of reversing an array using a loop.

int[] integers = {9, 8, 2, 5, 6, 11};

int[] opposite = new int[integers.length];

for (int p = 0; p < integers.length; p++) {

opposite[p] = integers[integers.length – 1 – p];

}

System.out.println(Arrays.toString(opposite)); // Result – [11, 6, 5, 2, 8, 9 ]

Copying an array in a Java program.

Java programmers can copy array data elements in a Java program using the System.arraycopy() or Arrays.copyOf() function methods.

System.arraycopy() function usage example.

int[] integers = {9, 8, 2, 5, 6, 11};

int[] copy = new int[6];

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

System.out.println(Arrays.toString(copy)); // Result – {9, 8, 2, 5, 6, 11};

Arrays.copyOf() function usage example.

int[] integers = {9, 8, 2, 5, 6, 11};

int[] copy = Arrays.copyOf(integers, integers.length);

System.out.println(Arrays.toString(copy)); // Result – {9, 8, 2, 5, 6, 11};

Array rotation/circular shift in Java.

Declaring array elements to rotate in a Java program means shifting array elements to the left or right by a particular position.

Java Array Right Rotation Example.

public class Main {

public static void main(String[] args) {

int[] integers = {9, 8, 2, 5, 6, 11};

int r = 8; // This is the number of positions to rotate the array element.

// This indicates a right rotation.

for (int p = 0; p < r; p++) {

int last = integers[integers.length – 1];

for (int q = integers.length – 1; q > 0; q–) {

integers[q] = integers[q – 1];

}

integers[0] = last;

}

System.out.println(Arrays.toString(integers)); // Result –

}

}

Merging array elements in Java.

Java programmers can group two or more arrays into a single array.

Example of merging arrays.

int[] arryelement1 = {70, 90, 20, 40};

int[] arryelement2 = {80, 30, 44, 66};

// Create a new array to store the merged array result in the Java program

int[] mergedarray = new int[arryelement1.length + aryelement2.length];

// Copy elements from both arrays into the merged array.

System.arraycopy(arryelement1, 0, merged, 0, aryelement1.length);

System.arraycopy(arryelement2, 0, merged, arryelement1.length, arryelement2.length);

System.out.println(Arrays.toString(mergedarray)); // Result – [70, 90, 20, 40, 80, 30, 44, 66]

Removing duplicate elements from an array in Java.

Java programmers can remove duplicate array elements by modifying them to the Set data type, which automatically removes duplicate array elements, or manually removing them by comparing them.

Example of removing array elements using the Set data type.

import java.util.*;

int[] integers = {2, 9, 8, 11, 2, 5, 6, 9, 11, 8};

Set<integers> set = new LinkedHashSet<>(); // Use a LinkedHashSet to maintain set data order

for (int integer : integers) {

set.add(integer);

}

// Convert the set back to the Array data type

int[] uniqueArray = set.stream().mapToInt(integer::intValue).toArray();

System.out.println(Arrays.toString(uniqueArray)); // Result – {9, 8, 2, 5, 6, 11}

A detailed summary of the Java array manipulation process.

  • Array Access – Access desired array elements by accessing them from their index storage locations.
  • Array Update – Modify array elements by accessing them from their index locations.
  • Array Iterate – Use loops such as for, while, do while, for each, etc. to traverse declared array elements in a Java program.
  • Array Search – Find desired array elements by using loops or binary search for declared array elements in a Java program.
  • Array Sort – Sort declared array elements by using the Arrays.sort() function method.
  • Array Reverse – Reverse the declared array elements in a Java program using loops or the Collections.reverse() function.
  • Array Copy – Copy the declared array elements in a Java program using the System.arraycopy() or Arrays.copyOf() function.
  • Array Merge – Merge the declared array elements in a Java program using the System.arraycopy() function.
  • Array Rotation – Circularly shift the declared array elements in a Java program.
  • Remove Duplicates Array – Use the Set data type to remove duplicate array values ​​from a declared array element in a Java program.

Leave a Reply