我用Java创建了一个非常简单的链表:
public class LinkedList {
class Node {
public Node next;
public int item;
public Node (int item) {
this.item = item;
}
}
int listSize = 0;
Node first = null;
Node last = null;
public void add(int n) {
假设我有一个对象集合:
List<String> myList = populateMyArrayList();
//Here I am having an ArrayList with 1000 elements
哪一种方法更好:
1 : Mergesort然后二进制搜索
Collections.sort(myList);
int keyIndex = Collections.binarySearch(myList, key);
2 :顺序搜索
for(String s : myList){
if(s.equals(key)){
return s;
}
}