# Arrays in a nutshell!!

#### konsowa

##### Coder
Full Member
Nobleman
Hey i just thought id give out some knowledge for those of you who have just started learning the language.
Lets Begin.

So as you all know when programming one will often use variables in which important values are stored. So most of you probably already know what a variable is but just for those of you who don't, a variable is basically a memory storage location in which you may store various data. These variables can be of many different types, such as ints, booleans, strings, and even chars just to name a few. Now moving on to the main topic arrays. Arrays can be described as a group of variables that all have the same type. An important thing to note is that every array has to have a defined size. So if you have an array with 10 elements, it will always have 10 places to store data. That data can change of course, but you can never fit another element inside the array itself. One more thing to note before moving on to examples is that arrays are 0 indexed that means we start counting from zero, don't worry if you don't get it right now you will probably see what i mean in the examples.

Now to create an array you must declare its type followed by brackets, then supply its name. Like so:

C++:
``int[] myInts;``
See how easy that was?
You just created your first array, Cheers.

One thing to note though is that the array you have just created is not initialized, to initialize it follow this example:

C++:
``int[] myInts = new int[5];``
Now what you just did is you created an array that has a size of 5. This allows you to store any integer value in any of the elements in the array.

In the next example you will learn how you can give your elements an initialized value, just follow my lead:

C++:
``int[] myInts = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };``
What you have just done is that you've given each of the 10 elements a starting value. We will use this example again in a moment.

Now lets say you wish to output the value of the element to the user of your application, lets say you wish to output the value of the second element from the last example to the user. How would you go about doing that? The solution is very easy.

To get the value of the second element all you need to do is the following:

C++:
``myInts[1]``
Thought i was gonna put the number 2 there didn't you, well remember when i said arrays where zero indexed? This is what i meant.

So basically to get the value of the first element all you need to do is myInts[0], simple huh?

Now as you might have noticed, that is inappropriate code so how can i output it in a console window you ask, here is how:

C++:
``Console.WriteLine(myInts[7].ToString());``
Now disregarding the ".ToString()" part which value do you think that line is gonna output??

I won't give you the answer to that and ill just let you tell me what you think based on what you have learned.

Well we have reached the end of this tutorial, i hope i was able to help you by giving you the basics about an array

Konsowa.

S

#### Sleinzel

Nice and clean Introduction.

Full Member
Nobleman
Thanks Sleinzel

#### Flyjunkie

##### Newbie
Full Member
Nobleman
Nice little write up there Konsowa...I've repped you for it. Gotta love it when you tell people the number '0' in programming has to be taken into account lol.

Would be good to see a range of these topics on items such as classes, inheritance, methods, functions e.t.c and even basic principles such as data types, loops e.t.c

#### konsowa

##### Coder
Full Member
Nobleman
Yea, i was thinking about making tutorials about those concepts..So yea u can expect those from me