1. 程式人生 > >Java基礎之數組-->>數組常用操作

Java基礎之數組-->>數組常用操作

style val order class print sea 聲明 cell esc

3.2一維數組

3.2.1聲明數組

數組類型[]  數組名稱;
int []   username;

或者

數組類型   數組名稱[];
int   username[];

3.2.2初始化一維數組

一維數組初始化有兩種格式,一種是先聲明再賦值,一種是直接聲明並賦值

int []  array = new int[5];  //創建一個整型數組對象,長度為5;

int []  array = {1,2,3,4,5}; //創建一個整型數組對象,長度為5,並同時賦值;

用new 運算符來創建數組對象時,必須指定數組大小。創建數組對象時,僅僅是在內存中為數組變量分配指定大小的空間,並沒有實際存儲數據,這時數據會被自動賦初始值

//數字數組,初值是 0;
//布爾數組,初值是false;
//字符數組,初值是‘\0‘;
//對象數組,初值是null;

3.2.3訪問一維數組

創建數組變量並賦值後,就可以訪問數組中元素

class oneday 
{
    public static void main(String[] args) 
    {
        int [] weeks = {1,2,3,4,5,6,7};
        for(int i = 0;i<weeks.length;i++){
            System.out.print(weeks[i]+" ");
        }
    }
}

//結果 1 2 3 4 5 6 7

3.2.4修改一維數組元素

數組中元素值是可以改變的,在聲明一個變量和創建一個數組對象後,可通過為數組中元素賦值方式,來修改數組中任一元素的值

