Java 菜鳥教程

Java 流程控制

Java 數(shù)組

Java 面向?qū)ο?I)

Java 面向?qū)ο?II)

Java 面向?qū)ο?III)

Java 異常處理

Java 列表(List)

Java Queue(隊(duì)列)

Java Map集合

Java Set集合

Java 輸入輸出(I/O)

Java Reader/Writer

Java 其他主題

Java 數(shù)組復(fù)制

在本教程中,您將在示例的幫助下了解可用于在Java中復(fù)制數(shù)組(一維和二維)的不同方法。

 在Java中,我們可以將一個(gè)數(shù)組復(fù)制到另一個(gè)數(shù)組中。有幾種技術(shù)可以用來在Java中復(fù)制數(shù)組。

1.使用賦值運(yùn)算符復(fù)制數(shù)組

讓我們舉個(gè)實(shí)例

class Main {
    public static void main(String[] args) {
       
        int [] numbers = {1, 2, 3, 4, 5, 6};
        int [] positiveNumbers = numbers;    //復(fù)制數(shù)組

        for (int number: positiveNumbers) {
            System.out.print(number + ", ");
        }
    }
}

輸出

1, 2, 3, 4, 5, 6

 在上面的示例中,我們使用賦值運(yùn)算符(=)將一個(gè)名為numbers的數(shù)組復(fù)制到另一個(gè)名為positiveEnumbers的數(shù)組中。

 這種技術(shù)是最簡單的一種,它同樣有效。然而,這種技術(shù)有一個(gè)問題。如果我們改變一個(gè)數(shù)組的元素,其他數(shù)組的相應(yīng)元素也會(huì)改變。例如,

class Main {
    public static void main(String[] args) {
      
        int [] numbers = {1, 2, 3, 4, 5, 6};
        int [] positiveNumbers = numbers;    //復(fù)制數(shù)組
      
        //更改第一個(gè)數(shù)組的值
        numbers[0] = -1;

        //打印第二個(gè)數(shù)組
        for (int number: positiveNumbers) {
            System.out.print(number + ", ");
        }
    }
}

輸出

-1, 2, 3, 4, 5, 6

 在這里,我們可以看到我們已經(jīng)改變了numbers數(shù)組的一個(gè)值。當(dāng)我們打印positiveEnumbers數(shù)組時(shí),可以看到相同的值也發(fā)生了更改。

這是因?yàn)閮蓚€(gè)數(shù)組都引用相同的數(shù)組對象。這是因?yàn)闇\拷貝。要了解有關(guān)淺拷貝的更多信息,請?jiān)L問淺拷貝

現(xiàn)在,要在復(fù)制數(shù)組的同時(shí)生成新的數(shù)組對象,我們需要深度復(fù)制而不是淺拷貝。

2.使用循環(huán)構(gòu)造復(fù)制數(shù)組

讓我們舉個(gè)實(shí)例:

import java.util.Arrays;

class Main {
    public static void main(String[] args) {
      
        int [] source = {1, 2, 3, 4, 5, 6};
        int [] destination = new int[6];

        //迭代并將元素從源復(fù)制到目標(biāo)
        for (int i = 0; i < source.length; ++i) {
            destination[i] = source[i];
        }
      
         //將數(shù)組轉(zhuǎn)換為字符串
        System.out.println(Arrays.toString(destination));
    }
}

輸出

[1, 2, 3, 4, 5, 6]

在上面的示例中,我們使用了for循環(huán)來遍歷源數(shù)組的每個(gè)元素。在每次迭代中,我們都將元素從source數(shù)組復(fù)制到destination數(shù)組。

在這里,源和目標(biāo)數(shù)組引用不同的對象(深度復(fù)制)。因此,如果一個(gè)數(shù)組的元素被更改,另一個(gè)數(shù)組的相應(yīng)元素也將保持不變。

注意以下語句,

System.out.println(Arrays.toString(destination));

在這里,toString()方法用于將數(shù)組轉(zhuǎn)換為字符串。

3.使用arraycopy()方法復(fù)制數(shù)組

在Java中,System類包包含一個(gè)名為arraycopy()的方法來復(fù)制數(shù)組。與上述兩種方法相比,這種方法是一種更好的復(fù)制數(shù)組的方法。

 方法允許您將源數(shù)組的指定部分復(fù)制到目標(biāo)數(shù)組。例如,

arraycopy(Object src, int srcPos,Object dest, int destPos, int length)

這里,

  • src -您要復(fù)制的源數(shù)組

  • srcPos -源數(shù)組中的起始位置(索引)

  • dest -目標(biāo)數(shù)組,將從源中復(fù)制元素

  • destPos -目標(biāo)數(shù)組中的起始位置(索引)

  • length -要復(fù)制的元素?cái)?shù)

讓我們舉個(gè)實(shí)例:

//使用Arrays.toString()方法
import java.util.Arrays;

