delphi的stringlist类真好用啊 试着用java写了一个
package com.zhao_yi.sysutils.classes;
import java.util.list;
import java.util.arraylist;
import java.util.stringtokenizer;
import com.zhao_yi.sysutils.sysutil;
import com.zhao_yi.sysutils.*;
import java.util.arrays;
import java.util.*;
public class stringlist {
private list strings = new arraylist();
private list objects = new arraylist();
private boolean sorted = false;
public stringlist() {
}
public string[] getstrings() {
return (string[])strings.toarray(new string[strings.size()]);
}
public void setstrings(string[] array){
strings = null; objects=null;
strings = new arraylist(arrays.aslist(array));
objects = new arraylist(array.length);
}
private void rangecheck(int index) throws indexoutofboundsexception {
if ((index < 0) || (index >= getcount()))
throw new indexoutofboundsexception();
}
public int getcount() {
return strings.size();
}
public string get(int index){
return (string) strings.get(index);
}
public int find(string s) {
int i, l, h, c;
l = 0;
h = getcount() – 1;
while (l <= h) {
i = (l + h) / 2;
c = sysutil.comparestrings(get(i), s);
if (c < 0)
l = i + 1;
else {
h = i – 1;
if (c == 0)
l = i;
}
}
return l;
}
public int addobject(string s, object aobject) {
int result = -1;
if (!sorted)
result = getcount();
else
result = find(s);
insertitem(result, s, aobject);
return result;
}
public int add(string s) {
return addobject(s, null);
}
public void addstrings(stringlist strings) {
for (int i = 0; i < strings.getcount(); i++)
add(strings.get(i));
}
public void addstrings(string[] strings) {
for (int i = 0; i < strings.length; i++)
add(strings[i]);
}
public void clear() {
strings.clear();
objects.clear();
}
public void delete(int index){
strings.remove(index);
objects.remove(index);
}
public void insertitem(int index, string s, object aobject) {
strings.add(index, s);
objects.add(index, aobject);
}
public void put(int index, string s) throws illegalstateexception{
if (this.sorted)
throw new illegalstateexception(“list sorted!”);
else
strings.set(index, s);
}
public void putobject(int index, object aobject) {
objects.set(index, aobject);
}
public void exchange(int index1, int index2) {
object temp = null;
temp = strings.get(index1);
strings.set(index1, strings.get(index2));
strings.set(index2, temp);
temp = objects.get(index1);
objects.set(index1, objects.get(index2));
objects.set(index2, temp);
}
public void quicksort(int l, int r) {
if (l < r) {
int i = l;
int j = r;
string s = get(l);
while (i < j) {
while (sysutil.comparestrings(get(i), s) <= 0)
i++;
while (sysutil.comparestrings(get(j), s) > 0)
j–;
if (i < j)
exchange(i, j);
}
exchange(i, l);
if (l < j)
quicksort(l, j);
if (i < r)
quicksort(i, r);
}
}
public void setsorted(boolean value) {
if (value != sorted) {
if (value)
quicksort(0, getcount() – 1);
sorted = value;
}
}
public int indexof(string s) {
return strings.indexof(s);
}
public string gettextstr() {
stringbuffer result = new stringbuffer();
for (int i = 0; i < strings.size(); i++) {
result.append(get(i));
result.append(systems.lineseparator);
}
return result.tostring();
}
public void settextstr(string value) {
clear();
stringtokenizer tokenizer = new stringtokenizer(value, systems.lineseparator);
while (tokenizer.hasmoretokens())
add(tokenizer.nexttoken());
}
}
