The code below splits a String based on its position and then puts it into an ArrayList.
So for example, if the String is "Who Framed Roger Rabbit?", then fragmentMessage will split the message at every 8th character.
So the ArrayList will look like this:  [Who Fram, ed Roger,  Rabbit?]
The elements of the ArrayList are as follows:
- ArrayList Element 0: Who Fram
- ArrayList Element 1: ed Roger
- ArrayList Element 2: Rabbit?
I then have a for-each loop in the main method which iterates over the ArrayList.
How can I detect if I have reached the last element of the array when iterating?
So for example, "if this is the last element of the array, then print "LAST ELEMENT".
import java.util.ArrayList;
import java.util.List;
public class LastElement {
public static void main(String[] args) {
    String message = "Who Framed Roger Rabbit?";
    for (String fragment : fragmentMessage(message)) {
        /*
         * If "fragment" is the last element of the array, then do something
         */
    }
}
private static List<String> fragmentMessage(String message) {
    List<String> fragmentedMessage = new ArrayList<>();
    for (int i = 0; i < message.length(); i = i + 8) {
        fragmentedMessage.add(message.substring(i, i + 8));
        System.out.println(fragmentedMessage);
    }
    return fragmentedMessage;
}
}
 
     
     
    