【问题标题】:Java ":" operator in a For expressionFor 表达式中的 Java ":" 运算符
【发布时间】:2013-08-09 05:53:41
【问题描述】:

在为别人的代码编写单元测试时,我遇到了“:”运算符的一个有趣用法。它看起来像这样:

for(Class classInstance : instanceOfOtherClass){

//Do some irrelevant stuff

}

我从未见过没有“?”而单独使用“:”运算符,就像编写三元语句时那样。我已经做了相当多的谷歌搜索,但似乎找不到任何关于我应该如何阅读这篇文章的明智答案......

我是否过于复杂了?有人见过吗?

【问题讨论】:

  • 我很好奇你用了什么搜索词。
  • 它是Java 5 中加入的,哦,十年前所以不会在旧网页中提及。我喜欢这个链接版权 2000-2001:jcp.org/aboutJava/communityprocess/jsr/tiger/enhanced-for.html
  • 它与看起来像 d = (a == b) ? c : e; 的 inline-if 无关
  • @SotiriosDelimanolis 让我满足你的好奇心。我从冒号开始,这是我在该行中唯一识别的东西。我很快就迷失了方向,但是在我的搜索历史中,有诸如“colon for loop”、“如何使用 java 冒号”和“colon plzrspnd”之类的内容。

标签: java for-loop junit ternary-operator ternary


【解决方案1】:

它叫for each

for(Class object : Objects){
    //it will iterate through all the objects contained in Objects
    //Objects can be an array, a list, etc.
    //object Class/Type must match the class of the objects contained in Objects

}

例如,这将迭代字符串中的字符

for(char c : string.toCharArray()){
    System.out.println(c);
}

您可以在这里找到这款 for 和经典款的对比:

Fastest way to iterate an Array in Java: loop variable vs enhanced for statement

【讨论】:

    【解决方案2】:

    为了将来参考,您是否输入过:

    java 7 for 循环:

    进入谷歌,第二个结果会对你有所帮助。为了您的辩护,如果您不将“7”放在那里,那么找到解决方案会更加困难(不是因为它是在 java 7 中引入的,而是在 java 5 中,而是因为如果它当前受支持,那么把 7 给你找到最新文档的可能性更高)。这是enhanced for loopfor-each loop 的示例。

    简而言之:

    EnhancedForStatement:

    for ( FormalParameter : Expression ) Statement
    

    其中Expression 必须是iterable 数组类型。

    简单来说(数组示例,但请注意可以使用implements Iterable 的任何内容):

    String[] words = new String[]{"This","is","the","end"};
    
    for (int i = 0; i < words.length; i++)
    {
          System.out.println(words[i]);
    }
    

    用for-each循环编写的是:

    for (String s : words)
    {
          System.out.println(s);
    }
    

    如果您想了解效率,请查看this post

    【讨论】:

    • Java 5 中引入了 forEach 循环。
    【解决方案3】:

    首先,它可以与数组一起使用。考虑一个 MyClass 类型的对象数组:

    MyClass[] objects = new MyClass[SIZE];
    

    像这样迭代:

    for(MyClass object : objects) {
      // do something with the individual object.
    }
    

    其次,您可以将它与可迭代集合一起使用。考虑单个元素的类MyClass,并通过定义Iterable 的子类来将它们集合起来:

    class MyClassList implements Iterable<MyClass> {
      // define necessary methods to implement the Iterable interface!
    }
    

    现在我们可以制作一个集合了:

    MyClassList objects = new MyClassList();
    // fill the collection somehow.
    

    然后迭代:

    for(MyClass object : objects) {
      // do something with the individual object
    }
    

    最后,请注意,这种结构的主要缺点是您无法访问元素在迭代中的位置。对于您正在处理的给定object,如果您想知道它的位置,您应该使用 for 循环:

    for(int i = 0; i < objects.size(); i++) {
      // Here we have access to the position i
    }
    

    或者使用临时索引:

    int i = 0;
    for(MyClass object : objects) {
      // do something with access to position i
      i++;
    }
    

    我个人不喜欢最后的 hack,只会退回到经典的 for 循环。希望这会有所帮助!

    【讨论】:

      【解决方案4】:
      // Infinite
       for ( ; ; ) {
      
         // Code
       }
      
      //Array<Object>
       int[] numbers = 
               {1,3,6,9,12,15,18};
           for (int item : numbers) {
               System.out.println("Count is: " + item);
           }
      
      //List<Object>
        ArrayList<Object> objectList ;//I assume it's perfectly initialized and filled
      
        for (Object temp : objectList) {
          System.out.println(temp);
      }
      

      【讨论】:

        【解决方案5】:

        使用示例:

        String[] words = {"one", "two", "three", "one", "two", "three"};
        for(String one : words){
        System.out.println("found it");
        }
        

        现在,我敢打赌,你认为当你运行它时,它会在屏幕上打印两次“找到它”。一次,对于 String = "one" 的每个实例......但你错了。 这将打印六次“找到它”! 原因是它正在搜索另一个实例的一个对象! 因为 String Array 中有六个 String 对象的实例,所以它会多次执行增强 for 循环中的语句。 希望这个丰富多彩的解释能让您更容易理解。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-03
          • 1970-01-01
          • 2020-06-23
          • 2023-03-27
          • 1970-01-01
          • 2016-07-03
          相关资源
          最近更新 更多