如何在 Java 中编写通用的 foreach 循环?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6283991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:11:45  来源:igfitidea点击:

How do I write a generic foreach loop in Java?

javaforeach

提问by aioobe

I have the following code so far:

到目前为止,我有以下代码:

/*
 * This method adds only the items that don't already exist in the
 * ArrayCollection. If items were added return true, otherwise return false.
 */
public boolean addAll(Collection<? extends E> toBeAdded) {

    // Create a flag to see if any items were added
    boolean stuffAdded = false;


    // Use a for-each loop to go through all of the items in toBeAdded
    for (something : c) {

        // If c is already in the ArrayCollection, continue
        if (this.contains(c)) { continue; }     

            // If c isn't already in the ArrayCollection, add it
            this.add(c)
            stuffAdded = true;
        }

        return stuffAdded;
    }
}

My question is: what do I replace something (and c) with to make this work?

我的问题是:我应该用什么来替换某些东西(和 c)来完成这项工作?

回答by aioobe

Something like this should do:

这样的事情应该做:

// Use a for-each loop to go through all of the items in toBeAdded
for (E c : toBeAdded) {

    // If c is already in the ArrayCollection, continue
    if (this.contains(c)) {
        continue;
    }

    // If c isn't already in the ArrayCollection, add it
    this.add(c);

    stuffAdded = true;
}


The general form is:

一般形式为:

for (TypeOfElements iteratorVariable : collectionToBeIteratedOver) `

回答by Ryan Amos

It's pretty simple to write a foreach in Java.

用 Java 编写 foreach 非常简单。

for(ObjectType name : iteratable/array){ name.doSomething() }

You can do foreach with either an iteratable or array. Be aware that if you don't typecheck your iterator (Iterator), then you need to use Object for ObjectType. Otherwise use whatever E is. For example

您可以使用可迭代或数组来执行 foreach。请注意,如果您没有对您的迭代器(Iterator)进行类型检查,那么您需要使用 Object for ObjectType。否则使用任何 E。例如

ArrayList<MyObject> al = getObjects();
for(MyObject obj : al){
  System.out.println(obj.toString());
}

For your case:

对于您的情况:

   for(E c : toBeAdded){
        // If c is already in the ArrayCollection, continue
        if( this.contains(c) ){ continue;}     

        // If c isn't already in the ArrayCollection, add it
        this.add(c)
        stuffAdded = true;
    }

回答by Nazarii Bardiuk

public boolean addAll(Collection<? extends E> toBeAdded) {
    boolean stuffAdded = false;
    for(E c : toBeAdded){
        if(!this.contains(c)){
             this.add(c)
             stuffAdded = true;
        }
    }
    return stuffAdded;
}

Also take a look at Collections.addAll

也看看 Collections.addAll

回答by Nazarii Bardiuk

You can write the foreach loop in 2 ways and they are equivalent.

您可以用 2 种方式编写 foreach 循环,它们是等效的。

List<Integer> ints = Arrays.asList(1,2,3);
int s = 0;
for (int n : ints) { s += n; }


for (Iterator<Integer> it = ints. iterator(); it.hasNext(); ) {
    int n = it.next();
    s += n;
}

回答by Ethan Aleman-Meza

E is the collection, c is the variable inside the loop for(E c : toBeAdded ) ...

E 是集合,c 是循环内的变量 for(E c : toBeAdded ) ...