How to reverse an array list in Java. – IT Blog
IT Blog
Boris IT Expert

How to reverse an array list in Java.

Boris~November 5, 2020 /Array List

If you want to reverse an array in Java, you should follow several steps. The general idea to set two pointers. The left pointer’s primary purpose is to start from the array’s left side and move to the center. The right pointer’s primary goal is to begin from the right side and move to the center. The second part is a loop. With its help, we would move pointers towards each other and swap the pointer’s elements. The loop supposes to stop when two pointers face each other in the center of the array.

Reverse the array list in the Java example. Step-by-step implementation.

If you want to reverse the array list, you have to follow several steps:

reverse the array in java
//Setting the start point of the array
        int leftPointer = 0;

//Setting the end point of the array
        int rightPointer = intArray.length - 1;
        while (leftPointer < rightPointer) 
reverse the array in java
 //Creating temporary variable to save start point value
            int temporaryValue = intArray[leftPointer];

//Since we saved first element value in temporary variable we are setting the value to the last item
            intArray[leftPointer] = intArray[rightPointer];

//Setting the last element value to temporary value which is first element value we saved before
            intArray[rightPointer] = temporaryValue;
reverse the array in java
//Moving start point to next element right of current one
            leftPointer++;
reverse the array in java
//Moving end point to next element left of current one
            rightPointer--;

The full code schema:

reverse the array in java

The full code example:

private void reverseTheArray() {
//Created a new array of integers
        int[] intArray = new int[]{14, 5, 35, 44, 5};

//Setting the start point of the array
        int leftPointer = 0;

//Setting the end point of the array
        int rightPointer = intArray.length - 1;

        while (leftPointer < rightPointer) {
//Creating temporary variable to save start point value
            int temporaryValue = intArray[leftPointer];

//Since we saved first element value in temporary variable we are setting the value to the last item
            intArray[leftPointer] = intArray[rightPointer];

//Setting the last element value to temporary value which is first element value we saved before
            intArray[rightPointer] = temporaryValue;

//Moving start point to next element right of current one
            leftPointer++;

 //Moving end point to next element left of current one
            rightPointer--;
        }

 //Get the length of the array
        int arrayLength = intArray.length;

 //Going through the array until it finishes
        for (int i = 0; i < arrayLength; i++) {
  //Show in logs each element of the array
            Log.d("arrayList", "" + intArray[i]);
        }
    }

In this article, you figured out the easiest way to reverse an array list in java.

Full code and more examples in my facebook group.

Useful links:

Leave Any Questions Related To This Article Below!