๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java] ๋ฐฐ์—ด ๋ณต์‚ฌํ•˜๊ธฐ

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 19.
๋ฐ˜์‘ํ˜•

โ–ถ ๋ฐฐ์—ด ๋ณต์‚ฌํ•˜๊ธฐ

๊ธฐ์กด ๋ฐฐ์—ด๊ณผ ์ž๋ฃŒํ˜• ๋ฐ ๋ฐฐ์—ด ํฌ๊ธฐ๊ฐ€ ๋˜‘๊ฐ™์€ ๋ฐฐ์—ด์„ ์ƒˆ๋กœ ๋งŒ๋“ค๊ฑฐ๋‚˜ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ์— ์ž๋ฃŒ๊ฐ€ ๊ฝ‰ ์ฐจ์„œ ๋” ํฐ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๊ธฐ์กด ๋ฐฐ์—ด์— ์ €์žฅ๋œ ์ž๋ฃŒ๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค ํ•  ๋•Œ ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•œ๋‹ค.

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 ๊ณผ ๋‹ค๋ฅธ ๋‚ด์šฉ์œผ๋กœ ์ถœ๋ ฅ๋จ
		}
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€