java function and array

1. Function

Function format:

Modifier return value type function name (parameter type formal parameter 1, parameter type formal parameter 2, ...)

    {

           execute statement;

           return return value;

    }

 

Return value type: The type of the operation result, if the function has no return value, it is represented by void

Function name: identifier, the name can be arbitrarily chosen

Formal parameter: is a variable used to store the actual parameters passed in by the calling function

Actual parameter: The specific value passed to the formal parameter

Return value: returned to the caller

 

illustrate:

1) The main method is the entry point of the program, which is called by the virtual machine. The methods cannot be nested, and the methods are used by calling;

2) For the case that the function has no specific return value, the return value type is represented by the keyword void, then the return statement in the function can be omitted if it is not written in the last line;

3) Function overloading: In the same class, there is more than one function with the same name, and the parameter list or parameter type of the function is different

  

example

[root@bch04 java]# cat Demo9.java 
public class Demo9 {
    public static void main(String[] args) {
    int i = 10;
    int j = 20;
    int max = getMax(i,j);
    System.out.println(max);
    }
    
    public static int getMax(int x,int y) {
    int result;
    if (x>=y) {
        result = x;
    } else {
        result =y;
    }
    return result;
    }
 
}
 
//operation result
[root@bch04 java]# java Demo9
20

 

[root@bch04 java]# cat Demo10.java 
public class Demo10 {
    public static void main(String[] args) {
        getResult(5);
 
}
    public static void getResult(int x) {
        System.out.println( x*8);
}
 
}
 
//operation result
[root@bch04 java]# java Demo10
40


[root@bch04 java]# cat Demo11.java 
public class Demo11 {
    public static void main(String[] args) {
    draw(10,20);
    System.out.println(equalas(10,20));
 
}
    public static void draw(int width,int height) {
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                System.out.print("*");
            }
        System.out.println(); 
        } 
    }  
    public static boolean equalas(int a, int b) {
        return a==b?true:false;
        }
 
}
 
//operation result
[root@bch04 java]# javac Demo11.java 
[root@bch04 java]# java Demo11
********************
********************
********************
********************
********************
********************
********************
********************
********************
********************
false


  

2. Array

1. Definition

Format one:

element type[] array name = new element type [number of elements or array length];

Example: int[] arr = new int[5];

 

Format two:

elementtype[] arrayname = new elementtype[]{element, element, ...};

Example: int[] arr = new int[]{3,5,1,7};

      int[] arr = {3,5,1,7};

Features: Directly specify the length of the array and the content of the elements in the array

 

illustrate:

1) Left half: declare array variables, specify the element type of the array, and the array identifier

2) Right half: create an array, use the keyword new, generate a container entity in memory, and open up the space size

3) After creating the array, you cannot modify the size of the array, you can use the length property to get the size of the array

 

 

2. Initialization

method one:

int[] arr3=new int[3];
arr3[0]=1;
arr3[1]=5;
arr3[2]=6;


Method 2: Do not use operator new

int[] arr = { 1, 2, 3, 4, 5 };
int[] arr2 = new int[] { 1, 2, 3, 4, 5 };


 

3. Array Traversal

public static void main(String[] args) {
       int[] x = { 1, 2, 3 };
       for (int y = 0; y < x.length; y++) {
           System.out.println(x[y]);
       }
    }

 

example

//selection sort
[root@bch04 java]# cat Demo11.java 
public class Demo11 {
    public static void main(String[] args) {
    int[] arr = new int[3];
    arr[0] = 1;
    arr[1] = 3;
    arr[2] = 2;
    int max = arr[0];
    for (int i=0; i<arr.length-1; i++) {
        for (int j=i+1; j<arr.length; j++) {
            if (arr[i] > arr[j]) {
                 max = arr[i];
                 arr[i] = arr[j];
                 arr[j] = max;
                }
 
            }
        }
        System.out.println(arr[0] +"\n" +arr[1] +"\n" +arr[2]);
   }
 
}
 
//运行结果
[root@bch04 java]# java Demo11
1
2
3

 

//冒泡排序
[root@bch04 java]# cat Demo11.java 
public class Demo11 {
    public static void main(String[] args) {
    int[] arr = new int[3];
    arr[0] = 1;
    arr[1] = 3;
    arr[2] = 2;
    int max = arr[0];
    for (int i=0; i<arr.length-1; i++) {
        for (int j=i+1; j<arr.length-1-i; j++) {
            if (arr[j] > arr[j+1]) {
                 max = arr[j];
                 arr[j] = arr[j+1];
                 arr[j+1] = max;
                }
 
            }
        }
        System.out.println(arr[0] +"\n" +arr[1] +"\n" +arr[2]);
   }
 
}
 
//运行结果
[root@bch04 java]# java Demo11
1
2
3


 

三、数组(2维)

1.定义

    数组类型[][] 数组名 = new 数组类型[一维数组的个数][每一个一维数组中元素的个数];

 

2.初始化

2.1 静态初始化

    int [][] a = new int[][]{ {12,34,45,89},{34,56,78,10},{1,3,6,4} };


2.2 动态初始化


1.png 

 

例子

[root@bch04 java]# cat Demo12.java 
public class Demo12 {
    public static void getArr2(int [][] a) {
        for (int i = 0; i<a.length; i++) {
            for (int j =0 ; j<a[i].length; j++) {
                    System.out.print(a[i][j] +" ,");
                    }
            }
        System.out.println();
    }
 
    public static long getSum(int [][] a) {
        long sum = 0L;
        for (int i =0; i<a.length; i++) {
            for (int j =0; j<a[i].length; j++) {
                    sum += a[i][j];
                }
            }
        return sum;
    }
 
    public static int getCount(int [][] a) {
        int count = 0;
        for (int i =0; i<a.length; i++) {
            for (int j =0; j<a[i].length; j++) {
                count++;  
            }
        }
        return count ;
    }   
 
    public static void main(String[] args) {
        int [][] a = new int [][]{{1,2,3,4,5,6},{6,5,4,3,2,1},{8,9,0}};
        getArr2(a);
        System.out.println("二维数组和:" +getSum(a));
        System.out.println("二维数组元素个数:" +getCount(a));
 
    }
 
}
 
//运行结果
[root@bch04 java]# java Demo12
1 ,2 ,3 ,4 ,5 ,6 ,6 ,5 ,4 ,3 ,2 ,1 ,8 ,9 ,0 ,
二维数组和:59
二维数组元素个数:15





Guess you like

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