Java foundation 4 review

Selection Sort (Sort ring):
public class demo1 {
    public static void main(String[] args) {
         // TODO Auto-generated method stub
         int[] num=  {2,5,7,8,123,46,13,765,14,363,752,34};
         for(int i=0;i<num.length-1;i++){
             for(int j=i+1;j<num.length;j++) {
                 if(num[i]>num[j]) {
                      int imp;
                      imp=num[i];
                      num[i]=num[j];
                      num[j]=imp;
                 }
             }
         }
         for(int i=0;i<num.length;i++) {
             System.out.println(num[i]);
         }
    }   
}
Bubble Sort:
public class demo2 {
//冒泡排序:从大到小;从后往前。
    public static void main(String[] args) {
         // TODO Auto-generated method stub
         int[] num=  {1,25,467,6,24,57,36,8,28,879,54};
         for(int i=0;i<num.length;i++) {
             for(int j=num.length-1;j>i;j--) {
                 if(num[j]>num[j-1]) {
                      int temp=num[j-1];
                      num[j-1]=num[j];
                      num[j]=temp;
                 }
                 /*if(num[j]<num[j-1]){
                     int temp=num[j-1];
                     num[j-1]=num[j];
                     num[j]=temp;
                     
                 } 从小到大*/
             }
                 
         }
         for(int i=0;i<num.length;i++) {
             System.out.println(num[i]);
         }
    }
}
Quick Sort:
import java.util.Arrays;
public class demo4 {
// 快速排列:使用Arrays.sort();方法,仅能排列从小到大。
    public static void main(String[] args) {
         // TODO Auto-generated method stub
         int[] num= {2,5,1,23,4,25,75,35,58,243};
         Arrays.sort(num);
         for(int i=0;i<num.length;i++) {
             System.out.println(num[i]);
         }
    }
}

Two-dimensional array (understand)

public class demo3 {
    public static void main(String[] args) {
//  将oldarr数组中除0以外的数重新组成一个新数组并打印出来。
         // TODO Auto-generated method stub
         int[] oldarr=  {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
         int num=0;
         for(int i=0;i<oldarr.length-1;i++) {
             if(oldarr[i]!=0) {
                 num++;
             }
         }
         int[] newarr=new int[num];
         int n=0;
         for(int i=0;i<oldarr.length-1;i++) {
             if(oldarr[i]!=0) {
                 newarr[n]=oldarr[i];
                 n++;
             }
         }
         for(int i=0;i<newarr.length-1;i++) {
             System.out.println(newarr[i]);
         }
    }   
}

method

public class demo{
    public satatic void main(String[] args){
        fun();  //调用方法,方法名();
    }
    //定义方法。
    public static void fun{
        for(int i=0;i<10;i++){
            System.out.println("Hello World!");
        }
    }
}
  1. No method is not a method parameter return value. Naming conventions: numbers, letters, underscores, $, can not start with a number. You can not use the keyword
// 定义方法。
public static void fun(){
    System.out.println("我是Fun方法。")
}
public static void fun2(){
    System.out.println("我是Fun2方法。")
    fun();//调用fun方法
}
  1. 2. There are parameters but no return value.
public static void 方法名(形参列表){
            方法体;
        }

Call: the method name (argument list) the number of arguments must be the same as the number of participating shaped, and reference argument type must match the type of the shape.

    fun3("#"); //调用fun方法。
}
public static void fun3(String n){//n="#"
    for(int i=0;i<=10;i++){
        System.out.print(n);
    }
}
    fun(10,5);
}
//判断两个数的最大数,并输出最大数。
public static void fun4(int a,int b){
    if(a>b){
        System.out.println(a);
    }else{
        System.out.println(b);
    }
}

3. There are parameters and return values.

  public static 返回的数据类型 方法名(参数列表{
        return;
    }

Call: variable name = data type method name (argument list);

Recursive method

Internal method calls itself

public class demo10 {
    public static void main(String[] args) {
    //  完成5!的阶乘。
        int a = fun(5);
        System.out.println(a);
    }
    public static int fun(int i) {
        if(i==1) {
           return 1;
        }else {
           return i*fun(i-1);
        }
    }
}

Guess you like

Origin www.cnblogs.com/wuliqqq/p/11291454.html