Array Output - Sam36502/SimpleTUI GitHub Wiki
Displaying arrays for the user should be formatted
like a sentence. To achieve this you can use the
sentenceArray(array)
method. This will separate all
the values in your array with commas and end with
an 'and'.
Example:
ArrayOutput.sentenceArray({1, 2, 3, 4});
// Returns "1, 2, 3 and 4"
ArrayOutput.sentenceArray({3.14, 4.20, 1.41, 9.99});
// Returns "3.14, 4.20, 1.41 and 9.99"
ArrayOutput.sentenceArray({"bread", "butter", "flour", "eggs"});
// Returns "bread, butter, flour and eggs"
If there is no need for fancy sentence formatting, then
arrayOut(array)
will do just fine. This simply outputs
all values of an array surrounded with square brackets.
This includes objects as well, as long as they have a
toString()
method.
Example:
ArrayOutput.sentenceArray({1, 2, 3, 4});
// Returns "[1] [2] [3] [4]"
ArrayOutput.sentenceArray({3.14, 4.20, 1.41, 9.99});
// Returns "[3.14] [4.20] [1.41] [9.99]"
ArrayOutput.sentenceArray({"bread", "butter", "flour", "eggs"});
// Returns "[bread] [butter] [flour] [eggs]"
If you wish to specify how the values should be
formatted as they are output, then customArray()
works wonders.
It outputs all the values with whichever opening bracket,
closing bracket, and delimiter the programmer chooses.
This includes objects as well, as long as they have a
toString()
method.
Example:
ArrayOutput.sentenceArray({1, 2, 3, 4}, "<- ", " ->", "|");
// Returns "<- 1 ->|<- 2 ->|<- 3 ->|<- 4 ->"
ArrayOutput.sentenceArray({3.14, 4.20, 1.41, 9.99}, "", "", " / ");
// Returns "3.14 / 4.20 / 1.41 / 9.99]"
ArrayOutput.sentenceArray({"bread", "butter", "flour", "eggs"}, "(", ")", "-");
// Returns "(bread)-(butter)-(flour)-(eggs)"