Java Base Array Practice Highlights

Source: Internet
Author: User

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

The first mistake I've written

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);        }    }}
Correct practice
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);        }    }}
Merging array topics

First, two arrays are prepared, their lengths are random numbers between 5-10, and the two arrays are initialized with a random number
Then we prepare the third array, the length of the third array is the first two, and the second
Merges the first two arrays into the third array by system.arraycopy

Java Base Array Practice Highlights

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.