Go Back   CodeCall Programming Forum > Blogs > TcM
Register Blogs Search Today's Posts Mark Forums Read

Rate this Entry

Arrays, an in depth explanation

Posted 02-04-2009 at 02:31 PM by TcM

Arrays, an in depth explanation
Well, this is my first blog. I'm planning to start blogging as much as I can. I will try to keep blogging, although I can't promise anything due to school and everything. Anyways, I'm going to blog what are Arrays, the different types of Arrays, advantages of each types and so on. I will explain them in the most basic way for you to understand.

So, what are Arrays?
Arrays are lists of variables that can hold certain data in them. These when declared reserve a certain amount of space in the RAM so you can store the data in them.

Why are Arrays useful?
Let’s say that you want the user to input 10 different names or integers or whatever. For sure you are too lazy, and it’s inefficient to make 10 DIFFERENT variables to store that data, so look at the bright side… this is an advantage of being lazy, of course you won’t do it, but you still need to store that data, so that is when Arrays come in, you just declare ONE list and you will have 10 different slots to insert the data in.

So, what is the deal? Life is not that simple.
Yup, you are 100% right. When you declare an Array list the first position is the first but not number 1. In most languages you have to access the first slot by the number 0, this will get you confused the first few times that you will use arrays when you will use it in a for or a for each loop. Believe me, you will have tons of ‘out of bounds’ exceptions, that is because you will loop from 1 to 10, but in reality the Array will only have from 0 to 9, so when it tries to access the slot in the 10th position it will not be found. So yes, that is the deal, life is not that easy… but after sometime you will automatically realize this as you are coding.

And, another thing is, if you are making a for loop to loop through the array, it does make a difference if you make it loop from 0 < 10, or 0 <= 10. The 0 < 10 will work, but the 0 <= 10 will give you an ‘out of bounds’ exception. You might say, if we have from 0 to 9, why use 10? Simple, when you are starting with these things you will sometimes think in this way… we start from 0 but we have 10 slots, so you will use 10 instead of 9. But there are only 2 good combinations which are

0 < 10
0 <= 9

This only applies if you will use the looping integer to access a specific location in an array. If for example you just want to make a loop and then in it make another integer to access the array, you use whatever numbers you want, as long as you will have the number of loops as the number of slots in the array.


Different types of Arrays
There are 2 types of Arrays, dynamic and static arrays. What is the difference you might ask? The answer is simple, dynamic is something what keeps on changing, because it’s dynamic, this means that when you declare it you will not define the number of slots that you will have, as to the contrary static is something that will never change, so it will have a fixed number of slots.

An example of a static array would be an integer array of 10 slots, if we had to use C# or Java it would be int[] myArray = new int[10];

An example of a dynamic array is an ArrayList, if we had to use C# or Java it would be something like ArrayList myArray = new ArrayList();

As you can see, in the first case we declared the type and the number of slots that we want; whilst in the second case we did not declare anything besides an empty dynamic array, which will hold the data as objects.

When to use which
Well, this depends on your scenario. Let’s take a bank, in a banking system we can’t say look, we will only accept 1000 accounts and after that we will not give access to more users to create more accounts with us. Of course that cannot happen, because a bank wants as much users he can get and the number is indefinite! So in that case he will use a dynamic Array. Please keep in mind that this does not mean that a real bank uses ArrayLists, this is just for your understanding. Whilst let’s say that you want the user to enter ONLY 10 numbers, in that case you can use a static type of array.


Advantages and Disadvantages
Well, some advantages is, Arrays make life much easier for us.. imagine we need to accept 100 numbers, we can’t afford (and are too lazy) to create 100 variables.. besides that if we do that we will only have a limited number, therefore that would mean it will be like a static array.. and when what would the bank do???

You can easily read and write data to it with a simple for each, or for loop… instead of making num1 = 1; num2 = 2; etc… although we can do that in an array, we can make for example myArray[0] = 1; you just have to remember 1 thing, the array starts from 0 and not from 1 so we won’t get an error.

Well, this is an advantage of dynamic arrays rather than arrays in general. Using a dynamic array will use less ram, because if we will insert 5 numbers instead of 10, we will only have 5 slots in the ram, but in a static array, if we insert 1 or 5, if we declared an array of 10 slots, we will have 10 slots in the RAM.

Another disadvantages of static versus dynamic is that in static arrays if we need to search a specific something and get it’s position you will have to make a for loop and go through each and every slot and compare the data with the data you are searching for, but most dynamic arrays will have a built in search option, in C# it’s myArray.Contains(); theoretically this is much faster than the for loop that we will have to use for the static ones. But I can’t assure you of this, because last time I tried on a slow computer at school and tried to do some bench marking.. I could not see any difference, because both searches were done in a split of a split of a second, so could not test it properly. But, I think the most efficient way to search is using HashTables, which maybe we will discuss some other time.

Posted in Uncategorized
Views 396 Comments 2 Edit Tags Email Blog Entry
Total Comments 2

Comments

  1. Old Comment
    Jordan's Avatar
    Very informative blog, TcM! Glad to see you back and sharing information.
    Posted 02-04-2009 at 03:10 PM by Jordan Jordan is online now
  2. Old Comment
    TcM's Avatar
    Thanks. I've been busy during the holidays and had tons of assignments... now I have some free time again..
    Posted 02-04-2009 at 03:14 PM by TcM TcM is offline
 
Total Trackbacks 0

Trackbacks


All times are GMT -5. The time now is 11:19 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.



LinkBacks Enabled by vBSEO 3.1.0