📄 index.java
字号:
package edu.stanford.nlp.util;import java.util.*;/** * Index is a list which maps between an Object vocabulary and * contiguous integer indices. It supports constant-time lookup in * both directions. * <p> * The typical usage would be: * <p><code>Index index = new Index(collection);</code> * <p> followed by * <p><code>int i = index.indexOf(object);</code> * <p> or * <p><code>Object o = index.get(i);</code> * The source contains a concrete example of use. * * @author <a href="mailto:klein@cs.stanford.edu">Dan Klein</a> * @version 1.0 * @since 1.0 * @see AbstractList */public class Index extends AbstractList { protected List objects = new ArrayList(); protected Map indexes = new HashMap(); protected boolean locked = false; /** Returns the index of each elem in an array. */ public int[] indices(List elems) { int[] indices=new int[elems.size()]; for(int i=0;i<elems.size();i++) indices[i]=indexOf(elems.get(i)); return(indices); } public int size() { return objects.size(); } public Object get(int i) { return objects.get(i); } public boolean isLocked() { return locked; } public void lock() { locked = true; } public void unlock() { locked = false; } public int indexOf(Object o) { Integer index = (Integer)indexes.get(o); if (index == null) return -1; return index.intValue(); } public int lastIndexOf(Object o) { return indexOf(o); } public boolean addAll(Collection c) { boolean changed = false; for (Iterator cI = c.iterator(); cI.hasNext();) { changed &= add(cI.next()); } return changed; } public List subList(int from, int to) { throw new UnsupportedOperationException(); } public boolean add(Object o) { Integer index = (Integer)indexes.get(o); if (index == null && ! locked) { index = new Integer(objects.size()); objects.add(o); indexes.put(o, index); modCount++; return true; } return false; } public boolean contains(Object o) { return indexes.containsKey(o); } public Index() { super(); } public Index(Collection c) { super(); addAll(c); } public static void main(String[] args) { List list = new ArrayList(); list.add("A"); list.add("B"); list.add("A"); list.add("C"); Index index = new Index(list); System.out.println("Index size: "+index.size()); System.out.println("Index has A? : "+index.contains("A")); System.out.println("Index of A: "+index.indexOf("A")); System.out.println("Index of B: "+index.indexOf("B")); System.out.println("Index of C: "+index.indexOf("C")); System.out.println("Object 0: "+index.get(0)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -