Java學習 (八)、靜態方法,Arrays類,二維數組,arrays二維數組
一、靜態方法
靜態方法屬於類的,可以直接使用類名.方法名()調用。
靜態方法的聲明
訪問修飾符 static 類型 方法名(參數列表)
{
//方法體
}
方法的作用:一個程序分解成幾個方法,有利於快速調試程序,也有利於提高程序代碼的利用率。因為方法是可以多次被調用的,調用次數和調用場合沒有限制。
方法分類:①返回值為(空)void的方法②帶具體返回類型的方法③不帶參數的方法④帶參數的方法
方法的返回值:如果方法中有返回值,方法中必須使用關鍵字return返回該值,返回值類型為該方法所定義的返回值類型。
①不帶返回值的方法

![]()
1 public class BubbleSort{
2 public static void main(String []argas)
3 {
4 int[] array={80,53,12,90,35,22,65,45,82,33};
5 bubble(array);
6 print(array);
7 }
8
9 //冒泡方法
10 public static void bubble(int[] array)
11 {
12 //N個數比較的輪數為N-1次
13 for(int i=0;i<array.length-1;i++)
14 {
15 //每一輪比較的次數為N-1-i次
16 for(int j=0;j<array.length-i-1;j++)
17 {
18 //比較相鄰的2個數,小靠前
19 if(array[j]>array[j+1])
20 {
21 //兩個數做交換,通過設置臨時變量
22 int temp=array[j];
23 array[j]=array[j+1];
24 array[j+1]=temp;
25 }
26 }
27 }
28
29 }
30
31 //打印輸出方法
32 public static void print(int[] array)
33 {
34 //把排好序的數組輸出
35 for(int i=0;i<array.length;i++)
36 {
37 System.out.print(array[i]+",");
38 }
39 }
40 }
View Code
②帶返回值的方法

![]()
1 public class BubbleSort{
2 public static void main(String []argas)
3 {
4 int[] array={80,53,12,90,35,22,65,45,82,33};
5 print(bubble(array));
6 }
7
8 //冒泡方法
9 public static int [] bubble(int[] array)
10 {
11 //N個數比較的輪數為N-1次
12 for(int i=0;i<array.length-1;i++)
13 {
14 //每一輪比較的次數為N-1-i次
15 for(int j=0;j<array.length-i-1;j++)
16 {
17 //比較相鄰的2個數,小靠前
18 if(array[j]>array[j+1])
19 {
20 //兩個數做交換,通過設置臨時變量
21 int temp=array[j];
22 array[j]=array[j+1];
23 array[j+1]=temp;
24 }
25 }
26 }
27 return array;
28 }
29
30 //打印輸出方法
31 public static void print(int[] array)
32 {
33 //把排好序的數組輸出
34 for(int i=0;i<array.length;i++)
35 {
36 System.out.print(array[i]+",");
37 }
38 }
39 }
View Code
③方法復用及不帶參數的方法

![]()
1 public class BubbleSort{
2 public static void main(String []argas)
3 {
4 int[] array={80,53,12,90,35,22,65,45,82,33};
5 show1();
6 print(array);
7 show2();
8 print(bubble(array));
9
10 int[] array1={80,25,12,30,35,22,55,45,82,33};
11 show1();
12 print(array1);
13 show2();
14 print(bubble(array1));
15 }
16
17 public static void show1()
18 {
19 System.out.print("排序前:");
20 }
21
22 public static void show2()
23 {
24 System.out.print("排序後:");
25 }
26
27 //冒泡方法
28 public static int [] bubble(int[] array)
29 {
30 //N個數比較的輪數為N-1次
31 for(int i=0;i<array.length-1;i++)
32 {
33 //每一輪比較的次數為N-1-i次
34 for(int j=0;j<array.length-i-1;j++)
35 {
36 //比較相鄰的2個數,小靠前
37 if(array[j]>array[j+1])
38 {
39 //兩個數做交換,通過設置臨時變量
40 int temp=array[j];
41 array[j]=array[j+1];
42 array[j+1]=temp;
43 }
44 }
45 }
46 return array;
47 }
48
49 //打印輸出方法
50 public static void print(int[] array)
51 {
52 //把排好序的數組輸出
53 for(int i=0;i<array.length;i++)
54 {
55 System.out.print(array[i]+",");
56 }
57 System.out.println();
58 }
59 }
View Code
二、Arrays類
Java的jdk中提供了一個Arrays工具類,此類專門為程序員操作數組提供了很多專有方法,通過方法的調用可以對數組進行賦值,排序,比較,查找元素等功能。
在jdk的api中搜索arrays可以看到該類的用法
就舉幾個例子

![]()
1 import java.util.Arrays;
2 public class ArraysUtilDemo{
3 public static void main(String []argas)
4 {
5 int[] arraySrc1={6,8,9,16,35,90};
6 //拷貝數組
7 int[] arrayDes1=Arrays.copyOf(arraySrc1,10);
8 for(int i=0;i<arrayDes1.length;i++)
9 {
10 System.out.print(arrayDes1[i]+" ");
11 }
12
13 System.out.println("\n**************************");
14 //拷貝指定數組中的指定范圍內的數據
15 int[] arrayDes2=Arrays.copyOfRange(arraySrc1,2,4);
16 for(int i=0;i<arrayDes2.length;i++)
17 {
18 System.out.print(arrayDes2[i]+" ");
19 }
20
21 System.out.println("\n**************************");
22 int[] arraySrc2={8,6,10,16,35,90};
23 boolean flag=Arrays.equals(arraySrc1,arraySrc2);
24 System.out.print(flag);
25
26 System.out.println("\n**************************");
27 //數組填充
28 int[] arrayDes3=new int[10];
29 Arrays.fill(arrayDes3,10);
30 for(int i=0;i<arrayDes3.length;i++)
31 {
32 System.out.print(arrayDes3[i]+" ");
33 }
34
35 System.out.println("\n**************************");
36 //對數組進行排序
37 Arrays.sort(arraySrc1);
38 for(int i=0;i<arraySrc1.length;i++)
39 {
40 System.out.print(arraySrc1[i]+" ");
41 }
42
43 System.out.println("\n**************************");
44 //二分法查找
45 int x=Arrays.binarySearch(arraySrc1,9);
46 System.out.print(x);
47
48 System.out.println("\n**************************");
49 //使用System類的方法來拷貝數組
50 int[] arrayDes4=new int[10];
51 System.arraycopy(arraySrc1,0,arrayDes4,2,5);
52 for(int i=0;i<arrayDes4.length;i++)
53 {
54 System.out.print(arrayDes4[i]+" ");
55 }
56 }
57 }
View Code
三、二維數組
① 可以看成以數組為元素的數組
② Java中二維數組的聲明和初始化應按照從高維到低維的順序排列
示例
int[][] arr1=new int[10][];//第二維長度未定
int[][] arr2=new int[10][20];//第二維長度確定
☆雖然這兩個數組的創建有區別,但系統為它們分配的堆內存空間大小是一樣的。
對於任何類型的二維數組而言,第一維的大小決定了二維數組對象的大小,因為二維數組的成員是數組引用,數組引用本身大小是固定的。
初始化二維數組
① 靜態初始化:int[][] arr={{1,2},{3,4,5},{6,7,8,9}};
② 動態初始化:
String[][] arrStr; //聲明
arrStr=new String[3][]; //創建,分配內存
arrStr[0]=new String[2]; //為高維初始化
arrStr[1]=new String[3];
arrStr[2]=new String[4];
arrStr[0][0]=new String(“abc00”); //為低維初始化
arrStr[0][1]=new String(“abc01”);
arrStr[1][0]=new String(“abc10”);
arrStr[1][1]=new String(“abc11”);
arrStr[1][2]=new String(“abc12”);
arrStr[2][0]=new String(“abc20”);
arrStr[2][1]=new String(“abc21”);
arrStr[2][2]=new String(“abc22”);
arrStr[2][3]=new String(“abc23”);

![]()
1 public class ArrayDemo2{
2 public static void main(String []args){
3 int[][] arr=new int[3][];
4 //每個高維的數組指向一個低維的int數組
5 arr[0]=new int[2];
6 arr[1]=new int[3];
7 arr[2]=new int[4];
8
9 //給低維數組進行賦值
10 arr[0][0]=1;
11 arr[0][1]=2;
12 arr[1][0]=3;
13 arr[1][1]=4;
14 arr[1][2]=5;
15 arr[2][0]=6;
16 arr[2][1]=7;
17 arr[2][2]=8;
18 arr[2][3]=9;
19
20 for(int i=0;i<arr.length;i++)
21 {
22 for(int j=0;j<arr[i].length;j++)
23 {
24 System.out.print(arr[i][j]);
25 }
26 System.out.println();
27 }
28 }
29 }
View Code