Write a java program to manipulate ArrayLists

In this article, we will create a program in Java to manipulate ArrayList. Write a program that performs the following actions:

  1. Read 2n integers as input.
  2. Create two ArrayList to store n elements in each ArrayList.
  3. Write a function generateOddEvenList which accepts this two ArrayList as input.
  4. The function fetches the odd index elements from the first array list and even index elements from the second array list and adds them to a new array list according to their index.
  5. Return the ArrayList.

Note:

  • The index of the first element is 0.
  • Consider 0 as an even number.
  • Maintain order in the output array list

Input and Output Format

  • Input consists of 2n+1 integers. The first integer denotes the size of the ArrayList, the next n integers are values to the first ArrayList, and the last n integers are values to the second ArrayList.
  • Output consists of a modified ArrayList as per step 4.

Refer sample output for formatting specifications

Sample Input 1:

5

12

13

14

15

16

2

3

4

5

6

Sample Output 1:

2

13

4

15

6

Java program to manipulate ArrayList

Following are the steps to manipulate ArrayList:

  • Input the size of the array from the user.
  • Declare and initialize two array lists with the numbers.
  • Create one more array list to store results.
  • Pass both the element of the list to display() method. 
  • Inside the method, iterate over the list and fetch the odd index elements from the first array list and even index elements from the second array list and add them to a new array list according to their index.
  • At last, return the list.
package testCases;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class MainJava {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int s = Integer.parseInt(sc.nextLine());
ArrayList<Integer> al1 = new ArrayList<Integer>();
ArrayList<Integer> al2 = new ArrayList<Integer>();
for (int i = 0; i < s; i++)
al1.add(sc.nextInt());
for (int i = 0; i < s; i++)
al2.add(sc.nextInt());
ArrayList<Integer> al3 = new ArrayList<Integer>();
al3 = display(al1, al2);
Iterator<Integer> it = al3.iterator();
while (it.hasNext()) {
int n = it.next();
System.out.println(n);
}
}
public static ArrayList<Integer> display(ArrayList<Integer> al1, ArrayList<Integer> al2) {
ArrayList<Integer> al3 = new ArrayList<Integer>();
for (int i = 0; i < al1.size(); i++) {
if (i % 2 == 0)
al3.add(al2.get(i));
else
al3.add(al1.get(i));
}
return al3;
}
}

Output

How to add elements to ArrayList in Java dynamically?

In ArrayList, you don’t have to worry about the size. But if you want to add elements dynamically then we can follow the below simple approach.

  • Here, just create an array list.
  • Add an element to it based on the index using add() method.
  • Iterate over the list and get each element using the get() method.
package com.company;

import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListProblem {
    public static void main(String[] args) {
        ArrayList<String> al = new ArrayList<>(20);
        for(int i =0 ;i < 20;i++){
            al.add(" "+i);
        }
        for(int i =0; i< 20;i++){
            System.out.print(" "+al.get(i));
        }
    }

}

Output

Thus, in this way, we manipulate ArrayList using the above method.