stringlist.java
来自「聊天程序」· Java 代码 · 共 250 行
JAVA
250 行
/*
* @(#)StringList.java
*
* Copyright (c) 2001-2002, JangHo Hwang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the JangHo Hwang nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: StringList.java,v 1.2 2002/03/05 13:35:33 xrath Exp $
*/
package rath.msnm.util;
/**
* Vector贸烦 静绰 巴捞瘤父, 郴侩拱捞 String捞扼绰 力距捞 乐促.
* 措脚俊, Cast窍绰单 鞘夸茄 厚侩阑 例皑且 荐 乐促.
* 肚茄 积己磊俊 檬扁 capacity甫 瘤沥窍瘤 臼栏搁
* 积己窍瘤 给窍霸 窃栏肺结, 版阿缴阑 老栏难林绰 努贰胶捞促.
* <p>
* 肚茄 绢蠢 皋家靛档 synchronized登绢 乐瘤 臼栏骨肺,
* 立辟 加档档 synchronized 虐况靛甫 荤侩茄 巴焊促 4硅 狐福促.
*
* @version $Id: StringList.java,v 1.2 2002/03/05 13:35:33 xrath Exp $
* @author Jang-Ho Hwang, rath@linuxkorea.co.kr
*/
public class StringList implements java.io.Serializable, Cloneable
{
private String[] strings = null;
private int elementCount;
/**
* capacity啊 4牢 StringList 按眉甫 积己茄促.
*
* @param capacity Initial capacity甫 瘤沥茄促.
*/
public StringList()
{
strings = new String[4];
}
/**
* StringList 按眉甫 积己茄促.
*
* @param capacity Initial capacity甫 瘤沥茄促.
*/
public StringList(int capacity)
{
strings = new String[capacity];
}
private void ensureCapacity( int minCapacity )
{
int oldCapacity = strings.length;
if (minCapacity > oldCapacity)
{
String[] oldData = strings;
int newCapacity = oldCapacity * 2;
if( newCapacity < minCapacity )
{
newCapacity = minCapacity;
}
strings = new String[newCapacity];
System.arraycopy(oldData, 0, strings, 0, elementCount+1-1 );
}
}
/**
* 泅犁 郴侩拱狼 农扁甫 馆券茄促.
*
* @return String郴侩拱狼 农扁.
*/
public int size()
{
return elementCount;
}
/**
* index俊 困摹茄 巩磊凯阑 馆券茄促.
*
* @param index 啊廉棵 巩磊凯狼 困摹.
* @return 漂沥 困摹俊 乐绰 巩磊凯
*/
public String get( int index )
{
return strings[index];
}
public int getInteger( int index )
{
return Integer.parseInt( strings[index] );
}
public boolean getBoolean( int index )
{
return Boolean.valueOf( strings[index] ).booleanValue();
}
/**
* index俊 困摹茄 巩磊凯阑 力芭茄促.
* 力芭茄促绰 巴篮 null肺 父电促绰 巴捞促.
* 阜富肺 remove甫 茄促绊秦辑 size啊 函窍瘤绰 臼阑 巴捞促.
*
* @param index 力芭且 巩磊凯狼 困摹.
*/
public void remove( int index )
{
strings[index] = null;
}
/**
* 漂沥 困摹俊 漂沥 巩磊凯阑 困摹矫挪促.
*
* @param index 背眉矫懦 困摹
* @param str 背眉且 巩磊凯
*/
public void setAt(int index, String str)
{
strings[index] = str;
}
/**
* StringArray俊 巩磊凯阑 窍唱 眠啊茄促.
* 傍埃捞 葛磊甫 版快 ensureCapacity皋家靛甫 烹秦辑
* 郴何 硅凯狼 农扁甫 犬厘茄促.
*
* @param str 眠啊且 巩磊凯
*/
public void add( String str )
{
ensureCapacity(elementCount+1);
strings[elementCount++] = str;
}
public void add( Object o )
{
add( String.valueOf(o) );
}
/**
* StringArray捞 粮犁窍绰 葛电 巩磊凯阑
* 力芭茄促.
*/
public void removeAll()
{
for(int i=0; i<elementCount; i++)
strings[i] = null;
elementCount = 0;
}
/**
* 泅犁 府胶飘俊 甸绢啊乐绰 郴侩阑
* String 硅凯肺 汗荤窍咯 馆券茄促.
*
* @return String[] 屈怕狼 郴侩拱.
*/
public String[] toArray()
{
String[] str = new String[ elementCount ];
System.arraycopy( strings, 0, str, 0, elementCount );
return str;
}
/**
* 泅犁 StringList俊 乐绰 葛电 磊丰甫
* Iterator屈侥栏肺 馆券茄促.
*/
public Stringator iterator()
{
return new Stringator()
{
private int offset = 0;
public boolean hasNext()
{
return offset<elementCount;
}
public String next()
{
return strings[offset++];
}
public void remove()
{
strings[offset] = null;
}
};
}
/**
* 汗荤茄促.
*/
public synchronized Object clone()
{
try
{
StringList v = (StringList)super.clone();
v.strings = new String[elementCount];
System.arraycopy(strings, 0, v.strings, 0, elementCount);
return v;
}
catch( CloneNotSupportedException e )
{
throw new InternalError();
}
}
/**
* 捞 StringArray捞 唱鸥尘 荐 乐绰 葛电 巩磊凯阑 馆券茄促.
*
* @return 捞 StringArray捞 唱鸥尘 荐 乐绰 巩磊凯.
*/
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append( "[" );
for(int i=0; i<elementCount; i++)
{
sb.append( strings[i] );
if( (i+1)!=elementCount )
sb.append( ", " );
}
sb.append( "]" );
return sb.toString();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?