package etc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class BinarySearchClassTest {
public static void main(String[] args) {
ArrayList<Car> list = new ArrayList<Car>();
list.add(new Car(1000, "hi"));
list.add(new Car(12350, "hi"));
list.add(new Car(123400, "hi"));
list.add(new Car(11100, "hi"));
list.add(new Car(500, "hi"));
list.add(new Car(130, "hi"));
Collections.sort(list, new Com());
int index = Collections.binarySearch(list, new Car(500, "aa"), new Com());
System.out.println(list.toString());
System.out.println("index : " + index);
System.out.println(list.get(index));
}
}
class Com implements Comparator<Car> {
@Override
public int compare(Car o1, Car o2) {
// TODO Auto-generated method stub
return o1.price - o2.price;
}
}
class Car {
int price;
String name;
public Car(int price, String name) {
super();
this.price = price;
this.name = name;
}
@Override
public String toString() {
return "Car [price=" + price + ", name=" + name + "] \n";
}
}
'소프트웨어 개발 > Java - Basic' 카테고리의 다른 글
[링크] URLClassLoader (0) | 2017.01.18 |
---|---|
가능한 알파벳조합을 set으로 구하기 (0) | 2016.07.04 |
Java Reflection (리플렉션) 을 이용해 private 특정 타입 필드를 죄다 반올림하기. (0) | 2016.04.17 |
SerialVersionUID가 뭐냐? (0) | 2016.04.10 |
임시 (0) | 2016.04.04 |