作者comodore (Commodore)
看板java
标题[问题] 两个回圈输出的顺序问题
时间Wed May 21 23:05:41 2014
Hi,
小弟写了两个阵列, 分别用回圈印出来, 小弟天真的以为第一个回圈要印完之後,
才会在继续跑第二个回圈, 继续印出来, 但结果却不是这样, 如下所示:
[source]
public static void main(String[] args){
int[] a = {1,2,3};
int[] b = Arrays.copyOf(a, a.length);
for(Integer i:a){
System.err.println("a:"+i);
}
for(Integer i:b){
System.out.println("b"+i);
}
}
[output]
a:1b1
a:2
a:3
b2
b3
请问这是为什麽啊?
谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 220.134.15.92
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/java/M.1400684744.A.13A.html
1F:推 b10030012:我试很正常啊 一回圈完 换另一个 05/21 23:15
2F:→ kingofsdtw:测试後程式正常 05/22 00:36
3F:推 virve:每次跑都这样吗 05/22 00:53
4F:→ comodore:有时候正常, 有时候不正常 05/22 09:23
5F:→ forget2009:我觉得出这样的结果很合理阿,要像你想的那种结果我会 05/22 11:11
6F:→ forget2009:用两个Thread synchronized锁住其中一个印完再解锁 05/22 11:13
7F:→ forget2009:喔 抱歉没看到上面那个b1... 05/22 11:15
8F:推 PsMonkey:他贴出来的 code 哪里有 thread 阿阿阿阿 Orz 05/22 12:23
9F:→ danny8376:感觉这应该是IDE上的console的问题? 05/22 12:34
10F:→ comodore:不好意思, System.err.println("a:"+1); 05/22 20:01
11F:→ comodore:改成 System.out.println("a:"+i); 就不会了 05/22 20:02
※ 编辑: comodore (220.134.15.92), 05/22/2014 20:02:58
12F:推 LPH66:应是 stream 有没有 buffer 的问题, 印象中 out 有 err 没有 05/22 23:49
13F:→ swpoker:没看到err out 阿 05/23 09:57
14F:→ comodore:to 楼上, 原po文内容有误, 修正如推文 05/24 23:14