Array
Arrays | |
---|---|
Type | Guide |
Category | AYA/YAYA |
An array is like a table. It has lines and elements. We can access the lines/elements with an index.
The arrays are the only variables needing to be initialized before having a value affected. They are initialized by 'array = IARRAY'
A simple array would be anarray = (“Hello”,“World”) :
anarray = IARRAY anarray[0] = "Hello" anarray[1] = "World"
So here is an array of two elements, it is initialized and then the two elements are added. In the 'anarray[0]', the 0 is the index of the element. The index starts to 0 so anarray[0] is the first element. The other way to put the two elements inside it after having initialized it would be writing anarray = (“Hello”,“World”).
If the number of elements inside the array is unknown, a new element can be added by anarray = (anarray,“a new element”) or shortly anarray ,= “a new element”. The element will be added at the end of the array. But an element can be added to the beginning by anarray = (“the new element”,anarray)
The foreach loops can be used to check all the elements in an arrays.
One dimension arrays
There are two types of arrays with one dimension but all the array with two dimensions are the same.
The string arrays
A big string with ',' inside it can be considered as an array and the different elements can be accessed with an index. So anarray = “Hello,World” is an array with the first element “Hello” and the second element “World”.
This type of array doesn't need to be initialized. However all the elements inside it must be a chain of character. It can be used as a normal array though when modifying or adding an element, this element must be a chain of character otherwise it won't work. Foreach loops cannot work for this kind of array either.
The "normal" arrays
They need to be initialized and are the (“Hello”,“World”) kind arrays. The elements inside it can be of any kind, it doesn't matter. Though putting an array in an array would mess up stuffs. Foreach loops work for them.
Two dimensions arrays
They are a mix between normal and string arrays. They are a normal array which elements are strings or string arrays. They are harder to handle. Foreach loops would work only for one dimension, the “normal” one.
A two dimension array looks like this :
anarray = IARRAY anarray[0] = "Hello,World" anarray[1] = "I,feel,good"
It can be written :
anarray = IARRAY anarray = ("Hello,World","I,feel,good")
To access an element, anarray[index1][index2] would be used. So here anarray[0][1] would be “World”.
But the way of modifying the elements inside the strings of the array is different. To modify it, the string must be taken, modified, and then put back into the array :
anarray = IARRAY anarray = ("Hello,World","I,feel,good") astring = anarray[0] astring[1] = "User" anarray[0] = astring
The first line of the array is now “Hello,User”.
Arrays are very useful to stock data and to do some complicated stuffs.