MatiasMerkuri
2/24/2019 - 8:59 PM

Another ArrayList Program

import java.util.*;

class First{
    public static void main(String args[]) {

        String[] things = {"apples", "noobs", "broccoli", "bacon", "goats", "minecraft"};
        List<String> list1 = new LinkedList<>();

        Collections.addAll(list1, things);

        String[] things2 = {"sausage", "bacon", "goats", "ham", "diamonds", "list2 end"};
        List<String> list2 = new LinkedList<>();

        Collections.addAll(list2, things2);

        // Merging the list1 and list2 values into list1
        list1.addAll(list2);
        // Setting list2 to null
        list2 = null;

        // Calling methods
        printMe(list1);
        removeStuff(list1, 2, 5);
        printMe(list1);
        reverseMe(list1);

    }

    // printMe Method which prints out list1 (check the parameters in line 22)
    private static void printMe(List<String> list){
        for (String b : list) {
            System.out.printf("%s ", b);
        }
    }

    // removeStuff Method which removes defined indexes from x to y in list1 array from 2 to 5 (check the parameters in line 23)
    private static void removeStuff(List<String> list, int from, int to){
        list.subList(from, to).clear();
        System.out.println();
    }

    // reverseMe Method which reverses the now shortened list1 (check the parameters in line 25)
    private static void reverseMe(List<String> list){
        ListIterator<String> iterator = list.listIterator(list.size());
        System.out.println();
        while (iterator.hasPrevious()) {
            System.out.printf("%s ", iterator.previous());
        }
    }
}