โถ ๋ฐฐ์ด ๋ณต์ฌํ๊ธฐ
๊ธฐ์กด ๋ฐฐ์ด๊ณผ ์๋ฃํ ๋ฐ ๋ฐฐ์ด ํฌ๊ธฐ๊ฐ ๋๊ฐ์ ๋ฐฐ์ด์ ์๋ก ๋ง๋ค๊ฑฐ๋ ๋ฐฐ์ด์ ๋ชจ๋ ์์์ ์๋ฃ๊ฐ ๊ฝ ์ฐจ์ ๋ ํฐ ๋ฐฐ์ด์ ๋ง๋ค์ด ๊ธฐ์กด ๋ฐฐ์ด์ ์ ์ฅ๋ ์๋ฃ๋ฅผ ๊ฐ์ ธ์ค๋ ค ํ ๋ ๋ฐฐ์ด์ ๋ณต์ฌํ๋ค.
System.arraycopy( ) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
package array;
public class ArrayCopy {
public static void main(String[] args) {
int[] array1 = { 10, 20, 30, 40, 50 };
int[] array2 = { 1, 2, 3, 4, 5 };
// ๋ณต์ฌํ ๋ฐฐ์ด, ๋ณต์ฌํ ์ฒซ ์์น, ๋์ ๋ฐฐ์ด, ๋ถ์ฌ ๋ฃ์ ์ฒซ ์์น, ๋ณต์ฌํ ์์ ๊ฐ์
System.arraycopy(array1, 0, array2, 1, 4);
for (int i = 0; i < array2.length; i++) {
System.out.println(array2[i]);
}
}
}
<๊ฒฐ๊ณผ๊ฐ>
โ
โโ
โ
โถ ๊ฐ์ฒด ๋ฐฐ์ด ๋ณต์ฌํ๊ธฐ
package array;
public class ObjectCopy1 {
public static void main(String[] args) {
Book[] bookArray1 = new Book[3];
Book[] bookArray2 = new Book[3];
bookArray1[0] = new Book("ํ๋ฐฑ์ฐ๋งฅ", "์กฐ์ ๋");
bookArray1[1] = new Book("๋ฐ๋ฏธ์", "ํค๋ฅด๋ง ํค์ธ");
bookArray1[2] = new Book("์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ", "์ ์๋ฏผ");
System.arraycopy(bookArray1, 0, bookArray2, 0, 3);
for (int i = 0; i < bookArray2.length; i++) {
bookArray2[i].showBookInfo();
}
}
}
<๊ฒฐ๊ณผ๊ฐ>
-bookArray2 ๋ฐฐ์ด์ ์ธ์คํด์ค๋ฅผ ๋ฐ๋ก ๋ง๋ค์ง ์์๋๋ฐ ๊ฐ ์์ ๊ฐ์ด ์ ์ถ๋ ฅ๋๊ณ ์๋ค.
๊ฐ์ฒด ๋ฐฐ์ด์ ์ฌ์ฉํ๋ ค๋ฉด ๊ผญ ์ธ์คํด์ค๋ฅผ ์์ฑํด์ ๋ฃ์ด์ผ ํ๋ค๊ณ ํ๋๋ฐ, ์ด ๊ฒฝ์ฐ๋ ์ด๋ป๊ฒ ๋ ๊ฒ์ผ๊น?
(๋ฐ์ ์ด์ด์..)
โ
โถ ์์ ๋ณต์ฌ
package array;
public class ObjectCopy2 {
public static void main(String[] args) {
Book[] bookArray1 = new Book[3];
Book[] bookArray2 = new Book[3];
bookArray1[0] = new Book("ํ๋ฐฑ์ฐ๋งฅ", "์กฐ์ ๋");
bookArray1[1] = new Book("๋ฐ๋ฏธ์", "ํค๋ฅด๋ง ํค์ธ");
bookArray1[2] = new Book("์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ", "์ ์๋ฏผ");
System.arraycopy(bookArray1, 0, bookArray2, 0, 3);
for (int i = 0; i < bookArray2.length; i++) {
bookArray2[i].showBookInfo();
}
bookArray1[0].setBookName("๋๋ชฉ"); // bookArray1 ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์ ๊ฐ ๋ณ๊ฒฝ
bookArray1[0].setAuthor("๋ฐ์์"); // bookArray1 ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์ ๊ฐ ๋ณ๊ฒฝ
System.out.println("=== bookArray1 ===");
for (int i = 0; i < bookArray1.length; i++) {
bookArray1[i].showBookInfo();
}
System.out.println("=== bookArray2 ===");
for (int i = 0; i < bookArray2.length; i++) {
bookArray2[i].showBookInfo(); // bookArray2 ๋ฐฐ์ด ์์ ๊ฐ๋ ๋ณ๊ฒฝ๋์ด ์ถ๋ ฅ
}
}
}
<๊ฒฐ๊ณผ๊ฐ>
-๊ฐ์ฒด ๋ฐฐ์ด์ ๋ณต์ฌํ ๋ ์ธ์คํด์ค๋ฅผ ๋ฐ๋ก ์์ฑํ๋ ๊ฒ ์๋๋ผ ๊ธฐ์กด ์ธ์คํด์ค์ ์ฃผ์ ๊ฐ๋ง ๋ณต์ฌํ๋ค.
๊ฒฐ๊ตญ ๋ ๋ฐฐ์ด์ ์๋ก ๋ค๋ฅธ ์์๊ฐ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์์ผ๋ฏ๋ก ๋ณต์ฌ๋๋ ๋ฐฐ์ด์ ์ธ์คํดํธ ๊ฐ์ด ๋ณ๊ฒฝ๋๋ฉด ๋ ๋ฐฐ์ด ๋ชจ๋ ์ํฅ์ ๋ฐ๋๋ค.
โถ ๊น์ ๋ณต์ฌ
package array;
public class ObjectCopy3 {
public static void main(String[] args) {
Book[] bookArray1 = new Book[3];
Book[] bookArray2 = new Book[3];
bookArray1[0] = new Book("ํ๋ฐฑ์ฐ๋งฅ", "์กฐ์ ๋");
bookArray1[1] = new Book("๋ฐ๋ฏธ์", "ํค๋ฅด๋ง ํค์ธ");
bookArray1[2] = new Book("์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ", "์ ์๋ฏผ");
bookArray2[0] = new Book(); // ๋ํดํธ ์์ฑ์๋ก bookArray2 ๋ฐฐ์ด ์ธ์คํด์ค ์์ฑ
bookArray2[1] = new Book();
bookArray2[2] = new Book();
for (int i = 0; i < bookArray1.length; i++) { // bookArray1 ๋ฐฐ์ด ์์๋ฅผ ์๋ก ์์ฑํ bookArray2 ๋ฐฐ์ด ์ธ์คํด์ค์ ๋ณต์ฌ
bookArray2[i].setBookName(bookArray1[i].getBookName());
bookArray2[i].setAuthor(bookArray1[i].getAuthor());
}
for (int i = 0; i < bookArray2.length; i++) {
bookArray2[i].showBookInfo(); // bookArray2 ๋ฐฐ์ด ์์ ๊ฐ ์ถ๋ ฅ
}
bookArray1[0].setBookName("๋๋ชฉ"); // bookArray1 ์ฒซ๋ฒ์งธ ๋ฐฐ์ด ์์ ๊ฐ ์์
bookArray1[0].setAuthor("๋ฐ์์"); // bookArray1 ์ฒซ๋ฒ์งธ ๋ฐฐ์ด ์์ ๊ฐ ์์
System.out.println("=== bookArray1 ===");
for (int i = 0; i < bookArray1.length; i++) { // bookArray1 ๋ฐฐ์ด ์์ ๊ฐ ์ถ๋ ฅ
bookArray1[i].showBookInfo();
}
System.out.println("=== bookArray2 ==="); // bookArray2 ๋ฐฐ์ด ์์ ๊ฐ๊ณผ ๋ค๋ฅธ ๋ด์ฉ์ด ์ถ๋ ฅ๋จ
for (int i = 0; i < bookArray2.length; i++) {
bookArray2[i].showBookInfo(); // bookArray1 ๊ณผ ๋ค๋ฅธ ๋ด์ฉ์ผ๋ก ์ถ๋ ฅ๋จ
}
}
}
<์คํ ๊ฒฐ๊ณผ>
โ
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ์ด์ฐจ์ ๋ฐฐ์ด (0) | 2021.02.19 |
---|---|
[Java] ํฅ์๋ for๋ฌธ๊ณผ ๋ฐฐ์ด (0) | 2021.02.19 |
[Java] ๋ฐฐ์ด (0) | 2021.02.19 |
[Java] ์ฐ์ต๋ฌธ์ (0) | 2021.02.19 |
[Java] static ์์ฉ - ์ฑ๊ธํค ํจํด (0) | 2021.02.19 |
๋๊ธ