04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

682<br />

String Functions<br />

The String Functions section has seven functions that are helpful when working with<br />

strings, including join(), which combines elements in an array into a string; nf(), which<br />

formats numbers into strings; <strong>and</strong> split(), which separates a series of data within a string<br />

into separate arrays of strings. Here’s a little example using the String function trim(),<br />

which removes whitespace characters around strings:<br />

String s1 = " Hi there, ";<br />

String s2 = " bye now. ";<br />

println(s1+s2);<br />

s1 = trim(s1);<br />

s2 = trim(s2);<br />

println(s1+" "+s2);<br />

Array Functions<br />

The Array Functions section includes nine functions that allow you to manipulate arrays. In<br />

Java, the size of an array can’t be changed once its length has been set. Values at specific<br />

index positions within arrays can easily be changed—as in scores[3] = 23; or names[4]<br />

= "Bella";—<strong>and</strong> there are ways of easily sorting <strong>and</strong> searching though arrays (see Java’s<br />

Arrays class). However, in Java, if you want to change the length of an array, you need to<br />

copy the contents of the array into a new array that has been initialized with the new<br />

desired length (too much work). I’ll provide an example of this process later in this section.<br />

There are two additional classes in Java, ArrayList <strong>and</strong> Vector, that (sort of) function as<br />

mutable arrays, in that they can dynamically change size. However, using these more complex<br />

structures necessitates converting data types (type casting, which I discussed a couple<br />

paragraphs back), <strong>and</strong> also requires some extra syntax (which I’m not going to cover here).<br />

To learn more about Java’s mutable array structures, check out http://java.sun.com/<br />

j2se/1.4.2/docs/api/java/util/Vector.html.<br />

<strong>Processing</strong> arrays, in conjunction with the array functions, are a hybrid solution—retaining<br />

the simplicity of using a st<strong>and</strong>ard Java array while also providing the ability to modify the<br />

array (including altering its length), similar to Java’s ArrayList <strong>and</strong> Vector classes.<br />

<strong>Processing</strong>’s array functions include append() <strong>and</strong> shorten(), which respectively add or<br />

subtract data to or from the end of an array, changing the array’s length by one position;<br />

splice(), which adds a single value or an entire array of values into an array at a specific<br />

index position, also changing the length of the array by the number of total values added;<br />

<strong>and</strong> subset(), which extracts an array of elements out of an array at a specific index position.<br />

The subset() function doesn’t affect the initial array.<br />

In the following sections, I’ve included some example implementations of an append()<br />

function. The first sketch example demonstrates the long way to append, as you might<br />

using Java arrays. The second example uses <strong>Processing</strong>’s built-in append() function. Finally,<br />

the third example uses <strong>Processing</strong>’s append() function again, but with an array full of<br />

object references instead of primitive values.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!