Here is very basic description on what is an array. I tried to describe things as simple as possible. This post is meant to be for people who just started programming.
Intuitively you can think that array is a shelf with multiple enumerated drawers, something like this:
Yep, it’s that simple. But there is a catch - you can put only one thing into each drawer…
Speaking a bit more formally:
Array is a collection of elements ordered using indexes.
What I can do with an array?
Let’s start from what you can do with shelf?. You can:
- create it (or buy one)
- get thing from a drawer
- change (set) thing which is in some drawer (but remember to keep only one thing there!)
- check how many drawers you have
The same with arrays - you can create, get and set elements. There are more operations you can do, but it depends on programming language you use, so we are not going there in this post.
Create an array
Now, it’s time for some code examples.
Intuitively creating an array is like creating a shelf with drawers. We usually have to tell how many drawers we need, before we can start putting things in them. Or we can just say:
I want to put that thing, this another thing and also that one into my drawers, so please create a shelf in which I can store these things.
To create an array we usually have to just declare it and ascribe it to
some variable (in this case it’s called
In code snippet above
myArray is a shelf with drawers indexed from 0 to 2 (because I
wanted to have 3 elements in my array):
Beware - arrays are indexed starting from 0 instead of 1 in almost all programming languages (except Pascal).
Access an array
Now you want to check what you put in one of your drawers.
For accessing arrays elements usually we use
 in which we put
number of index we want to access. Actually, I know only one example
of programming language where accessing elements looks differently - Erlang.
and plenty other it is the same.
myArray means - I want to get this thing, which is
in my drawer number 0.
myArray = "aa"; means I want to put
aa into my drawer number 0.
There are more operations you can do for arrays, but these are the basic ones and the most important ones, so let’s not go further now.
Array in different programming languages
Here are some examples of an array in some other programming languages. I present here how to create an array of five integers (numbers) and how to get and set an element.
int means that we declare an array of integers. Setting and
C# in many ways is quite similar to Java. We just need to specify type of our array twice in the declaration, because we need to specify size of our array:
Almost the same as in Java, but in array declaration we have to put
after variable name instead of writing
int as its type.
It’s because memory allocation works a bit differently in C++
(we don’t want to go any further in it, don’t we?).
It’s not necessary to put a number of elements into these square brackets if we declare all elements in an array like this:
This time number of elements is going to be in square brackets before
type declaration, so it’s
In Python it’s better to use
list instead of array, but
still it’s possible to have an array (actually I’ve never used it!).
We have to import module
array then we can create an array using
constructor from this module.
'i' in array declaration means that we want to have numbers in it.
You can put other things in it, check documentation.
Erlang is a bit different language, it uses different paradigm - functional
Anyway the same as in Python, better to use
lists instead of arrays.
To learn more about arrays you can read (and code!) how to:
- check arrays length
- how to iterate over all arrays elements (for example you want to print each of elements in separate line).
I don’t give links here, because I don’t know which programming language you prefer (anyway it doesn’t really matter…). On the other hand being a software developer means that you have to be able to find answers on your own.
If you want to share your discovers or issues - please do so in comments!
Here you had a description of what is an array and how it looks like in some programming languages. Next time another important data-structure - list! It’s a bit similar to array, but there are some differences. And it has more of standard operations.
Having some thoughts? Leave a comment!