If we wanted to keep track of the score in a game, we stored the current score in a variable. But what if we had a game with 100 levels and we wanted to keep track of the score that the player got at each level?
Unfortunately, variables can only store ONE thing at time, which means we would have to declare and initialize 100 variables in our program. Doing all that writing is very tedious, which is why we use arrays to initialize a lot of variables at one time. An array is like a list of variables.
Every item in the array must be the same data type, which means that an array can only store all integers or all data type array name we are initializing the array with 5 elements the brackets mean that we are declaring an array booleans or all strings.
An array has a fixed size. This means that when you declare the array, you must tell Processing how many items you are going to store in the array. So if you tell Processing you want 5 items in your array you can not increase the array size to 6 or decrease the array size to 2 later in your program.
We assign a value to the array by writing the array name and the index position of of the value between brackets.
int[] scores = new int[5]; scores[0] = 3; scores[1] = 2; scores[2] = 3; scores[3] = 5; scores[4] = 1;
int[] times = {12, 34, 56, 14, 39, 13}; println(“The size of times is: “ + times.length);
Leave a Reply