CompareUtil 클래스의 void compareList (ArrayList first, ArrayList second) 메소드를 완성하십시오.
compareList 메소드는 ArrayList 타입의 객체 2개를 입력받아 저장 데이터를 하나씩 비교하고 차이가 있는 경우에만 HashMap에 저장하여 리턴합니다.
class CompareUtilMain {
main 적절히 구현
}
<< 실행 예시 1>>
No |
첫번째 ArrayList 내용 |
두번째 ArrayList 내용 |
결과값(HashMap) |
1 |
Learn to build powerful Enterprise Java applications using the Spring Framework. |
Learn To Build Powerful Enterprise Java applications using the Spring. |
키 : 값 LINE 1:Learn To Build Powerful LINE 3:using the Spring. |
<< 실행 예시 2>>
No |
첫번째 ArrayList 내용 |
두번째 ArrayList 내용 |
결과값(HashMap) |
1 |
Learn to build powerful Enterprise Java applications using the Spring Framework. oracle database jjang |
Learn To Build Powerful Enterprise Java applications using the Spring. |
ERROR : 두 객체의 저장 갯수가 다르므로 비교가 불가능합니다. |
package CompareArrayList.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
class CompareUtil {
void CompareList(ArrayList<String> arr_first, ArrayList<String> arr_second) {
int size1 = arr_first.size();
int size2 = arr_second.size();
HashMap<String, String> map = new HashMap<String, String>();
if (size1 == size2) {
// 두데이터들 하나씩 비교
for (int i = 0; i < size2; i++) {
String s1 = arr_first.get(i);
String s2 = arr_second.get(i);
if (!(s1.equals(s2))) {
map.put("Line " + (i + 1), s2);
}
}
} else {
map.put("Error", "두 객체의 저장 갯수가 다르므로 비교가 불가능합니다.");
}
// key, data 출력
Iterator<String> it = map.keySet().iterator();
// key만 저장
while (it.hasNext() == true) {
String key = it.next();
System.out.println(key + " : " + map.get(key));
}
}
}
public class CompareUtilMain {
public static void main(String[] args) {
ArrayList<String> arr_first = new ArrayList<String>();
ArrayList<String> arr_second = new ArrayList<String>();
arr_first.add("Learn to build powerful");
arr_first.add("Enterprise Java applications");
arr_first.add("using the Spring Framework.");
arr_second.add("Learn To Build Powerful");
arr_second.add("Enterprise Java applications");
arr_second.add("using the Spring.");
// arr_second.add("oracle database jjang");
CompareUtil util = new CompareUtil();
util.CompareList(arr_first, arr_second);
}
}