0

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

我的要求是,如果列表中包含值“M”,我想终止 for 循环中的条件检查(等于所示的检查),但想继续该 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 列表。

上面的程序是为了简单,实际上列表中会包含员工对象。

4

5 回答 5

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();
    }
}
于 2013-04-05T11:07:11.293 回答
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");
于 2013-04-05T11:08:37.620 回答
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");
}

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

// 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)。

于 2013-04-05T11:19:47.197 回答
0

加入continue;If条件

if (list.get(i).equals("M")) {
                flag = "M";
                continue;
}
于 2013-04-05T11:06:10.050 回答
0

改变

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

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

注意使用,"M".equals(flag)所以我们不会抛出NPEwhen flagis null

于 2013-04-05T11:08:16.917 回答