什么在这个for循环中发生了什么?

时间:2013-08-02 03:38:25

标签: java for-loop try-catch

我不确定这里发生了什么......它弄乱了我的程序。我不确定它是否有某种内存泄漏或只是Eclipse搞砸了。关于最新情况的任何想法?下面是我的代码片段和控制台输出

String Address; //Address is the first three octets of an ip and "i" is the last

for (int i = 0; i <= 255; i++){
        try {
            System.out.println(i);
            Address = Address.concat(String.valueOf(i));
            System.out.println(Address + InetAddress.getLocalHost().getHostAddress());
        } catch (Exception e) {}
}

和控制台输出:

    0
    5.20.86.0
    5.20.86.136
    1
    5.20.86.01
    5.20.86.136
    2
    5.20.86.012
    5.20.86.136
    3
    5.20.86.0123
    5.20.86.136
    4
    5.20.86.01234
    5.20.86.136
    5
    5.20.86.012345
    5.20.86.136
    6
    5.20.86.0123456
    5.20.86.136
    7
    5.20.86.01234567
    5.20.86.136
    8
    5.20.86.012345678
    5.20.86.136
    9
    5.20.86.0123456789
    5.20.86.136
    10
    5.20.86.012345678910
    5.20.86.136
11
5.20.86.01234567891011
5.20.86.136
12
5.20.86.0123456789101112
5.20.86.136
13
5.20.86.012345678910111213
5.20.86.136
14
5.20.86.01234567891011121314
5.20.86.136
15
5.20.86.0123456789101112131415
5.20.86.136
16
5.20.86.012345678910111213141516
5.20.86.136
17
5.20.86.01234567891011121314151617
5.20.86.136
18
5.20.86.0123456789101112131415161718
5.20.86.136
19
5.20.86.012345678910111213141516171819
5.20.86.136
20
5.20.86.01234567891011121314151617181920
5.20.86.136
21
5.20.86.0123456789101112131415161718192021
5.20.86.136
22
5.20.86.012345678910111213141516171819202122
5.20.86.136
23
5.20.86.01234567891011121314151617181920212223
5.20.86.136
24
5.20.86.0123456789101112131415161718192021222324
5.20.86.136
25
5.20.86.012345678910111213141516171819202122232425
5.20.86.136
26
5.20.86.01234567891011121314151617181920212223242526
5.20.86.136
27
5.20.86.0123456789101112131415161718192021222324252627
5.20.86.136
28
5.20.86.012345678910111213141516171819202122232425262728
5.20.86.136
29
5.20.86.01234567891011121314151617181920212223242526272829
5.20.86.136
30
5.20.86.0123456789101112131415161718192021222324252627282930
5.20.86.136
31
5.20.86.012345678910111213141516171819202122232425262728293031
5.20.86.136
32
5.20.86.01234567891011121314151617181920212223242526272829303132
5.20.86.136
33
5.20.86.0123456789101112131415161718192021222324252627282930313233
5.20.86.136
34
5.20.86.012345678910111213141516171819202122232425262728293031323334
5.20.86.136
35
5.20.86.01234567891011121314151617181920212223242526272829303132333435
5.20.86.136
36
5.20.86.0123456789101112131415161718192021222324252627282930313233343536
5.20.86.136
37
5.20.86.012345678910111213141516171819202122232425262728293031323334353637
5.20.86.136
38
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738
5.20.86.136
39
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839
5.20.86.136
40
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940
5.20.86.136
41
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041
5.20.86.136
42
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142
5.20.86.136
43
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243
5.20.86.136
44
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344
5.20.86.136
45
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445
5.20.86.136
46
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546
5.20.86.136
47
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647
5.20.86.136
48
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
5.20.86.136
49
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
5.20.86.136
50
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
5.20.86.136
51
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
5.20.86.136
52
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
5.20.86.136
53
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
5.20.86.136
54
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
5.20.86.136
55
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
5.20.86.136
56
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
5.20.86.136
57
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
5.20.86.136
58
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
5.20.86.136
59
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
5.20.86.136
60
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
5.20.86.136
61
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
5.20.86.136
62
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
5.20.86.136
63
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
5.20.86.136
64
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
5.20.86.136
65
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
5.20.86.136
66
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
5.20.86.136
67
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
5.20.86.136
68
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
5.20.86.136
69
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
5.20.86.136
70
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
5.20.86.136
71
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
5.20.86.136
72
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
5.20.86.136
73
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
5.20.86.136
74
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
5.20.86.136
75
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
5.20.86.136
76
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
5.20.86.136
77
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
5.20.86.136
78
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
5.20.86.136
79
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
5.20.86.136
80
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
5.20.86.136
81
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
5.20.86.136
82
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
5.20.86.136
83
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
5.20.86.136
84
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
5.20.86.136
85
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
5.20.86.136
86
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
5.20.86.136
87
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
5.20.86.136
88
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
5.20.86.136
89
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
5.20.86.136
90
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
5.20.86.136
91
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
5.20.86.136
92
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
5.20.86.136
93
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
5.20.86.136
94
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
5.20.86.136
95
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
5.20.86.136
96
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
5.20.86.136
97
5.20.86.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
5.20.86.136
98
5.20.86.01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
5.20.86.136
99
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
5.20.86.136
100
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
5.20.86.136
101
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
5.20.86.136
102
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
5.20.86.136
103
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
5.20.86.136
104
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
5.20.86.136
105
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
5.20.86.136
106
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
5.20.86.136
107
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
5.20.86.136
108
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
5.20.86.136
109
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
5.20.86.136
110
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
5.20.86.136
111
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
5.20.86.136
112
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
5.20.86.136
113
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
5.20.86.136
114
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
5.20.86.136
115
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
5.20.86.136
116
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
5.20.86.136
117
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
5.20.86.136
118
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
5.20.86.136
119
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
5.20.86.136
120
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
5.20.86.136
121
5.20.86.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
5.20.86.136
122

等等....(我不得不删除大部分内容,因为它超过了最大字符数限制)但是你得到了点

编辑:我把这些System.out.print用于调试我的程序并查看它为什么不起作用,我得到了这个并且无法找出任何解释

1 个答案:

答案 0 :(得分:2)

很简单:

Address = Address.concat(String.valueOf(i));

Address始终有任何价值:5.20.86.0。

在for循环中,您追加i当前值,但不删除以前的值! 因此5.20.86.0123456789105.20.86.012345678910而不是012345678910 11 12 13 14等等。

我希望这是一个很好的解释。不知道你想归档什么,但不是问题。