class oneday 
{
    public static void main(String[] args) 
    {
        String[] students = {"張三","李四"};   //創建兩個學生;

        int languageScore[] = new int[2];        //定義一個整型數組,保存兩個同學的語文成績

        languageScore[
0] =97; //為第一個成績賦值 languageScore[1] =100; //為第二個成績賦值 System.out.println("張三:"+languageScore[0]); System.out.println("李四:"+languageScore[1]); } } //結果:張三:97 李四:100

3.3數組常用操作

3.3.1數組長度

要獲得數組長度,可以用數組本身length屬性獲得。

class oneday 
{
    public static void main(String[] args) 
    {
        int[] weeks = {1,2,3,4,5,6,7};
        int len = weeks.length;       //獲取數組長度
        System.out.println("數組長度為:"+len);
        }
    }
//數組長度為:7

3.3.2數組填充

數組填充指的是將一個數組或數組指定元素用固定值添加到數組中,可以使用 Arrays 類提供的 fill 對數組填充

Arrays.fill(數組名,值);        //將值全部填充到數組
Arrays.fill(數組名,開始下標,結束下表,值);    //將值填充到開始下標到結束下標的部分
import java.util.Arrays;               //導入Arrays 類
class oneday 
{
    public static void main(String[] args) 
    {
        int [] a = new int[5];    //定義一個整型數組a
        int [] b = new int[5];    //定義一個整型數組b
        
        Arrays.fill(a,1);           //給數組a填充值 1
        Arrays.fill(b,2,4,0);     //給數組b下標2~4之間填充0
        //循環輸出數組元素
        for(int i = 0;i<a.length;i++){
            System.out.print(a[i]+" ");
        }System.out.println();
        for(int i =0;i<b.length;i++){
            System.out.print(b[i]+" ");
        }System.out.println();
    }
    }
// 1 1 1 1 1
// 0 0 0 0 0

3.3.3數組復制

數組復制是將一個指定數組範圍內的元素值復制到另一個數組中去,利用 Java 提供的 Arraycopy() 函數(方法)來進行數組的復制操作。

// Arraycopy (數組a,開始復制下標,復制到數組b,開始復制下標,復制長度);

eg:將數組a的元素從第二個開始復制到數組b的第二個元素開始中,復制5個長度

    import java.util.Arrays;               //導入Arrays 類
    class oneday 
    {
        public static void main(String[] args) 
        {
            int [] a = {1,2,3,4,5,6,7};
            int [] b ={11,12,13,14,15,16,17};

            System.arraycopy(b,1,a,1,5);
            System.out.println("復制後的a數組值為:");
            for(int i=0;i<a.length;i++){
                System.out.print(a[i]+" ");
            }
        }
    }
// 復制後的a數組值為:
//     1 12 13 14 15 16 7

3.3.4數組比較

數組之間可以比較,如果兩個數組長度一樣,並且相同位置的元素也一樣,那麽這兩個數組相等;否則,不相等。可以使用 Arrays 提供的 equals 來判斷兩個數組是否相等。

// Arrays.equals(數組1,數組2);
    import java.util.Arrays;               //導入Arrays 類
    class oneday 
    {
        public static void main(String[] args) 
        {
            int [] a = new int[5];
            int [] b = new int[5];

            Arrays.fill(a,1);    //給數組a填充1
            Arrays.fill(b,2,4,20);   //用20來填充數組b的下標2~4的部分

            if(Arrays.equals(a,b)){
                System.out.print("兩個數組相等");
            }else{
                System.out.print("兩個數組不等");
            }System.out.println();
        }
    }
// 兩個數組不等
// Arrays.equals 返回值為 boolean

3.3.5數組排序

數組排序主要包括 sort 函數(方法)排序和冒泡排序

Arrays.sort(數組);
//或者
Arrays.sort(數組,開始下標,結束下標);

①sort 函數(方法)是升序排序,可以將數組全部排序,也可以在指定範圍內將元素排序。

    import java.util.Arrays;               //導入Arrays 類
    class oneday 
    {
        public static void main(String[] args) 
        {
            int [] a = {12,62,53,74,8};
            int [] b = {45,68,2,56,7};
            //將數組a全部排序
            Arrays.sort(a);
            //將數組b第2個和第4個之間排序
            Arrays.sort(b,2,4);
            System.out.println("數組a排序後為:");
            //循環輸出a元素
            for(int i = 0;i<a.length;i++){
                System.out.print(a[i]+" ");
            }
            System.out.println();
            System.out.println("數組b排序後為:");
            for(int i = 0;i<b.length;i++){
                System.out.print(b[i]+" ");
            }
        }
    }
/*
數組a排序後為:
8 12 53 62 74
數組b排序後為:
45 68 2 56 7 
*/

②冒泡排序法 ,又稱為交換排序法。

    class oneday 
    {
        public static void main(String[] args) 
        {
            int [] a = {12,62,53,74,8};
            int temp;                         //定義一個中間量進行交換
            for(int i = 0;i<a.length;i++){
                for(int j = i;j<a.length;j++){
                    if(a[i]>a[j]){
                        temp = a[i];
                        a[i]=a[j];
                        a[j]=temp;
                    }
                }
            }
            System.out.println("數組a排序後:");
            for(int i=0;i<a.length;i++){
                System.out.print(a[i]+" ");
            }
        }
    }

/*
數組a排序後:
8 12 53 62 74
*/

3.3.6在數組中搜索指定元素

使用 Arrays 提供的 binarySearch 函數來搜索指定元素

binarySearch(數組,指定元素);
//或者
binarySearch(數組,開始位置,結束位置,指定元素);

binarySearch 方法返回值是 int 類型,指所在的下標

    class oneday 
    {
        public static void main(String[] args) 
        {
            int [] a = {12,62,53,74,8};
            int num1 = Arrays.binarySearch(a,53);
            System.out.println("53的下標為:"+num1);
        }
    }
//53的下標為:2

3.3.7把數組轉換成字符串

任何數組類型都可以使用 Arrays 類的 toString 函數(方法)轉換為字符串。

// toString(數組類型,數組名);

返回值為字符串類型

    class oneday 
    {
        public static void main(String[] args) 
        {
            int [] a = {12,62,53,74,8};
            double [] b = {3.68,56.44,99.51,12.3};
            //輸出數組轉換後的字符串
            System.out.println("int類型數組a轉換成字符串為:"+Arrays.toString(a));
            System.out.println("double類型數組b轉換成字符串為:"+Arrays.toString(b));
        }
    }
/*
int類型數組a轉換成字符串為:[12, 62, 53, 74, 8]
double類型數組b轉換成字符串為:[3.68, 56.44, 99.51, 12.3]
*/

3.4多維數組

3.4.1聲明二維數組

數組類型 [] [] 數組名字;
int [] [] num;

//或者

數組類型 數組名字 [] [];
int num [] [];

3.4.2創建二維數組

創建二維數組對象有兩種格式:

int [] [] num = new int[3][4];   //創建一個int類型的二維數組,長度為3和4;
String[][] username = new String[2][2] //創建一個string類型數組,長度為2和2 

//或者
//創建並賦值
int [][] num = {{3,4,5},{6,7,8,9}}; 
String [][] username = {{"張三","李四"},{"趙二","老王"}};

3.4.3訪問二維數組

創建數組變量並賦值後就可以訪問二維數組,第一個下標為行索引,第二個下標為列索引

//int[] [] num1={{10,20,30,40},{50,60,70,80}};

列下標0

列下標1

列下標2

列下標3

行下標0

10

20

30

40

行下標1

50

60

70

80

    class oneday 
    {
        public static void main(String[] args) 
        {
            int[] [] num1={{10,20,30,40},{50,60,70,80}};
            System.out.println(num1[0][2]);
        }
    }
//30

上述輸出數組num1的第一行(下標為0)第三列(下標為2)的元素,輸出值為:30

3.4遍歷二維數組

    import java.util.Arrays;               //導入Arrays 類
    class oneday 
    {
        public static void main(String[] args) 
        {
            int [] [] a={{1,2,3},{4,5,6},{7,8,9}};       //定義一個整型二維數組a
            int [] [] b=new int[3][3];                        //定義一個整型二維數組b                
            int k = 1;
            int i,j =0;
            for(i=0;i<b.length;i++){
                for(j=0;j<b[i].length;j++){
                    b[i][j]=k++;
                }
                //輸出a數組;
                System.out.println("輸出a數組");
                for(i=0;i<a.length;i++){
                    for(j=0;j<a[i].length;j++){
                        System.out.print(a[i][i]+" ");
                    }
                    System.out.println();
                }
                //輸出b數組
                System.out.println("輸出b數組");
                for(i=0;i<b.length;i++){
                    for(j=0;j<b[i].length;j++){
                        System.out.print(b[i][j]+" ");
                    }
                    System.out.println();
                }
            }
        }
    }

/*
輸出a數組
1 1 1
5 5 5
9 9 9
輸出b數組
1 2 3
0 0 0
0 0 0
*/

非常感謝<從零開始學Java>一書對我的幫助,本文借鑒於此,僅用來學習,回顧,鞏固。再次感謝。

Java基礎之數組-->>數組常用操作