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:
- Set two pointers. Left and right pointers. One at the start of the array and another at the end of the array list.

//Setting the start point of the array
int leftPointer = 0;
//Setting the end point of the array
int rightPointer = intArray.length - 1;
- Start the “while loop” It suppose to work until the left pointer less than the right one.
while (leftPointer < rightPointer)
- Swap elements. With each iteration, we need to swap the values in the elements of the pointers.

//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;
- Move the left pointer to the center of the array list.

//Moving start point to next element right of current one
leftPointer++;
- Move the right pointer to the center of the array list.

//Moving end point to next element left of current one
rightPointer--;
The full code schema:

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!