?? stringvector.java
字號:
/**
* Vector主要用來保存各種類型的對象(包括相同類型和不同類型的對象)。
* 但是在一些情況下使用會給程序帶來性能上的影響。這主要是由Vector類的兩個特點所決定的。
* 第一,Vector提供了線程的安全保護功能。即使Vector類中的許多方法同步。
* 但是如果你已經確認你的應用程序是單線程,這些方法的同步就完全不必要了。
* 第二,在Vector查找存儲的各種對象時,常常要花很多的時間進行類型的匹配。
* 而當這些對象都是同一類型時,這些匹配就完全不必要了。
* 因此,有必要設計一個單線程的,保存特定類型對象的類或集合來替代Vector類
*/
package com.ultrapower.common;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
/**
* @author VictorZheng
*
*/
public class StringVector
{
// 這兒的transient標示這個屬性不需要自動序列化
private transient String[] data;
private int count;
public int size()
{
return data.length;//count;
}
public StringVector()
{
// default size is 10
this(10);
}
public StringVector(int initialSize)
{
count=initialSize;
data = new String[initialSize];
}
public void add(String str)
{
// ignore null strings
if(str == null) { return; }
ensureCapacity(count + 1);
data[count++] = str;
}
private void ensureCapacity(int minCapacity)
{
int oldCapacity = data.length;
if (minCapacity > oldCapacity)
{
String oldData[] = data;
int newCapacity = oldCapacity * 2;
data = new String[newCapacity];
System.arraycopy(oldData, 0, data, 0, count);
}
}
public void remove(String str)
{
if(str == null)
{
return; // ignore null str
}
for(int i = 0; i < count; i++)
{
// check for a match
if(data[i].equals(str))
{
System.arraycopy(data,i+1,data,i,count-1); // copy data
// allow previously valid array element be gc'd
data[--count] = null;
return;
}
}
}
public final String getStringAt(int index)
{
if(index < 0)
{ return null; }
else if(index > count)
{
return null; // index is > # strings
}
else
{
return data[index]; // index is good
}
}
public synchronized void writeObject(java.io.DataOutputStream s)
throws java.io.IOException
{
// Write out array length
s.writeInt(count);
// Write out all elements in the proper order.
for (int i=0; i<count; i++)
s.writeUTF(data[i]);
}
public synchronized void readObject(java.io.DataInputStream s)
throws java.io.IOException, ClassNotFoundException
{
System.out.println("Enter readObject");
// Read in array length and allocate array
int arrayLength = s.readInt();
System.out.println("StringVector count=" + arrayLength);
data = new String[arrayLength];
// 同步data的大小
count = arrayLength;
// Read in all elements in the proper order.
for (int i=0; i<arrayLength; i++)
{
data[i] = s.readUTF();
System.out.println("讀入:" + data[i]);
}
}
public byte[] serialize()
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try
{
writeObject(dos);
baos.close();
dos.close();
}
catch(Exception exc)
{
exc.printStackTrace();
}
finally
{
}
return baos.toByteArray();
}
public static StringVector deserialize(byte[] data) {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
StringVector sv = new StringVector();
try
{
sv.readObject(dis);
bais.close();
dis.close();
}
catch(Exception exc)
{
exc.printStackTrace();
sv = null;
}
finally
{
}
return sv;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -