Difference: 1. foreach is more efficient when the number of loops is unknown or it is troublesome to calculate the number of loops, while for is more efficient in more complex loops; 2. foreach is not supported in loops When adding and deleting operations, the array or collection cannot be modified, but the for loop can.
Related recommendations: "Java Video Tutorial"
First of all, let’s talk about foreach, which is also called enhancement. for loop, foreach is actually a special simplified version of the for loop.
Let’s talk about the writing format of foreach:
for(元素类型 元素名称 : 遍历数组(集合)(或者能进行迭代的)){ 语句 }
Although foreach is a simplified version of the for loop, it does not mean that foreach is more powerful than for. It is easy to use. Foreach is more efficient when the number of loops is unknown or it is troublesome to calculate the number of loops. However, some more complex loops still need to use for loops to be more efficient.
Let’s take a look at the following example:
public static void main(String[] args) { List<String> arr = new ArrayList<String>(); arr.add("你好"); arr.add("我好"); arr.add("大家好"); //foreach循环 for(String str : arr){ //这里的str就是为了获取每次循环的arr中的值 System.out.println(str); //就相当于 String str=arr[i] } }
But in comparison, it is a little troublesome for us to use a for loop to input
public static void main(String[] args) { List<String> arr = new ArrayList<String>(); arr.add("你好"); arr.add("我好"); arr.add("大家好"); //for循环 for(int i=0;i<arr.size();i++){ System.out.println(arr.get(i)); //要获取list中元素需要用get方法 } }
In addition to this ordinary collection, you can also use key-value pairs like map
For example:
public static void main(String[] args) { Map<String,String> mapstr = new HashMap<String,String>(); mapstr.put("王", "男"); mapstr.put("李", "男"); mapstr.put("张", "女"); //entrySet方法是为了获取键值对的集合 for(Map.Entry<String, String> s : mapstr.entrySet()){ //这里的Map.Entry<String, String>其实就是一个类型 用来表示键值对的类型 System.out.println("key="+s.getKey()); //这里其实还是相当于 s=maostr.entrySet,只不过s存储的是键值对。 System.out.println("value="+s.getValue()); //所以可以用get方法获取出来存储的键值对。 } }
In addition, foreach does not support adding and deleting operations in a loop, because when using foreach When looping, the array (collection) has been locked and cannot be modified, otherwise java.util.ConcurrentModificationExceptionException
For example:
public static void main(String[] args) { List<String> arr = new ArrayList<String>(); arr.add("你好"); arr.add("我好"); arr.add("大家好"); //foreach循环 for(String str : arr){ System.out.println(str); arr.add("1"); //对arr进行添加 } }
So the summary is as follows:
foreach is suitable for just traversing a collection or array, while for is more efficient in more complex loops.
Foreach cannot modify the array or collection (add or delete operations). If you want to modify it, you must use a for loop.
So the for loop is more flexible in comparison.
The above is the detailed content of What is the difference between for and foreach in java?. For more information, please follow other related articles on the PHP Chinese website!