How to detect if an element is the last element in an array?

15,223

Solution 1

There is no direct method to know if you have the last entry of a List in a foreach loop.

The easiest would be to use an indexed-loop.

Something like that:

    List<String> fragmentMessage = fragmentMessage(message);
    for (int i = 0; i < fragmentMessage.size(); i++) {
        String fragment = fragmentMessage.get(i);
        /*
         * If "fragment" is the last element of the array, then do something
         */
        if (i == fragmentMessage.size()-1) {

        }
    }

Another option would be an Iterator

    for (Iterator<String> iterator = fragmentMessage(message).iterator();
        iterator.hasNext();) {
        String fragment = iterator.next();
        if (!iterator.hasNext()) {

        }
    }

Solution 2

You have a few options.

Use an Iterator:

    String message = "Who Framed Roger Rabbit?";
    List<String> fragments = fragmentMessage(message);

    Iterator<String> iterator = fragments.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());

        if (!iterator.hasNext()) {
            System.out.println("All done!");
        }
    }

Check the index within for/each loop:

    for (String str :
            fragments) {

        System.out.println(str);

        // If this item is the last in the list
        if (fragments.indexOf(str) == fragments.size() - 1) {
            System.out.println("All done!");
        }
    }

With this method, if you're using strings in your list, just be aware that if there are duplicates, this will not be very accurate.

Track the index with standard for loop:

    for (int i = 0; i < fragments.size(); i++) {
        System.out.println(fragments.get(i));

        if (i == fragments.size() - 1) {
            System.out.println("All done!");
        }
    }
Share:
15,223
48502104
Author by

48502104

Updated on June 04, 2022

Comments

  • 48502104
    48502104 almost 2 years

    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:

    1. ArrayList Element 0: Who Fram
    2. ArrayList Element 1: ed Roger
    3. 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;
    }
    }