结肠在Java中意味着什么? [英] What Does The Colon Mean In Java?

查看:196
本文介绍了结肠在Java中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结肠在Java中意味着什么?我有这个:

What does the colon mean in Java? I have this:

public static List<String> findAllAnagrams(List<String> words) {
    List<String> result = new LinkedList<String>();
    for(String i : words){
        for (String j : words){
            if (result.contains(i)) {
                break;
            }
            else if (i == j) {

            } else {
                if (areAnagrams(i,j)){
                    result.add(i);
                    System.out.println(result);
                }
            }
        }
    }          
    return result;
}


推荐答案

这意味着一件事,它是一个增强的for循环。

It means one thing, it is an enhanced for loop.

for (String i: words) 

for (int i = 0; i < words.length; i++) {
    //
}

Joshua Bloch ,在他的值得阅读 Effective Java 的第46项中,说明如下:

Joshua Bloch, in Item 46 of his worth reading Effective Java, says the following:


在1.5版中引入的for-each循环通过完全隐藏迭代器或索引变量来消除混乱和错误的机会。由此产生的习语同样适用于集合和数组:

The for-each loop, introduced in release 1.5, gets rid of the clutter and the opportunity for error by hiding the iterator or index variable completely. The resulting idiom applies equally to collections and arrays:

迭代集合和数组的首选习惯用法

The preferred idiom for iterating over collections and arrays



for (Element e : elements) {
    doSomething(e);
} 




当你看到冒号(:)时,将其读作in。
因此,上面的循环读作对于元素中的每个元素e。注意,即使对于数组,使用for-each循环也没有性能损失。事实上,在某些情况下,它可能比普通的for循环提供轻微的性能优势,因为它只计算一次数组索引的限制。虽然您可以手动执行此操作(第45项),但程序员并不总是这样做。

When you see the colon (:), read it as "in." Thus, the loop above reads as "for each element e in elements." Note that there is no performance penalty for using the for-each loop, even for arrays. In fact, it may offer a slight performance advantage over an ordinary for loop in some circumstances, as it computes the limit of the array index only once. While you can do this by hand (Item 45), programmers don’t always do so.

这篇关于结肠在Java中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