Altering Arrays#
Think of these as a collection of fun functions, but just for arrays!
Sorting#
Luckily, sorting is something machines are very good at. But how do we do it? We could resort to the fellow hungarian dancers and implement sorting ourselves, but I also already mentioned that we usually don't need to do that because programming languages already include good sorting algorithms. Processing also has shorthand for sorting. Helpfully, this is just called sort()
and it takes an array as its first parameter and returns a new array! Let's measure how tall everyone is, note things down and then order the list:
// body height measured in centimeters
int[] bodyHeights = {175, 184, 157, 191, 180, 162, 165};
println(bodyHeights);
// this will print out
// [0] 175
// [1] 184
// [2] 157
// [3] 191
// [4] 180
// [5] 162
// [6] 165
int[] sortedBodyHeights = sort(bodyHeights);
println(sortedBodyHeights);
//this will now print out
// [0] 157
// [1] 162
// [2] 165
// [3] 175
// [4] 180
// [5] 184
// [6] 191
Processing's way vs. Java's way
in this case, sort()
returns a new array. This is what Processing chose to do in this case. There is also Arrays.sort()
that comes with Java itself, and this is a way to sort an array in place - so no new array is created and your existing array in the existing variable is changed instead. We're sticking to Processing as much as possible, but things like these often happen in programming. When in doubt, always take a quick look at the reference documentation!
Adding Elements to an Existing Array#
Processing offers you append()
, which will create a new array that will also include the new item you just appended. It looks like this:
float[] historicHighwaterMarks = {1.0, 1.2, 1.5, 1.8};
historicHighwaterMarks = append(historicHighwaterMarks, 2.0);
println(historicHighwaterMarks);
// this will print
// [0] 1.0
// [1] 1.2
// [2] 1.5
// [3] 1.8
// [4] 2.0
Append always adds one single element to the array and returns the new array. In the example above, we're just writing it back to our existing variable historicHighwaterMarks
.
If you need to add more than one element, expand()
lets you create as many spots as you like. It works slightly differently because it will only create those extra spots, not fill them at the same time.
float[] ratings = {5.0, 4.9};
ratings = expand(ratings, 7);
ratings[5] = 4.6;
println(ratings);
// this will print
// [0] 5.0
// [1] 4.9
// [2] 0.0
// [3] 0.0
// [4] 0.0
// [5] 4.6
// [6] 0.0
Note how there are a bunch of 0.0
elements in our array now. It used to be two elements long, and now it has seven elements (like we requested). We can then set whatever value we like to those new elements without getting an IndexOutOfBoundsException
. This is again a function that returns an array, we could choose to store it in an altogether new variable instead. We're just choosing to overwrite our old, existing ratings
variable instead.
Adding to the front of the Array
If you want to add elements of the beginning of your array, splice()
can be used. More about splice below!
Removing an item from an array#
You can use shorten()
to remove the last element from an array. I'll let you figure this one out from the reference documentation.
Removing from the front
If you want to remove the first element instead, you can use the subset()
-function!
Inserting Multiple Elements in the Middle#
The final modification of our arrays we'll get into is splice()
– and it's a slightly more comlicated one.
splice()
, will also take an array and it will return an array back to you. But it will also take a second array and an index at which to stuff that new array. Let's write some code:
String[] sadSandwich = {
"top bun",
"onions",
"lettuce",
"bottom bun"
};
String[] fillings = {
"sauce",
"falafel",
"cucumber"
};
String[] awesomeSandwich = splice(sadSandwich, fillings, 2);
println(awesomeSandwich);
We're splicing together sadSandwich
and fillings
, but the fillings
, obviously, must be in the correct place. You can't just place them on the top bun. Instead, we're specifying to put all the fillings
at index 2
— where lettuce used to live — pushing all the remaining elements further back. Our Result will be:
- top bun
- onions
- sauce
- falafel
- cucumber
- lettuce
- bottom bun
Which, in my opinion, is a pretty solid sandwich!
Searching in an Array#
Our array may be a long list of things that we don't fully know, but we might still search for something in it. The easier question could be "Is this name even in my list of names?" That is something that Java offers, but Processing does not have a shorthand for it.
import java.util.Arrays;
String[] names = {
"Ethan",
"Riley",
"Caleb",
"Chloe",
"Dylan",
"Hannah",
"Jacob"
};
String searchString = "Chloe";
for (int i = 0; i < names.length; i++) {
if (names[i].equals(searchString)) {
println("We found " + searchString + " at index " + i);
}
}
Yep, we're just going over all the items manually in our own for loop, and once we find what we were looking for, we know where it is! You could of course put this into a function if you need this a lot. There's one thing about comparing strings in there that we'll get to in our next article.
Other Languages
Programming Languages differ a lot, and one of the ways they differ may be how they treat things like arrays. Arrays in C or in Java are somewhat "stiff". JavaScript and Python for example let you change arrays much more dynamically. If you are slightly advanced at programming already, look at ArrayList and Set in Java, which are slightly more dynamic and advanced in that regard.