java基礎數組練習集錦

來源:互聯網
上載者:User

標籤:sys   rand   rgs   第一步   temp   迴圈   ack   random   arrays   

數組反轉
package zsc.czy.array;public class A {    public static void main(String[] args) {  //最笨的方法        int a[] = new int[5];        for(int i = 0;i<a.length;i++){            a[i] = (int) (Math.random() * 100);            System.out.println(a[i]);        }        System.out.println();        int b[] = new int[5];        for (int i = 0; i < b.length; i++) {            b[i]= a[a.length-i-1];            System.out.println(b[i]);        }    }}
package zsc.czy.array;public class B {        //首尾交換    public static void main(String[] args) {        int a[] = new int[5];        for(int i = 0;i<a.length;i++){            a[i] = (int) (Math.random() * 100);            System.out.println(a[i]);        }        System.out.println();        int temp =0;        for (int i = 0; i < a.length/2; i++) {             temp = a[i];             a[i]= a[a.length-1-i];             a[a.length-1-i] = temp;        }        for(int i :a){            System.out.println(i);        }    }}
選擇排序

一開始我的錯誤寫法

package zsc.czy.arraySort;public class QuickSort {/** *我第一次寫,是這樣的,是錯誤的,但還不知道錯在哪裡 *  * @param args */    public static void main(String[] args) {        int a[] = new int[5];        for (int i = 0; i < a.length; i++) {            a[i] = (int) (Math.random() * 100);            System.out.println(a[i]);        }        System.out.println();        int temp=0;        for (int i = 0; i < a.length-1; i++) {            for(int j = i;j<a.length-1;j++){                if(a[j]>a[j+1]){                    temp = a[j];                    a[j]=a[j+1];                    a[j+1]=temp;                }            }        }        for(int i:a){            System.out.println(i);        }    }}
正確做法
package zsc.czy.arraySort;public class QuickSort {/** * 選擇法排序的思路: 把第一位和其他所有的進行比較,只要比第一位小的,就換到第一個位置來 比較完後,第一位就是最小的 然後再從第二位和剩餘的其他所有進行比較,只要比第二位小,就換到第二個位置來 比較完後,第二位就是第二小的 以此類推 *  * @param args */    public static void main(String[] args) {        int a[] = new int[5];        for (int i = 0; i < a.length; i++) {            a[i] = (int) (Math.random() * 100);            System.out.println(a[i]);        }        System.out.println();        int temp=0;        for (int i = 0; i < a.length-1; i++) {            for(int j = i+1;j<a.length;j++){                if(a[j]<a[i]){                    temp = a[i];                    a[i]=a[j];                    a[j]=temp;                }            }        }        for(int i:a){            System.out.println(i);        }    }}
冒泡排序
package zsc.czy.arraySort;public class BubbleSort {/* * 冒泡法排序的思路:  * 第一步:從第一位開始,把相鄰兩位進行比較  * 如果發現前面的比後面的大,就把大的資料交換在後面,迴圈比較完畢後,最後一位就是最大的 * 第二步: 再來一次,只不過不用比較最後一位  * 以此類推 */    public static void main(String[] args) {        int a[] = new int[5];        for (int i = 0; i < a.length; i++) {            a[i] = (int) (Math.random() * 100);            System.out.println(a[i]);        }        System.out.println();        for(int i=0;i<a.length-1;i++){            for(int j=0;j<a.length-i-1;j++){                if(a[j]>a[j+1]){                    int temp = a[j];                    a[j]=a[j+1];                    a[j+1]=temp;                }            }        }        for(int i :a){            System.out.println(i);        }    }}
合并數組題目

首先準備兩個數組,他倆的長度是5-10之間的隨機數,並使用隨機數初始化這兩個數組
然後準備第三個數組,第三個數組的長度是前兩個的和
通過System.arraycopy 把前兩個數組合并到第三個數組中

java基礎數組練習集錦

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.