728x90
320x100
package chap02;// 연습2-4
// 배열의 모든 요소를 copy 합니다
import java.util.Scanner;
class Practice04 {
//--- 배열 b의 모든 요소를 배열 a에 copy ---//
static void copy(int[] a, int[] b) {
int num = a.length <= b.length ? a.length : b.length;
for (int i = 0; i < num; i++)
a[i] = b[i];
}
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("a의 요솟수는 : ");
int numa = stdIn.nextInt(); // 요솟수
int[] a = new int[numa]; // 요솟수가 numa인 배열
for (int i = 0; i < numa; i++) {
System.out.print("a[" + i + "] : ");
a[i] = stdIn.nextInt();
}
System.out.print("b의 요솟수는 : ");
int numb = stdIn.nextInt(); // 요솟수
int[] b = new int[numb]; // 요솟수가 numb인 배열
for (int i = 0; i < numb; i++) {
System.out.print("b[" + i + "] : ");
b[i] = stdIn.nextInt();
}
copy(a, b); // 배열 b의 모든 요소를 배열 a에 copy
System.out.println("배열 b의 모든 요소를 배열 a에 copy 하였습니다.");
for (int i = 0; i < numa; i++)
System.out.println("a[" + i + "] = " + a[i]);
}
}
728x90
320x100
'💻 하나씩 차곡차곡 > 자료구조 & 알고리즘(JAVA)' 카테고리의 다른 글
입력 받은 10진수를 2진수~36진수로 기수 변환하여 출력하기 (chap02/CardConv) (0) | 2022.10.21 |
---|---|
배열요소를 역순으로 복사하기 (chap02/Practice05) (0) | 2022.10.20 |
배열 요소의 합계 구하기 (chap02/Practice03) (0) | 2022.10.18 |
배열요소를 역순으로 정리하기 (chap02/ReverseArray) (0) | 2022.10.17 |
배열요소의 최댓값 구하기 (chap02/MaxOfArray) (0) | 2022.10.16 |