CompareUtil 예제
package CompareArrayList.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
public class CompareUtilMain {
public static void main(String[] args) {
ArrayList<String> arr_first = new ArrayList<String>();
ArrayList<String> arr_second = new ArrayList<String>();
HashMap<String, String> hash = new HashMap<String, 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");
if (arr_first.size() == arr_second.size()) {
for (int i = 0; i < arr_first.size(); i++) {
if (arr_first.get(i) != arr_second.get(i)) {
hash.put(String.valueOf(i), (String)arr_second.get(i));
String express = hash.get(String.valueOf(i));
System.out.println("LINE : "+String.valueOf(i)+" "+express);
}
}
} else {
System.out.println("ERROR : 두 객체의 저장 갯수가 다르므로 비교가 불가능합니다");
}
}
}
CompareArrayList 객체
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);
}
}