站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题一个简化的存储long型数据的List

一个简化的存储long型数据的List

摘要: 一个简化的存储long型数据的List

</td> </tr> <tr> <td width="501" height="35" valign="top" class="ArticleTeitle">
/**
* $RCSfile: LongList.java,v $
* $Revision: 1.1 $
* $Date: 2004/06/11 09:09:16 $
*
* New Jive from Jdon.com.
*
* This software is the proprietary information of CoolServlets, Inc.
* Use is subject to license terms.
*/


/**
* A simplified List for long values. Only the bare number of methods needed
* by Jive have been implemented so far, so additional implementation work
* would be welcome.


*
* The implementation uses an array for maximum speed. If the number of elements
* grows larger than capacity, the capacity will automatically grow.
*/
public class LongList {

long [] elements;
int capacity;
int size;

/**
* Creates a new list of long values with a default capacity of 50.
*/
public LongList() {
this(50);
}

/**
* Creates a new list of long values with a specified initial capacity.
*
* @param initialCapacity a capacity to initialize the list with.
*/
public LongList(int initialCapacity) {
size = 0;
capacity = initialCapacity;
elements = new long[capacity];
}

/**
* Adds a new long value to the end of the list.
*/
public void add(long value) {
elements[size] = value;
size++;
if (size == capacity) {
capacity = capacity * 2;
long[] newElements = new long[capacity];
for (int i=0; i newElements[i] = elements[i];
}
elements = newElements;
}
}

/**
* Returns the long value at the specified index. If the index is not
* valid, an IndexOutOfBoundException will be thrown.
*
* @param index the index of the value to return.
* @return the value at the specified index.
*/
public long get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index " + index + " not valid.");
}
return elements[index];
}

/**
* Returns the number of elements in the list.
*
* @return the number of elements in the list.
*/
public int size() {
return size;
}

/**
* Returns a new array containing the list elements.
*
* @return an array of the list elements.
*/
public long[] toArray() {
int size = this.size;
long[] newElements = new long[size];
for (int i=0; i newElements[i] = elements[i];
}
return newElements;
}

public String toString() {
StringBuffer buf = new StringBuffer();
for (int i=0; i<this.size; i++) { }
return buf.toString();
}
</td> <td width="183" valign="top" class="ArticleTeitle">
</td> </tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">


↑返回目录
前一篇: 一个用DES来加密、解密的类
后一篇: java 做的代理搜寻