如何检测一个元素是否是数组中的最后一个元素?

下面的代码根据它的位置拆分一个字符串,然后将它放入一个 ArrayList。

因此,例如,如果 String 是"Who Framed Roger Rabbit?",则将fragmentMessage在每 8 个字符处拆分消息。

因此 ArrayList 将如下所示: [Who Fram, ed Roger,  Rabbit?]

ArrayList 的元素如下:

  1. 数组列表元素 0: Who Fram

  2. ArrayList 元素 1: ed Roger

  3. ArrayList 元素 2: Rabbit?

然后我在主要方法中有一个 for-each 循环,它迭代 ArrayList。

迭代时如何检测是否已到达数组的最后一个元素?

例如,“如果这是数组的最后一个元素,则打印“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;

}

}


慕盖茨4494581
浏览 521回答 3
3回答

慕莱坞森

没有直接的方法可以知道List在 foreach 循环中是否有 a 的最后一个条目。最简单的方法是使用索引循环。类似的东西:&nbsp; &nbsp; List<String> fragmentMessage = fragmentMessage(message);&nbsp; &nbsp; for (int i = 0; i < fragmentMessage.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; String fragment = fragmentMessage.get(i);&nbsp; &nbsp; &nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* If "fragment" is the last element of the array, then do something&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; if (i == fragmentMessage.size()-1) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }另一种选择是 Iterator&nbsp; &nbsp; for (Iterator<String> iterator = fragmentMessage(message).iterator();&nbsp; &nbsp; &nbsp; &nbsp; iterator.hasNext();) {&nbsp; &nbsp; &nbsp; &nbsp; String fragment = iterator.next();&nbsp; &nbsp; &nbsp; &nbsp; if (!iterator.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

慕的地6264312

你有几个选择。使用迭代器:&nbsp; &nbsp; String message = "Who Framed Roger Rabbit?";&nbsp; &nbsp; List<String> fragments = fragmentMessage(message);&nbsp; &nbsp; Iterator<String> iterator = fragments.iterator();&nbsp; &nbsp; while (iterator.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(iterator.next());&nbsp; &nbsp; &nbsp; &nbsp; if (!iterator.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("All done!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }检查 for/each 循环中的索引:&nbsp; &nbsp; for (String str :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fragments) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(str);&nbsp; &nbsp; &nbsp; &nbsp; // If this item is the last in the list&nbsp; &nbsp; &nbsp; &nbsp; if (fragments.indexOf(str) == fragments.size() - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("All done!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }使用这种方法,如果您在列表中使用字符串,请注意,如果有重复项,这将不会很准确。使用标准 for 循环跟踪索引:&nbsp; &nbsp; for (int i = 0; i < fragments.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(fragments.get(i));&nbsp; &nbsp; &nbsp; &nbsp; if (i == fragments.size() - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("All done!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

POPMUISE

我的最终解决方案如下所示。它基于@k5_答案,归功于他/她/其他人。我使用 if-else 语句仅针对最后一个元素。&nbsp; &nbsp; List<String> fragmentMessage = fragmentMessage(message);&nbsp; &nbsp; for (int i = 0; i < fragmentMessage.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; String fragment = fragmentMessage.get(i);&nbsp; &nbsp; &nbsp; &nbsp; if (message.isEmpty()) { // Only if message == "".&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(createFrame(message));&nbsp; &nbsp; &nbsp; &nbsp; } else if (i == fragmentMessage.size() - 1) { // Last element of the array.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(createLastFrame(fragment));&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(createFrame(fragment)); // Only if message is not the last message.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java