2017년 11월 10일 금요일

JAVA 배열 실습) 배열의 이중참조, 복사 및 비교

// 배열의 이중참조, 복사 및 비교
/*
 * 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;
}

}

댓글 없음:

댓글 쓰기

시스템 보안

1. 내컴퓨터는 해킹이 당한적 있는가? 있다. 2. 컴퓨터를 하게되면 해킹을 당한다. 무엇을 이용해 해킹을 하는가? 인터넷이 가장 보편적. 사회적인 공격(주변인이 사전정보를 가지고 해킹) 3. 대응을 어떻게 해야하나? 보안프로...