Arrays in Programming
An array is a collection of the value of the same data type. It can store a vast amount of different values in a single variable. Array stores all the values on the specified index position. Using the index position, the value can be retrieved or stored.
The following array consist of the values 1, 3, 8, 23 and 99. The indexes are given using the numbers to identify the each element in the array. The name of the array is numbers. When you need to refer the number 23 in the array by referring as numbers(3). The location of the number 23 is 4.
The next array name is names. This consist of 4 names. Therefore we can store string values in the arrays as well.
Lists
Lists are similar to one-dimensional arrays. However, they are always mutable. Since they are always one-dimensional, they have many particular constructs, such as the pair construct and the stack construct, unlike arrays constructed with a simple integer-value construct. In Scratch the List are similar to arrays.
In Scratch we can create list and add any number of items to the list while we run the program. We can create List in Scratch by clicking the option “Make a List” inside the variable code blocks.
You can download Scratch latest version (Offline Editor) using this link to do the following practical.
In arrays we see the locations of the elements named starting by 1. But in the Scratch you can see the values starting from the one. The length is displayed below the list. This means the number of elements in the array. The above example there are four elements as {Cupcake, Pizza, Cut Fruits. Watermelon}. Therefore the length is 4.
Scratch code blocks for list
Let’s create list for days in week. We can create the list and then pull the values by clicking the plus icon on the list or using the add function as shown below.
Once we create the list, we can add new elements, update elements or delete elements on the list as below. The length of the list will be updated accordingly.
When you need to print/display value in the list we need to mention the list name and the element location. We use following code block to print a value in the list. In the first white space we mention the location of the element. In this example we print 1st element of the Days array. Therefore we can see “Monday” as the output.