The Java Generic ArrayList class provides the expansion capability for an internal array. Access to the list’s elements is fast but both insertion and deletion of an element are slow because this class needs to rearrange the internal array structure after the insertion and the deletion process. Below are a few examples showing you how to use the generic ArrayList class.

The first example will use the ListIterator object from that particular generic ArrayList to remove a particular name from the list.

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class ListDemo {

    private static List<String> no_rock = new ArrayList<String>();
    public static void main(String[] args) {
        
        String[] duplicated_name = {"Rock", "Ray", "Robert", "Rock"};
        for(String name : duplicated_name)
            no_rock.add(name);
        
        ListIterator<String> li = no_rock.listIterator();
        
        while(li.hasNext()) {
            
            if(li.next().equals("Rock"))
                li.remove();
        }

        System.out.print(no_rock); // ["Ray", "Robert"]
    }
   
}

The net example will add in element from an array to the generic arraylist at the head section of the arraylist through a do while loop.

public class Loop {
    private static int SIZE = 5;
    private static String[] hello_world  = {"hello", "world", "great", "really", "nice"};
    public static void main(String[] args) {
        List<String> hello = new ArrayList<String>();
        do {
            SIZE--;
            hello.add(0, hello_world[SIZE]);
        } while(SIZE > 0);
        System.out.println(hello);
    }
         
}

The above program will produce the below outcome!

The elements within the arraylist

The next Java program will remove those elements from the second ArrayList that are overlapping with the first ArrayList!

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test {

    private static List<String> hello = new ArrayList<String>();
    private static List<String> another_parallel_world = new ArrayList<String>();
    private static List<String> from_this_world = new ArrayList<String>();
    public static void main(String[] args) {
        // Adding one collection to another empty collection 
        String[] hello_world = {"Nice World", "Wonderfull World", "Great World"};
        for(String hey : hello_world)
            hello.add(hey);
        another_parallel_world.addAll(hello);
        Iterator<String> another_parallel_world_iterator = another_parallel_world.iterator();
        while(another_parallel_world_iterator.hasNext())
            System.out.println(another_parallel_world_iterator.next());

        // Remove elements from collection that are also in another collection 
        String[] hello_world_2 = {"Nice World", "Wonderfull World", "Great World", "Smooth World"};
        for(String hey : hello_world_2)
            from_this_world.add(hey);
        from_this_world.removeAll(hello);
        Iterator<String> but_you_are_not_from_this_world_iterator = from_this_world.iterator();
        while(but_you_are_not_from_this_world_iterator.hasNext())
            System.out.println(but_you_are_not_from_this_world_iterator.next());

    }
}
Only Smooth World has been left behind in the second arraylist

Finally, the below program will return a portion of the view from an ArrayList and assign it to a list object, whatever changes within the list object will also reflect on the ArrayList object and vice versa.

import java.util.ArrayList;
import java.util.List;

public class Loop {
    private static int SIZE = 5;
    private static String[] hello_world  = {"hello", "world", "great", "really", "nice"};
    public static void main(String[] args) {
        List<String> hello = new ArrayList<String>();
        do {
            SIZE--;
            hello.add(0, hello_world[SIZE]);
        } while(SIZE > 0);

        // return a view of a portion of the hello arraylist starting from hello till nice
        List<String> hello_too = hello.subList(1, 5); 
        hello_too.set(2, "really great"); // reset the second element in the hello_too list
        
        System.out.println(hello_too);
        System.out.print(hello);
    }
         
}
The word really has been changed to really great

Hope you like this post, if possible please share it to help spread this article 🙂

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *