package com.ebizserve.tarket.service.command;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
public class Test {
private static Logger
logger = Logger.
getLogger(Test.
class);
public static void main(String[] args) {
int index;
Set<Float> set =
new HashSet<Float>();
List<Float> list =
new ArrayList<Float>();
set.add(68.1f);
set.add(68.0f);
set.add(68.3f);
set.add(68.2f);
for (Iterator it = set.iterator(); it.hasNext();) {
list.add((Float) it.next());
}
// Collections.sort()方法用于自然排序List对象;
Collections.
sort(list);
for (Float l : list) {
logger.info("自然排序:" + l);
}
logger.info("-------------------------------------");
// Collections.reverse()方法用于倒转排序List对象;
Collections.
reverse(list);
for (Float l : list) {
logger.info("倒转排序:" + l);
}
// Collections.binarySearch()方法检索对象所在位置;
int k = Collections.
binarySearch(list,
new Float(68.0));
// list.indexOf()方法检索对象所以位置;
// int k = list.indexOf(68.0f);
logger.info("k---------" + k);
index = k + 1;
logger.info("index---------" + index);
}
}
Collections.
binarySearch()方法查找的索引位置不正确,后改为用List.indexOf()方法;请同志们分析下。