Recursion Hack

public void drawLine(int n) {
    if (n == 0) {
        return;
    }
    for (int i = 1; i <= n; i++) {
        System.out.print("*");
    }
    System.out.println();
    drawLine(n - 1);
}

drawLine(10);
**********
*********
********
*******
******
*****
****
***
**
*

Sorting Hack

import java.util.ArrayList;
import java.util.Comparator;

public class Country {
    private String name;
    private int size;
    
    public Country(String name, int size) {
        this.name = name;
        this.size = size;
    }
    
    public int getSize() {
        return size;
    }
    
    public String getName() {
        return name;
    }
    
    public static void selectionSort(ArrayList<Country> countries, Comparator<Country> comparator) {
        for (int i = 0; i < countries.size() - 1; i++) {
            int maxIndex = i;
            for (int j = i + 1; j < countries.size(); j++) {
                if (comparator.compare(countries.get(j), countries.get(maxIndex)) > 0) {
                    maxIndex = j;
                }
            }
            if (maxIndex != i) {
                Country temp = countries.get(i);
                countries.set(i, countries.get(maxIndex));
                countries.set(maxIndex, temp);
            }
        }
    }
    
    public static void main(String[] args) {
        ArrayList<Country> countries = new ArrayList<>();
        countries.add(new Country("USA", 9834));
        countries.add(new Country("China", 9596));
        countries.add(new Country("Japan", 377));
        countries.add(new Country("India", 3287));
        countries.add(new Country("Russia", 17098));
        
        // Sort the countries in decreasing order based on their size
        selectionSort(countries, new Comparator<Country>() {
            @Override
            public int compare(Country c1, Country c2) {
                return Integer.compare(c2.getSize(), c1.getSize());
            }
        });
        
        // Print the sorted list of countries
        for (Country country : countries) {
            System.out.println(country.getName() + " - " + country.getSize());
        }
    }
}

Country.main(null)
Japan - 377
India - 3287
China - 9596
USA - 9834
Russia - 17098

Array List Hacks

Test if two arraylists contain the same elements in reverse order

import java.util.ArrayList;
import java.util.Collections;

public class ArrayListReverseOrderTest {

    public static void main(String[] args) {
        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        
        ArrayList<Integer> list2 = new ArrayList<>();
        list2.add(3);
        list2.add(2);
        list2.add(1);
        
        boolean isSame = isArrayListsEqualInReverseOrder(list1, list2);
        
        System.out.println("Do the arraylists contains the same elements in reverse order? " + isSame);
    }
    
    public static <T> boolean isArrayListsEqualInReverseOrder(ArrayList<T> list1, ArrayList<T> list2) {
        if (list1.size() != list2.size()) {
            return false;
        }
        
        ArrayList<T> reverseList1 = new ArrayList<>(list1);
        Collections.reverse(reverseList1);
        
        for (int i = 0; i < list2.size(); i++) {
            if (!list2.get(i).equals(reverseList1.get(i))) {
                return false;
            }
        }
        
        return true;
    }
}

ArrayListReverseOrderTest.main(null)
Do the arraylists contains the same elements in reverse order? true

Overwrite all the elements in an arraylist with the alphabet

import java.util.ArrayList;

public class ArrayListOverwriteTest {

    public static void main(String[] args) {
        ArrayList<Character> list = new ArrayList<>();
        
        // Add 26 elements to the list
        for (int i = 0; i < 26; i++) {
            list.add(null);
        }
        
        // Overwrite the elements with the alphabet
        for (int i = 0; i < 26; i++) {
            char c = (char) (i + 'A');
            list.set(i, c);
        }
        
        // Print the list
        System.out.println(list);
    }
}

ArrayListOverwriteTest.main(null)
[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]