How to Iterate over Array in Java 1.5 using foreach loop Example

Though there are many ways to loop over Array in Java, including classical for loop, while loop with length counter, nothing matches elegance of Java 1.5 foreach loop, which makes iteration super easy and super cool. When we need to iterate through each element of array and has to perform some operation on them e.g. filtering, transformation or simply printing, foreach loop comes to it's full glory. There is not much difference between traditional for loop and Java 1.5 foreach loop, except that former has counter and condition, which is checked after each iteration, on the other hand foreach loop does this task under the hood. But this elegance comes at cost of control, as you can not control iteration using counter, so depending upon your need, you have to choose between them. Just like in last article, we learned about best way to Iterate over each entry in HashMap, In this article, we will take a look at iteration over String array using both Java 1.5 foreach loop and traditional for loop.


Java 1.5 foreach loop example

Java String array Iteration example using foreach loopHere is the complete code example of How to iterate over String array in Java. Our array contains list of programming languages, and we will loop over them and print there name. In case of classical for loop, we will only go over till the middle of array, just to show the power of counter in condition.


/** * * Best way to loop over array in Java. Though Java 1.5 foreach loop * is most elegant way of iterating over array, it doesn't provide any * counter, which is available in classic for loop. So, depending upon, whether * you need a counter or not, you can decide between Java 1.5 foreach or traditional * for loop. * * @author Javin */public class JavaForEachOverArray {    public static void main(String args[]) {             String[] languages = {"Java", "Scala", "C++", "Ruby", "Python", "Perl"};               // looping over array using foreach loop        System.out.println("Iterating over String array using Java 1.5 foreach loop");        for(String str : languages){            System.out.println(str);        }               // looping over classical for loop        System.out.println("Looping over String array using for loop");        for(int i=0; i<languages.length/2; i++){            System.out.println(languages[i]);        }          }      }Output:Iterating over String array using Java 1.5 foreach loopJavaScalaC++RubyPythonPerlLooping over String array using for loopJavaScalaC++


That's all about how to iterate over String array in Java. You can use same technique to loop over any primitive or object array e.g. Integer, Float, Double or Boolean arrays. I personally prefer foreach loop over other looping construct, if I have to deal with all the elements one by one, other wise, you can use while, for or do while to traverse any array in Java.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值