Tässä opetusohjelmassa opit eri tavoista, joilla voit kopioida Java-taulukoita (sekä yksi- että kaksiulotteisia) Java-esimerkkien avulla.
Java-tilassa voimme kopioida yhden taulukon toiseen. Taulukoiden kopioimiseen Java-järjestelmässä on useita tekniikoita.
1. Taulukoiden kopiointi Assignment Operatorilla
Otetaan esimerkki,
class Main ( public static void main(String() args) ( int () numbers = (1, 2, 3, 4, 5, 6); int () positiveNumbers = numbers; // copying arrays for (int number: positiveNumbers) ( System.out.print(number + ", "); ) ) )
Tuotos :
1, 2, 3, 4, 5, 6
Yllä olevassa esimerkissä olemme käyttäneet määritysoperaattoria ( =
) kopioimaan numeroita sisältävän taulukon toiseen ryhmään nimeltä positiivinen numero.
Tämä tekniikka on helpoin ja se toimii myös. Tässä tekniikassa on kuitenkin ongelma. Jos vaihdamme yhden taulukon elementtejä, myös muiden taulukoiden vastaavat elementit muuttuvat. Esimerkiksi,
class Main ( public static void main(String() args) ( int () numbers = (1, 2, 3, 4, 5, 6); int () positiveNumbers = numbers; // copying arrays // change value of first array numbers(0) = -1; // printing the second array for (int number: positiveNumbers) ( System.out.print(number + ", "); ) ) )
Tuotos :
-1, 2, 3, 4, 5, 6
Täällä voimme nähdä, että olemme muuttaneet yhtä numeroryhmän arvoa. Kun tulostamme positiivisen numerot-taulukon, voimme nähdä, että sama arvo muuttuu.
Se johtuu siitä, että molemmat taulukot viittaavat samaan taulukko-objektiin. Tämä johtuu matalasta kopiosta. Saat lisätietoja matalasta kopiosta käymällä matalassa kopiossa.
Uusien matriisiobjektien tekemiseen samalla kun kopioimme taulukoita, tarvitsemme syvän kopion eikä matalan kopion.
2. Looping Construct -sovelluksen käyttäminen kopioimalla taulukoita
Otetaan esimerkki:
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); // iterate and copy elements from source to destination for (int i = 0; i < source.length; ++i) ( destination(i) = source(i); ) // converting array to string System.out.println(Arrays.toString(destination)); ) )
Tuotos :
(1, 2, 3, 4, 5, 6)
Yllä olevassa esimerkissä olemme käyttäneet for
silmukkaa iteroimaan lähdematriisin jokaisen elementin läpi. Kussakin iteraatiossa kopioimme elementtejä lähdetaulukosta kohdetaulukkoon.
Lähde- ja kohdetaulukko viittaavat tässä eri kohteisiin (syväkopio). Siksi, jos yhden taulukon elementtejä muutetaan, toisen taulukon vastaavat elementit eivät muutu.
Huomaa lausunto,
System.out.println(Arrays.toString(destination));
Tässä toString () -menetelmää käytetään taulukon muuntamiseen merkkijonoksi. Lisätietoja on käymällä toString () -menetelmässä (virallinen Java-dokumentaatio).
3. Taulukoiden kopioiminen arraycopy () -menetelmällä
Java-järjestelmäluokka sisältää menetelmän, jonka nimi arraycopy()
on taulukoiden kopiointi. Tämä menetelmä on parempi tapa kopioida taulukoita kuin edellä mainitut kaksi.
arraycopy()
Menetelmä voit kopioida tietyn osan virtalähdemuodostelmalla määränpäähän jono. Esimerkiksi,
arraycopy(Object src, int srcPos,Object dest, int destPos, int length)
Tässä,
- src - lähdetaulukko, jonka haluat kopioida
- srcPos - lähtökohta (hakemisto) lähdematriisissa
- dest - kohde matriisi, johon elementit kopioidaan lähteestä
- destPos - lähtöpaikka (hakemisto) kohdetaulukossa
- pituus - kopioitavien elementtien lukumäärä
Otetaan esimerkki:
// To use Arrays.toString() method 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); // Creating n2 array of having length of n1 array int() n2 = new int(n1.length); // copying entire n1 array to n2 System.arraycopy(n1, 0, n2, 0, n1.length); System.out.println("n2 = " + Arrays.toString(n2)); // copying elements from index 2 on n1 array // copying element to index 1 of n3 array // 2 elements will be copied System.arraycopy(n1, 2, n3, 1, 2); System.out.println("n3 = " + Arrays.toString(n3)); ) )
Tuotos :
n2 = (2, 3, 12, 4, 12, -2) n3 = (0, 12, 4, 0, 0)
Yllä olevassa esimerkissä olemme käyttäneet arraycopy()
menetelmää,
System.arraycopy(n1, 0, n2, 0, n1.length)
- kokonaiset elementit n1-ryhmästä kopioidaan n2-ryhmäänSystem.arraycopy(n1, 2, n3, 1, 2)
- 2 n1-ryhmän elementtiä indeksistä 2 alkaen kopioidaan hakemistoon alkaen yhdestä n3-taulukosta
Kuten näette, int-tyyppisen taulukon elementtien oletusarvo on 0.
4. Taulukoiden kopiointi käyttäen copyOfRange () -menetelmää
We can also use the copyOfRange() method defined in Java Arrays class to copy arrays. For example,
// To use toString() and copyOfRange() method import java.util.Arrays; class ArraysCopy ( public static void main(String() args) ( int() source = (2, 3, 12, 4, 12, -2); // copying entire source array to destination int() destination1 = Arrays.copyOfRange(source, 0, source.length); System.out.println("destination1 = " + Arrays.toString(destination1)); // copying from index 2 to 5 (5 is not included) int() destination2 = Arrays.copyOfRange(source, 2, 5); System.out.println("destination2 = " + Arrays.toString(destination2)); ) )
Output
destination1 = (2, 3, 12, 4, 12, -2) destination2 = (12, 4, 12)
In the above example, notice the line,
int() destination1 = Arrays.copyOfRange(source, 0, source.length);
Here, we can see that we are creating the destination1 array and copying the source array to it at the same time. We are not creating the destination1 array before calling the copyOfRange()
method. To learn more about the method, visit Java copyOfRange.
5. Copying 2d Arrays Using Loop
Similar to the single-dimensional array, we can also copy the 2-dimensional array using the for
loop. For example,
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) ( // allocating space for each row of destination array destination(i) = new int(source(i).length); for (int j = 0; j < destination(i).length; ++j) ( destination(i)(j) = source(i)(j); ) ) // displaying destination array System.out.println(Arrays.deepToString(destination)); ) )
Output:
((1, 2, 3, 4), (5, 6), (0, 2, 42, -4, 5))
In the above program, notice the line,
System.out.println(Arrays.deepToString(destination);
Tässä deepToString()
menetelmää käytetään aikaansaamaan parempi kuva 2-ulotteisesta taulukosta. Lisätietoja on Java deepToString () -sivulla.
2d-taulukoiden kopioiminen array-kopion avulla ()
Yllä olevan koodin yksinkertaistamiseksi voimme korvata sisemmän silmukan System.arraycopy()
kuten yksiulotteisen taulukon tapauksessa. Esimerkiksi,
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) ( // allocating space for each row of destination array destination(i) = new int(source(i).length); System.arraycopy(source(i), 0, destination(i), 0, destination(i).length); ) // displaying destination array System.out.println(Arrays.deepToString(destination)); ) )
Tuotos :
((1, 2, 3, 4), (5, 6), (0, 2, 42, -4, 5))
Täällä voimme nähdä, että saamme saman tuotoksen korvaamalla sisempi for
silmukka arraycopy()
menetelmällä.