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

709 views java
-5

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;
}
}

answered question

Instead of the for loop, you could use an Iterator, which has a hasNext() method.

Or a traditional for loop, which uses an index.

There might be better ways, depending on what you actually want to achieve. But you didn't say...

It would be better for anyone to promote cleaner approach solutions if explained what you are trying to achieve.

2 Answers

13

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()) {

        }
    }

posted this
3

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!");
        }
    }

posted this

Have an answer?

JD

Please login first before posting an answer.