Java-Written Stringlist class

Source: Internet
Author: User
Tags array arrays return stringbuffer

Delphi Stringlist class really good use ah try to write a 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 (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 ());
}


}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.