Java Reference
In-Depth Information
Each item in an array can be treated like a variable. You can change the value using the
assignment operator = . For example, we can change the value of the first item in the piz-
zas array to " Ham & Pineapple ":
pizzas[0] = "Ham & Pineapple";
If Donatello receives a few more pizza orders, he can fill up the boxes up with pizzas one
by one. We can also do this to our array by assigning more values:
pizza[1] = "Mushroom";
pizza[2] = "Spinach & Rocket"
Just as Donatello can put a pizza in any of the boxes in the pile, we can also use the index
notation to add new items to any element in the pizzas array:
pizzas[5] = "Pineapple & Sweetcorn";
<< "Pineapple & Sweetcorn"
We can look at the pizzas array by simply typing its name into the console:
<< ["Ham & Pineapple", "Mushroom", "Spinach & Rocket",
undefined, "Pineapple & Sweetcorn"]
Here we can see that the sixth item (with an index of 5 ) has been filled with the string
"P ineapple & Sweetcorn ". This has made the array longer than it was before, so all
the other unused slots in the array are filled by the value undefined , just as if Donatello
had put a pizza in the sixth box and left the other boxes empty.
Creating Array Literals
We can create an array literal using square brackets that already contain some initial values,
so there's no need to add each value one by one. So we could create another pile of pizza
boxes as the following array literal:
Search WWH ::

Custom Search