⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linearlistsearcher.java

📁 BOOK:Beginning Algorithms Code Examples
💻 JAVA
字号:
package com.wrox.algorithms.bsearch;import com.wrox.algorithms.iteration.Iterator;import com.wrox.algorithms.lists.List;import com.wrox.algorithms.sorting.Comparator;/** * A {@link ListSearcher} that performs a linear scan. * */public class LinearListSearcher implements ListSearcher {    /** The strategy to use for key comparison. */    private final Comparator _comparator;    /**     * Constructor.     *     * @param comparator The strategy to use for key comparison.     */    public LinearListSearcher(Comparator comparator) {        assert comparator != null : "comparator can't be null";        _comparator = comparator;    }    public int search(List list, Object value) {        assert list != null : "list can't be null";        int index = 0;        Iterator i = list.iterator();        for (i.first(); !i.isDone(); i.next()) {            int cmp = _comparator.compare(value, i.current());            if (cmp == 0) {                return index;            } else if (cmp < 0) {                break;            }            ++index;        }        return -(index + 1);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -