Java:如何在for循环中终止条件检查

时间:2013-04-05 11:04:03

标签: java

我有一个数组列表,如图所示。

我的要求是,如果List中包含值“M”,我想在for循环中终止条件检查(等于检查,如图所示),但是想要继续循环中的进一步操作

这是我的程序

package com;

import java.util.ArrayList;
import java.util.List;

public class Jai {
    public static void main(String args[]) {
        String flag = null;
        ArrayList<String> list = new ArrayList<String>();

        list.add("M");
        list.add("M");
        list.add("M");
        list.add("F");
        list.add("M");
        list.add("M");
        list.add("M");
        list.add("F");

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals("M")) {
                flag = "M";
            } else {
                flag = "F";
            }
            // this is to indicate that i need to continue further operations
            // inside for loop
            System.out.println("Hi");

        }

        if (flag.equals("M"))
            System.out.println("This is M List");
        else
            System.out.println("This is F List");

    }
}

列表中包含值M,因此我想将其视为M列表。

上述程序适用于Simplicity,实际上List中将包含Employee Objects。

5 个答案:

答案 0 :(得分:2)

如果我理解正确,您想测试列表是否包含“M”。干净的方法不是使用for循环,而是简单地调用

if (list.contains("M"))

要回答你的问题,你可以使用

突破循环
if (list.get(i).equals("M")) {
    flag = "M";
    break;
}

如果你实际上只想在元素不是M的情况下在循环中做某事,那么执行

for (int i = 0; i < list.size(); i++) {
    if (list.get(i).equals("M")) {
        flag = "M";
    } 
    else {
        flag = "F";
        doSomethingOnlyForNonMElements();
    }
}

答案 1 :(得分:2)

我认为,这就是你要找的东西

    boolean hasSeenM = false;

    for (int i = 0; i < list.size(); i++) {
        if ( !hasSeenM && list.get(i).equals("M")) {
            hasSeenM = true;
        }
        // this is to indicate that i need to continue further operations
        // inside for loop
        System.out.println("Hi");

    }

    if (hasSeenM)
        System.out.println("This is M List");
    else
        System.out.println("This is F List");

答案 2 :(得分:1)

我建议按照以下方式进行操作

// Indicates whether the list has M or not
boolean hasM = false;

// Go over the entire list
for (int i = 0; i < list.size(); i++) {
    // Check if we found M within the list, if not, proceed
    // to checking if the current item has M in it.
    if (!hasM && list.get(i).equals("M")) {
        // The current item is M, set hasM to true
        hasM = true;
    }

    // this is to indicate that i need to continue further operations
    // inside for loop
    System.out.println("Hi");
}

检查布尔值比比较字符串更便宜。 if必须是该循环的一部分,除非您愿意打破它并继续另一个循环,如下所示排除该检查

// Indicates whether the list has M or not
boolean hasM = false;

// Remember the position of iteration
int i;

// Go over the list, until we find M
for (i = 0; i < list.size() && !hasM; i++) {
    // Since this loop will only iterate until M is found,
    // we can remove the check for whether M was found or not
    if (list.get(i).equals("M")) {
        // The current item is M, set hasM to true
        hasM = true;
    }

    // this is to indicate that i need to continue further operations
    // inside for loop
    System.out.println("Hi");
}

// Continue in a different loop. If this loop iterates, it
// will be after M was found.
for (; i < list.size(); i++) {
    // Do stuff after M was found
}

如JB Nizet的回答所述,您可以使用list.contains("M")来测试列表在迭代之前是否具有M,但是除非迭代本身需要这样做,否则会不必要地将方法复杂度增加到O(2n)而不是O(n)。

答案 3 :(得分:0)

在If条件

中添加continue;
if (list.get(i).equals("M")) {
                flag = "M";
                continue;
}

答案 4 :(得分:0)

更改

for (int i = 0; i < list.size(); i++) { 

for (int i = 0; i < list.size() && !"M".equals(flag); i++) {

请注意使用"M".equals(flag),因此NPEflag时我们不会抛出null