加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

结肠在Java中是什么意思?

发布时间:2020-12-15 00:26:43 所属栏目:Java 来源:网络整理
导读:在 Java中冒号是什么意思?我有这个: public static ListString findAllAnagrams(ListString words) { ListString result = new LinkedListString(); for(String i : words){ for (String j : words){ if (result.contains(i)) { break; } else if (i == j)
在 Java中冒号是什么意思?我有这个:
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 (String i: words)

意思是相同的东西

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

约书亚·布洛赫(Joshua Bloch)在他有价值的Java中的第46项中说:

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);
}

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读