Lesson 1

Welcome! Today, I am thrilled to guide you through a fascinating task involving ArrayLists in **Java**: pairing up 'opposite' elements. Specifically, we're going to learn how to access and manipulate elements within a Java `ArrayList`

. The task at hand provides an excellent opportunity to elevate your **ArrayList-handling skills** within the Java language. Are you ready to get started? Let's dive right in!

Our task today is to form pairs of 'opposite' elements in a given `ArrayList`

of integers. In an `ArrayList`

consisting of `n`

elements, the first and last elements are known as 'opposite', the second element and the second last element are considered 'opposite', and so on. For an `ArrayList`

with an odd length, the middle element is its own 'opposite'.

You will be provided with an `ArrayList`

of `n`

integers. The value of `n`

could range from 1 to 100, inclusive. The task will require you to return an `ArrayList`

of `String`

objects. Each `String`

will consist of an element and its 'opposite' element joined by a space.

Let's use the example `ArrayList`

`numbers`

as `{1, 2, 3, 4, 5}`

to simplify our understanding. In this case, the output of our `solution(numbers)`

function will be `{"1 5", "2 4", "3 3", "4 2", "5 1"}`

.

Before we start writing code, let's familiarize ourselves with how to access the elements of an `ArrayList`

in Java.

In Java, the `i`

-th element of an `ArrayList`

`numbers`

can be accessed as `numbers.get(i)`

, with the index starting from `0`

. Consequently, the first element is `numbers.get(0)`

, the second one is `numbers.get(1)`

, and so forth, up to `numbers.get(numbers.size() - 1)`

for the last element.

Java`1import java.util.ArrayList; 2 3public class Solution { 4 public static ArrayList<String> solution(ArrayList<Integer> numbers) { 5 int i = 0; 6 int elementAtGivenIndex = numbers.get(i);`

Now, let's figure out how to access an element's 'opposite'.

The 'opposite' of the `i`

-th element of the `ArrayList`

is the element at the `numbers.size() - i - 1`

-th position. To illustrate this concept, consider standing at the start of a line and your friend standing at the end of the line. In this scenario, you and your friend could be considered 'opposites'. Similarly, the 'opposite' of `numbers.get(0)`

is `numbers.get(numbers.size() - 0 - 1)`

, the 'opposite' to `numbers.get(1)`

is `numbers.get(numbers.size() - 1 - 1)`

, and so forth.

Now that we understand how to locate an element's 'opposite', we can proceed to code our solution. Let's start by initializing an empty `ArrayList`

named `result`

to store our 'opposite' pairs and compute the `ArrayList`

's size for future reference.

Java`1import java.util.ArrayList; 2 3public class Solution { 4 public static ArrayList<String> solution(ArrayList<Integer> numbers) { 5 ArrayList<String> result = new ArrayList<>(); 6 int n = numbers.size();`

The next step is to loop over all elements in our `numbers`

`ArrayList`

. Within our loop, we'll create a `String`

for each pairing of 'opposite' elements. This `String`

will consist of the `i`

-th element and the `n - i - 1`

-th element separated by a space. This pair will then be added to our `result`

`ArrayList`

. Here's the final version of our function:

Java`1import java.util.ArrayList; 2import java.util.Arrays; 3 4public class Solution { 5 public static ArrayList<String> solution(ArrayList<Integer> numbers) { 6 ArrayList<String> result = new ArrayList<>(); 7 int n = numbers.size(); 8 for (int i = 0; i < n; i++) { 9 result.add(numbers.get(i) + " " + numbers.get(n - i - 1)); 10 } 11 return result; 12 } 13 14 public static void main(String[] args) { 15 ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); 16 System.out.println(solution(numbers)); 17 } 18}`

This function iterates over all the elements of the `ArrayList`

. For each of these elements, it forms a `String`

with its 'opposite' and subsequently adds the pair into the `result`

`ArrayList`

.

Great job! You've successfully navigated through the concept of 'opposite' pairs and `ArrayList`

indexing in Java. By now, you should be familiar with the notion of accessing and pairing elements in an `ArrayList`

based on their positions. This fundamental step brings you closer to mastering `ArrayList`

manipulation within Java. Up next, we've prepared a series of hands-on exercises for you to apply and further practice what you've learned today. Remember, practice makes perfect. Keep up the great work, and happy coding!