/*
* int a[] = {1, 2, 3, 4, 5};
* int b[] = {1, 2, 3, 4, 5};
*
* a == b // 거짓, 참조형 변수이기 때문에 서로의 주소값을 비교한다.
*/
public class Test004 {
public static void main(String[] args) {
int a[] = {1, 2, 3, 4, 5};
int b[] = a; // 이중참조, 주소만 가져온다.
int c[] = new int[a.length];
int d[] = {1, 2, 3, 4, 5};
for(int i=0; i<a.length; i++)
c[i] = a[i]; // 복사, 진짜 메모리에 복사를 한다.
System.out.println(compareArray(a,b)); // true
System.out.println(a == b); // true
System.out.println(compareArray(a,c)); // true
System.out.println(a == c); // false
}
static boolean compareArray(int a[], int b[]) {
int i;
if(a.length == b.length) {
for(i=0; i<a.length; i++)
if(a[i] != b[i])
break;
if (i < a.length)
return false;
else
return true;
}
else
return false;
}
}
댓글 없음:
댓글 쓰기