Java basic array practice collection

array inversion

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);
        }
    }

}

selection sort

My mistake at the beginning

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);
        }
    }

}

the right way

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);
        }
    }

}

Bubble Sort

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);
        }
    }

}

Merge arrays

topic

First prepare two arrays, their lengths are random numbers between 5-10, and use random numbers to initialize these two arrays
Then prepare a third array, the length of the third array is the sum of the first two
through the System .arraycopy merges the first two arrays into a third array

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324797449&siteId=291194637