Class StringList

java.lang.Object
processing.data.StringList
All Implemented Interfaces:
Iterable<String>

public class StringList extends Object implements Iterable<String>
Helper class for a list of String objects. Lists are designed to have some features of ArrayList, but to maintain the simplicity and efficiency of working with arrays. Functions such as sort() and shuffle() always act on the list itself. To get a sorted copy, use list.copy().sort().
See Also:
  • Constructor Details

    • 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.
    • StringList

      public StringList(Iterable<String> iterable)
      Create from something iterable, for instance: StringList list = new StringList(hashMap.keySet());
  • Method Details

    • 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 null.
    • 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)
    • replaceValue

      public int replaceValue(String value, String newValue)
    • replaceValues

      public int replaceValues(String value, String newValue)
    • 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)
      Check if a value is a part of the list
    • sort

      public void sort()
      Sorts the array in place.
    • sortReverse

      public void sortReverse()
      A sort in reverse. It's equivalent to running sort() and then reverse(), but is more efficient than running each separately.
    • reverse

      public void reverse()
      Reverse the order of the list
    • shuffle

      public void shuffle()
      Randomize the order of the list elements.
    • 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.
    • choice

      public String choice()
      Return a random value from the list. Throws an exception if there are no entries available. (Can't just return null because IntList and FloatList can't do that, and would be inconsistent.)
    • removeChoice

      public String removeChoice()
    • 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.
    • iterator

      public Iterator<String> iterator()
      Specified by:
      iterator in interface Iterable<String>
    • array

      @Deprecated public String[] array()
      Deprecated.
    • toArray

      public String[] toArray()
      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

      @Deprecated public String[] array(String[] array)
      Deprecated.
    • toArray

      public String[] toArray(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.
    • 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.
    • join

      public String join(String separator)
    • 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.
    • toString

      public String toString()
      Overrides:
      toString in class Object