- java.lang.Object
-
- processing.data.StringList
-
public class StringList extends Object implements Iterable<String>
Helper class for a list of Strings. Lists are designed to have some of the features of ArrayLists, but to maintain the simplicity and efficiency of working with arrays. Functions like sort() and shuffle() always act on the list itself. To get a sorted copy, use list.copy().sort().
-
-
Constructor Summary
Constructors Constructor Description StringList()StringList(int length)StringList(Iterable<String> iter)Create from something iterable, for instance: StringList list = new StringList(hashMap.keySet());StringList(Object... items)Construct a StringList from a random pile of objects.StringList(String[] list)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappend(String value)Add a new entry to the list.voidappend(String[] values)voidappend(StringList list)voidappendUnique(String value)Add this value, but only if it's not already in the list.String[]array()Create a new array with a copy of all the values.String[]array(String[] array)Copy values into the specified array.voidclear()Remove all entries from the list.StringListcopy()Stringget(int index)Get an entry at a particular index.IntDictgetOrder()Create a dictionary associating each entry in this list to its index.StringListgetSubset(int start)StringListgetSubset(int start, int num)IntDictgetTally()Count the number of times each String entry is found in this list.String[]getUnique()Get a list of all unique entries.booleanhasValue(String value)intindex(String what)Return the first index of a particular value.voidinsert(int index, String value)voidinsert(int index, String[] values)voidinsert(int index, StringList list)Iterator<String>iterator()Stringjoin(String separator)voidlower()Make the entire list lower case.Stringpop()voidprint()voidpush(String value)Just an alias for append(), but matches pop()Stringremove(int index)Remove an element from the specified index.intremoveValue(String value)intremoveValues(String value)intreplaceValue(String value, String newValue)intreplaceValues(String value, String newValue)voidresize(int length)voidreverse()voidsave(File file)Save tab-delimited entries to a file (TSV format, UTF-8 encoding)voidset(int index, String what)Set the entry at a particular index.voidshuffle()Randomize the order of the list elements.voidshuffle(PApplet sketch)Randomize the list order using the random() function from the specified sketch, allowing shuffle() to use its current randomSeed() setting.intsize()Get the length of the list.voidsort()Sorts the array in place.voidsortReverse()Reverse sort, orders values from highest to lowest.StringtoJSON()Return this dictionary as a String in JSON format.StringtoString()voidupper()Make the entire list upper case.String[]values()Returns the actual array being used to store the data.voidwrite(PrintWriter writer)Write entries to a PrintWriter, one per line-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
StringList
public StringList()
-
StringList
public StringList(int length)
-
StringList
public StringList(String[] list)
-
StringList
public StringList(Object... items)
Construct a StringList from a random pile of objects. Null values will stay null, but all the others will be converted to String values.
-
-
Method Detail
-
size
public int size()
Get the length of the list.
-
resize
public void resize(int length)
-
clear
public void clear()
Remove all entries from the list.
-
get
public String get(int index)
Get an entry at a particular index.
-
set
public void set(int index, String what)Set the entry at a particular index. If the index is past the length of the list, it'll expand the list to accommodate, and fill the intermediate entries with 0s.
-
push
public void push(String value)
Just an alias for append(), but matches pop()
-
pop
public String pop()
-
remove
public String remove(int index)
Remove an element from the specified index.
-
removeValue
public int removeValue(String value)
-
removeValues
public int removeValues(String value)
-
append
public void append(String value)
Add a new entry to the list.
-
append
public void append(String[] values)
-
append
public void append(StringList list)
-
appendUnique
public void appendUnique(String value)
Add this value, but only if it's not already in the list.
-
insert
public void insert(int index, String value)
-
insert
public void insert(int index, String[] values)
-
insert
public void insert(int index, StringList list)
-
index
public int index(String what)
Return the first index of a particular value.
-
hasValue
public boolean hasValue(String value)
-
sort
public void sort()
Sorts the array in place.
-
sortReverse
public void sortReverse()
Reverse sort, orders values from highest to lowest.
-
reverse
public void reverse()
-
shuffle
public void shuffle()
Randomize the order of the list elements. Note that this does not obey the randomSeed() function in PApplet.
-
shuffle
public void shuffle(PApplet sketch)
Randomize the list order using the random() function from the specified sketch, allowing shuffle() to use its current randomSeed() setting.
-
lower
public void lower()
Make the entire list lower case.
-
upper
public void upper()
Make the entire list upper case.
-
copy
public StringList copy()
-
values
public String[] values()
Returns the actual array being used to store the data. Suitable for iterating with a for() loop, but modifying the list could cause terrible things to happen.
-
array
public String[] array()
Create a new array with a copy of all the values.- Returns:
- an array sized by the length of the list with each of the values.
-
array
public String[] array(String[] array)
Copy values into the specified array. If the specified array is null or not the same size, a new array will be allocated.- Parameters:
array-
-
getSubset
public StringList getSubset(int start)
-
getSubset
public StringList getSubset(int start, int num)
-
getUnique
public String[] getUnique()
Get a list of all unique entries.
-
getTally
public IntDict getTally()
Count the number of times each String entry is found in this list.
-
getOrder
public IntDict getOrder()
Create a dictionary associating each entry in this list to its index.
-
print
public void print()
-
save
public void save(File file)
Save tab-delimited entries to a file (TSV format, UTF-8 encoding)
-
write
public void write(PrintWriter writer)
Write entries to a PrintWriter, one per line
-
toJSON
public String toJSON()
Return this dictionary as a String in JSON format.
-
-