- <p>class Array4 {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String[] args) {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>int [] arr = {22,3,12,4,54,3,2,89};</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("排序前的数组 " );</p><p><span class="Apple-tab-span" style="white-space:pre"> </span><font color="#ff0000">for (int a :arr ) </font>{</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.print(a+" ");<span class="Apple-tab-span" style="white-space:pre"> </span></p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(); //{2,1,3}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span><font color="#0000ff">for (int x = 0;x < arr.length ;x++ ) {</font></p><p><font color="#0000ff"><span class="Apple-tab-span" style="white-space:pre"> </span>for (int y = x;y < arr.length ;y++ ) {</font></p><p><span class="Apple-tab-span" style="white-space:pre"> </span>if (arr[y] < arr[x]) {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span><font color="#9932cc">int<span class="Apple-tab-span" style="white-space:pre"> </span>temp = arr[x];</font></p><p><font color="#9932cc"><span class="Apple-tab-span" style="white-space:pre"> </span>arr[x] = arr[y];</font></p><p><font color="#9932cc"><span class="Apple-tab-span" style="white-space:pre"> </span>arr[y] = temp;</font></p><p> <span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("排序后的数组 " );</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>for (int b :arr ) {</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.print(b+" ");<span class="Apple-tab-span" style="white-space:pre"> </span></p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}<span class="Apple-tab-span" style="white-space:pre"> </span></p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p>}</p>
复制代码
问题一
上面红色的地方(for (int a :arr ))可不可以写成for (int x = 0 ; x <= arr.length;x++ ) 两种表达方式一样吗?
问题二
如果 两种方式可以等价 那蓝色的地方 可不可以用(for (int a :arr ))表示 我试了下 好像不行
问题三
紫色的这块不是很理解 int temp = arr[x];
arr[x] = arr[y]; arr[y] = temp; 谢谢
|
|