Deep copy로 복제.
// 배열 이름.clone()
public class CloneArray {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5 };
int[] b = a.clone();
b[3] = 0;
System.out.print("a =");
for (int i = 0; i < a.length; i++) {
System.out.print(" " + a[i]); // a = 1 2 3 4 5
}
System.out.print("\nb =");
for (int i = 0; i < b.length; i++) {
System.out.print(" " + b[i]); // b = 1 2 3 0 5
}
}
}
package chap02;
import java.util.Scanner;
public class MaxOfArray {
static int maxOf(int[] a) { // a는 height 배열을 참조 => call by reference
int max = a[0];
for (int i = 0; i < a.length; i++) {
if (a[i] > max)
max = a[i];
}
return max;
}
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.println("키의 최댓값을 구합니다.");
System.out.print("사람 수 : ");
int num = stdIn.nextInt();
int[] height = new int[num];
for (int i = 0; i < height.length; i++) {
System.out.print("height[" + i + "] : ");
height[i] = stdIn.nextInt();
}
System.out.println("최댓값은 " + maxOf(height) + "입니다. ");
}
}
maxOf() 메소드의 parameter int[] a는 배열 height를 참조하여 복사해오고 있다. (call by reference)
public class ReverseArray {
static void swap(int[] a, int idx1, int idx2) {
int t = a[idx1];
a[idx1] = a[idx2];
a[idx2] = t;
}
static void reverse(int[] a) {
for (int i = 0; i < a.length / 2; i++) {
swap(a, i, a.length - i - 1);
}
}
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("요소수 : ");
int num = stdIn.nextInt();
int[] x = new int[num];
for (int i = 0; i < x.length; i++) {
System.out.print("x[" + i + "] : ");
x[i] = stdIn.nextInt();
}
reverse(x);
System.out.println("요소를 역순으로 정렬했습니다.");
for (int i = 0; i < x.length; i++) {
System.out.println(" x[" + i + "] : " + x[i]);
}
}
}
swap() 메소드는 변수 3가지를 통해 값을 교환하는 방식.
x = y; y = x; 방식으로 교환시, 같은 값으로 할당됨. 그러므로 임의 변수 t를 잡고 변수를 교환함.
배열의 교환 횟수는 : n / 2
public class PracticeReverseArray {
static void swap(int[] a, int idx1, int idx2) {
int t = a[idx1];
a[idx1] = a[idx2];
a[idx2] = t;
}
public static void main(String[] args) {
int[] x = { 5, 10, 73, 2, -5, 42 };
for (int j = 0; j < x.length; j++) {
System.out.print(x[j] + " ");
}
for (int i = 0; i < x.length / 2; i++) {
swap(x, i, x.length - 1 - i);
System.out.println();
System.out.println("a[" + i + "]과(와) a[" + (x.length - i - 1) + "]를 교환합니다.");
for (int j = 0; j < x.length; j++) {
System.out.print(x[j] + " ");
}
}
System.out.println();
System.out.println("역순 정렬을 마쳤습니다.");
}
}
public class PracticeReverseArray {
static void swap(int[] a, int idx1, int idx2) {
int t = a[idx1];
a[idx1] = a[idx2];
a[idx2] = t;
}
public static void main(String[] args) {
int[] x = { 5, 10, 73, 2, -5, 42 };
for (int j = 0; j < x.length; j++) {
System.out.print(x[j] + " ");
}
for (int i = 0; i < x.length / 2; i++) {
swap(x, i, x.length - 1 - i);
System.out.println();
System.out.println("a[" + i + "]과(와) a[" + (x.length - i - 1) + "]를 교환합니다.");
for (int j = 0; j < x.length; j++) {
System.out.print(x[j] + " ");
}
}
System.out.println();
System.out.println("역순 정렬을 마쳤습니다.");
}
}
public class PracticeSumArray {
static int sumOf(int[] a) {
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
public static void main(String[] args) {
int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
System.out.println(sumOf(x));
}
}
두 배열의 모든 요소가 같은지 판별하는 메소드 구현
public class ArrayEqual {
static boolean equals(int[] a, int[] b) {
if (a.length != b.length)
return false;
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i])
return false;
}
return true;
}
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("배열 a의 요소수 : ");
int na = stdIn.nextInt();
int[] a = new int[na];
for (int i = 0; i < a.length; i++) {
System.out.print("a[" + i + "] : ");
a[i] = stdIn.nextInt();
}
System.out.print("배열 a의 요소수 : ");
int nb = stdIn.nextInt();
int[] b = new int[nb];
for (int i = 0; i < b.length; i++) {
System.out.print("b[" + i + "] : ");
b[i] = stdIn.nextInt();
}
System.out.println("배열 a와 b는 " + (equals(a, b) ? "같습니다" : "같지 않습니다."));
}
}
두 배열의 길이가 다르면 false, 같다면 각 요소들의 값을 비교한 후, 다르면 false, 같으면 true
package chap02;
public class PracticeCopyArray {
static void copy(int[] a, int[] b) {
if (a.length != b.length)
return;
for (int i = 0; i < a.length; i++) {
b[i] = a[i];
}
}
static void rcopy(int[] a, int[] b) {
if (a.length != b.length)
return;
for (int i = 0; i < a.length; i++) {
b[i] = a[a.length - i - 1];
}
}
public static void main(String[] args) {
int[] x = { 1, 2, 3, 4, 5, 6 };
int[] y = new int[6];
int[] z = new int[6];
copy(x, y);
for (int i = 0; i < x.length; i++) {
System.out.print(x[i] + " ");
}
System.out.println();
for (int i = 0; i < y.length; i++) {
System.out.print(y[i] + " ");
}
rcopy(y, z);
System.out.println();
for (int i = 0; i < z.length; i++) {
System.out.print(z[i] + " ");
}
}
}
길이가 다르면 복사할 수 없으니, return; 길이가 같다면 복사.
역순은 아까와 달리, 같은 배열이 아니기에 임시 변수가 필요하지 않다.
그러므로 바로 복사
출처: Do it 자료구조와 함께 배우는 알고리즘 입문 자바편