您好,欢迎访问一九零五行业门户网

Java中for循环和foreach循环有什么区别?

(1)遍历元素首先,我们一一段代码为例:
string[] array = {"1", "2", "3"}; for (string i : array) { system.out.println(i); } arraylist<string> list = new arraylist<>(); list.add("111"); list.add("222"); list.add("333"); for (string i : list) { system.out.println(i); }
遍历后结果如下:
1
2
3
111
222
333
结果毫无疑问。
我们再来看看编译后的源码(idea自带,在target包里打开你的类源码文件即可):
string[] array = new string[]{"1", "2", "3"}; string[] var2 = array; int var3 = array.length; for(int var4 = 0; var4 < var3; ++var4) { string i = var2[var4]; system.out.println(i); } arraylist<string> list = new arraylist(); list.add("111"); list.add("222"); list.add("333"); iterator var7 = list.iterator(); while(var7.hasnext()) { string i = (string)var7.next(); system.out.println(i); }
可见,遍历数组使用的是原始for循环,集合的话使用的是iterator迭代器。
(2)删除元素哦的k!接下来我们来删除元素:
使用for循环:
arraylist<string> list = new arraylist<>(); list.add("111"); list.add("222"); list.add("333"); log.info(list.tostring()); for (int i = 0; i <list.size(); i++) { list.remove("222"); } log.info(list.tostring());
结果:
11:11:52.532 [main] info com.xiaolinge.com.hello.helloword - [111, 222, 333]
11:11:52.539 [main] info com.xiaolinge.com.hello.helloword - [111, 333]
显然成功!
使用foreach:
arraylist<string> list = new arraylist<>(); list.add("111"); list.add("222"); list.add("333"); log.info(list.tostring()); for (string i : list) { list.remove("222"); } log.info(list.tostring());
结果:
11:50:48.333 [main] info com.xiaolinge.com.hello.helloword - [111, 222, 333]
exception in thread "main" java.util.concurrentmodificationexception
at java.util.arraylist$itr.checkforcomodification(arraylist.java:909)
at java.util.arraylist$itr.next(arraylist.java:859)
at com.xiaolinge.com.hello.helloword.main(helloword.java:30)
显然木有成功!
原因:
迭代器内部的每次遍历都会记录list内部的modcount当做预期值,然后在每次循环中用预期值与list的成员变量modcount作比较,但是普通list.remove调用的是list的remove,这时modcount++,但是iterator内记录的预期值=并没有变化,所以会报错。
如果想要删除元素的话需要使用迭代器内部的remove方法:
arraylist<string> list = new arraylist<>(); list.add("111"); list.add("222"); list.add("333"); log.info(list.tostring()); iterator<string> it = list.iterator(); while (it.hasnext()){ string next = it.next(); //if外使用list的remove方法还是会报错的 if(next.equals("222")){ it.remove();//这里使用的是迭代器里面的remove()方法, // 当然如果使用list的remove方法在此删除质地感元素的话是成功的,比如:list.remove("222") } } log.info(list.tostring());
结果:
12:06:14.042 [main] info com.xiaolinge.com.hello.helloword - [111, 222, 333]
12:06:14.046 [main] info com.xiaolinge.com.hello.helloword - [111, 333]
(3)修改元素使用原始for:
arraylist<string> list = new arraylist<>(); list.add("111"); list.add("222"); list.add("333"); log.info(list.tostring()); for (int i = 0; i <list.size(); i++) { list.set(i,"444"); } log.info(list.tostring());
结果:
12:12:56.910 [main] info com.xiaolinge.com.hello.helloword - [111, 222, 333]
12:12:56.915 [main] info com.xiaolinge.com.hello.helloword - [444, 444, 444]
哦的k!可以修改元素;
使用foreach:
arraylist<string> list = new arraylist<>(); list.add("111"); list.add("222"); list.add("333"); log.info(list.tostring()); for (string i : list) { i="444"; } log.info(list.tostring());
结果:
12:34:47.207 [main] info com.xiaolinge.com.hello.helloword - [111, 222, 333]
12:34:47.211 [main] info com.xiaolinge.com.hello.helloword - [111, 222, 333]
看到咯,不行的哦。
辣么,修改元素不行,修改元素的属性可不可以呢?让我们来看下吧。
(4)foreach修改元素属性(for就不测试了)
创建一个学生类:
public class student { private int age; public int getage() { return age; } public void setage(int age) { this.age = age; } public string getname() { return name; } public void setname(string name) { this.name = name; } private string name; public student(){}; public student(int age,string name){ this.age=age; this.name=name; } }
哦的k,接下来测试代码:
student student=new student(1,"huge"); student student1=new student(1,"xiaoyao"); list<student> studentlist=new arraylist<student>(); studentlist.add(student); studentlist.add(student1); system.out.println(student.getname()); system.out.println(student1.getname()); for(student stu:studentlist) { stu.setname("jingtian"); } system.out.println(student.getname()); system.out.println(student1.getname());
结果:
huge
xiaoyao
jingtian
jingtian
484很神奇!修改不了对象,却可以修改对象的属性。
以上就是java中for循环和foreach循环有什么区别?的详细内容。
其它类似信息

推荐信息