Today I studied arrays. Before explaining what an array is,
I want to re-explain defining variables. For example if I want to make a variable
that represents an integer, I must type (int variable_name = integer;). The (int)
is used to tell the software to make a space big enough to hold an integer in
memory.
When defining an array of integers you must type (int[]
variable_ name;). Unlike an integer an array of integers doesn’t hold anything,
it can only refer to a list of integers in memory. So a variable for an integer
is similar to a bucket, but a variable for an array is similar to a label that
refers to buckets. When allocating memory to a variable in an array you can use
the key word new. This allows you to put a certain amount of place holders in
that array. You can then allocate each index separately with an integer.
Another cool thing you can do with array is iterate through
it. This can be done using a for loop. One key point to remember when making a
for loop. Is that you must go from i=0 to i<array.length. This is necessary,
because the index of arrays starts at 0 and ends at one minus the length of the
array.
No comments:
Post a Comment