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

check if element is last in array javascript
javascript for loop last item
foreach last item javascript
php foreach last item
how to check last element in foreach java
codehs last element in array
get last element of array java
check if element is last element in list java

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

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

        }
    }

How to check if an element of an array is the last within a loop in , For loop. To check wheter an iteration is the last in the loop or not, we only need to know the length of the array. Based on this  The comparison that we need to make to check if an iteration is the last within an array, can vary according to the way you iterate. For loop. To check wheter an iteration is the last in the loop or not, we only need to know the length of the array.

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

Get the first and last item in an array using JavaScript, The first and last elements are accessed using an index and the first value is How to check whether an array is subset of another array using JavaScript ? Custom function to get the last elements from the array We can create our own custom function which will return the n last elements from the array. With this method we can find the n elements from the end. If n is provided then it will return only last element.

My final solution looks like this. It is based off @k5_ answer and credit goes to him/her/other.

I used an if-else statement to only target the last element.

    List<String> fragmentMessage = fragmentMessage(message);
    for (int i = 0; i < fragmentMessage.size(); i++) {
        String fragment = fragmentMessage.get(i);
        if (message.isEmpty()) { // Only if message == "".
            System.out.println(createFrame(message));
        } else if (i == fragmentMessage.size() - 1) { // Last element of the array.
            System.out.println(createLastFrame(fragment));
        } else {
            System.out.println(createFrame(fragment)); // Only if message is not the last message.
        }
    }

Determine the first and last iteration in a foreach loop in PHP , Given an array of elements and the task is to determine first and last iteration in Use a counter variable and check when the counter value is zero then it is the  First, let me say that the use of '\0' in an integer array as an end of array indicator is suspect. It makes little sense. You would not want to use strlen with an integer array.

Checking if the array pointer has reached the last item in an array , How do i know if the array pointer has reached the end? That will end the loop prematurely if the last element in the array appears anywhere earlier in the  Method #4 : Using reversed () + next () reversed () coupled with next () can easily be used to get the last element, as like one of the naive method, reversed method returns the reversed ordering of list as an iterator, and next () method prints the next element, in this case last element. filter_none.

How to get last element of an array in JavaScript?, To get the last item without knowing beforehand how many items it contains, you can use the length property to determine it, and since the array  This article discusses the preferred way to retrieve the last element of an Array in JavaScript. Tip 7 in this Useful JavaScript Hacks Article proposes the following method to get the last item in an array. syntax1.js

PHP end() Function, Web Certificates Web Editor Web Development Test Your Typing Speed Output the value of the current and the last element in an array: The end() function moves the internal pointer to, and outputs, the last element in the array. the value of the last element in the array on success, or FALSE if the array is empty. The end() function moves the internal pointer to, and outputs, the last element in the array. Related methods: current() - returns the value of the current element in an array; next() - moves the internal pointer to, and outputs, the next element in the array; prev() - moves the internal pointer to, and outputs, the previous element in the array

Comments
  • 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...
  • Possible duplicate of How to get the last value of an ArrayList
  • You considered the case that the last element is also empty?
  • @k5_ I can't see of any way the last element can be empty in my code. Is there a way I can privately send you my class?