class Main {
    public static void main(String[] args) {
        int[] n1 = {2, 3, 12, 4, 12, -2};
      
        int[] n3 = new int[5];

        //創(chuàng)建長度為n1的n2數(shù)組
        int[] n2 = new int[n1.length];
      
        //將整個(gè)n1數(shù)組復(fù)制到n2
        System.arraycopy(n1, 0, n2, 0, n1.length);
        System.out.println("n2 = " + Arrays.toString(n2));  
      
         //從n1數(shù)組的索引2復(fù)制元素
         //將元素復(fù)制到n3數(shù)組的索引1
        //將復(fù)制2個(gè)元素
        System.arraycopy(n1, 2, n3, 1, 2);
        System.out.println("n3 = " + Arrays.toString(n3));  
    }
}

輸出

n2 = [2, 3, 12, 4, 12, -2]
n3 = [0, 12, 4, 0, 0]

在上面的示例中,我們使用了arraycopy()方法,

  • System.arraycopy(n1, 0, n2, 0, n1.length) - 將n1數(shù)組中的整個(gè)元素復(fù)制到n2數(shù)組中

  • System.arraycopy(n1, 2, n3, 1, 2 )-  從索引2開始的n1數(shù)組的兩個(gè)元素被復(fù)制到從n3數(shù)組1開始的索引中

正如您看到的,int類型數(shù)組元素的默認(rèn)初始值為0。

4.使用copyOfRange()方法復(fù)制數(shù)組

我們還可以使用Java Arrays類中定義的copyOfRange()方法來復(fù)制數(shù)組。例如,

//使用toString()和copyOfRange()方法
import java.util.Arrays;

class ArraysCopy {
    public static void main(String[] args) {
      
        int[] source = {2, 3, 12, 4, 12, -2};
      
        //將整個(gè)源數(shù)組復(fù)制到目標(biāo)
        int[] destination1 = Arrays.copyOfRange(source, 0, source.length);      
        System.out.println("destination1 = " + Arrays.toString(destination1)); 
      
        //從索引2復(fù)制到5(不包括5) 
        int[] destination2 = Arrays.copyOfRange(source, 2, 5); 
        System.out.println("destination2 = " + Arrays.toString(destination2));   
    }
}

輸出結(jié)果

destination1 = [2, 3, 12, 4, 12, -2]
destination2 = [12, 4, 12]

在上面的示例中,請注意以下行:

int[] destination1 = Arrays.copyOfRange(source, 0, source.length);

在這里,我們可以看到我們正在創(chuàng)建destination1數(shù)組并同時(shí)將源數(shù)組復(fù)制到它。在調(diào)用copyOfRange()方法之前,我們不會(huì)創(chuàng)建destination1數(shù)組。要了解有關(guān)該方法的更多信息,請?jiān)L問Java copyOfRange。

5.使用循環(huán)復(fù)制二維數(shù)組

類似于一維數(shù)組,我們還可以使用for循環(huán)來復(fù)制二維數(shù)組。例如,

import java.util.Arrays;

class Main {
    public static void main(String[] args) {
      
        int[][] source = {
              {1, 2, 3, 4}, 
              {5, 6},
              {0, 2, 42, -4, 5}
              };

        int[][] destination = new int[source.length][];

        for (int i = 0; i<destination.length; ++i) {

            //為目標(biāo)數(shù)組的每一行分配空間
            destination[i] = new int[source[i].length];

            for (int j = 0; j<destination[i].length; ++j) {
                destination[i][j] = source[i][j];
            }
        }
     
        //顯示目標(biāo)數(shù)組
        System.out.println(Arrays.deepToString(destination));  
      
    }
}

輸出

[[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]]

在上面的程序中,請注意以下行:

System.out.println(Arrays.deepToString(destination);

在這里,使用deepToString()方法提供二維數(shù)組的更好表示。要了解更多信息,請?jiān)L問Java deepToString()。

使用arraycopy()復(fù)制二維數(shù)組

為了使上面的代碼更簡單,我們可以使用System.arraycopy()替換內(nèi)部循環(huán),就像處理一維數(shù)組一樣。例如,例如,

import java.util.Arrays;

class Main {
    public static void main(String[] args) {
      
        int[][] source = {
              {1, 2, 3, 4}, 
              {5, 6},
              {0, 2, 42, -4, 5}
              };

        int[][] destination = new int[source.length][];

        for (int i = 0; i<source.length; ++i) {

             //為目標(biāo)數(shù)組的每一行分配空間
             destination[i] = new int[source[i].length];
             System.arraycopy(source[i], 0, destination[i], 0, destination[i].length);
        }
     
        //顯示目標(biāo)數(shù)組
        System.out.println(Arrays.deepToString(destination));      
    }
}

輸出

[[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]]

 在這里,我們可以看到,通過用arraycopy()方法替換內(nèi)部for循環(huán),可以得到相同的輸出。

丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清