Difference between revisions of "Arrays"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Arrays & Repetition)
 
(31 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
===TRC Video===
 +
<youtube>https://www.youtube.com/watch?v=J6E4Neml7Ho</youtube>
 +
 +
https://www.youtube.com/watch?v=J6E4Neml7Ho
  
 
==Understanding arrays==
 
==Understanding arrays==
<p>Arrays are like a queue of people, but person number 0 is the first person in the queue. Imagine it like this:</p>
+
[[File:Arrays.jpg|200px|thumb|right|People queuing.]]
[[File:Arrays.jpg]]
+
<p>Arrays are like a queue of people, but person number 0 is the first person in the queue. Imagine it like the picture on the right. As you can see, you know that if you wanted the 3rd person in the queue you would select them something like "queue person 2" and if you wanted the last person in the queue you would say something like "queue person 4" and if you wanted the first person in the queue, you would say "queue person 0". Essentially, instead of starting counting from 1, you start counting from 0.</p>
<p>As you can see from the image above, you know that if you wanted the 3rd person in the queue you would select them something like "queue person 2" and if you wanted the last person in the queue you would say something like "queue person 4" and if you wanted the first person in the queue, you would say "queue person 0". Essentially, instead of starting counting from 1, you start counting from 0.</P>
+
<p>You should also remember that it is possible to create an array within an array. Think of it as a queue, and in each queue is another queue. These special arrays are called [[#MA|Multidimensional Arrays]]</p>
 
 
<p>You should also remember that it is possible to create an array within an array. Think of it as a queue, and in each queue is another queue.</p>
 
  
 
==Arrays in C#==
 
==Arrays in C#==
Line 11: Line 13:
  
 
Creates an <u>empty</u> array that will return integers.
 
Creates an <u>empty</u> array that will return integers.
<syntaxhighlight lang="csharp" line>int[] arrayname;</syntaxhighlight>
+
<syntaxhighlight lang="csharp">int[] arrayname; // Allocates Array to Memory But Doesn't Initalize It</syntaxhighlight>
  
 
Creates an <u>empty</u> array that will return integers, but it's size is 10. (10 people in a queue).
 
Creates an <u>empty</u> array that will return integers, but it's size is 10. (10 people in a queue).
<syntaxhighlight lang="csharp" line>arraynumbers = new int[10];</syntaxhighlight>
+
<syntaxhighlight lang="csharp">arraynumbers = new int[10]; // Allocates &amp Initalises</syntaxhighlight>
  
 
Creates an array with values for each number in the queue.
 
Creates an array with values for each number in the queue.
<syntaxhighlight lang="csharp" line>int[] numbers = {1, 2, 3, 4, 5};</syntaxhighlight>
+
<syntaxhighlight lang="csharp">int[] numbers = {1, 2, 3, 4, 5};</syntaxhighlight>
  
 
Don't forget that it can be any data type.
 
Don't forget that it can be any data type.
<syntaxhighlight lang="csharp" line>string[] strings = {"one", "two", "three", "four", "five"};</syntaxhighlight>
+
<syntaxhighlight lang="csharp">string[] strings = {"one", "two", "three", "four", "five"};</syntaxhighlight>
  
 
<p>To get something from an array, you can simply do the same as I explained before.</p>
 
<p>To get something from an array, you can simply do the same as I explained before.</p>
Line 33: Line 35:
 
//five
 
//five
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
<p id="MA">To create a '''Multidimensional Array''' add a comer in the square brackets forming the array, for example:</p>
 +
 +
<tabber>
 +
C#=
 +
<syntaxhighlight lang="csharp" line>
 +
int[,] _2DArray = new int[5,5]; // Creates a 2dimensional Array
 +
_2DArray[2,2] = 7; // Accesses item at index X:2 Y:2 and sets it = to 7
 +
 +
Console.WriteLine("{0} AT X:2 Y:2", _2DArray[2,2]);
 +
</syntaxhighlight>
 +
</tabber>
 +
 +
For '''3Dimensional Arrays''' the same process applies, just with another commer, and the same continues for 4, 5, 6... dimensional arrays.
 +
 +
<tabber>
 +
C#=
 +
<syntaxhighlight lang="csharp" line>
 +
int[,,] _3DArray = new int[5,5,5]; // Creates a 3dimensional Array
 +
_3DArray[2,2,2] = 7; // Accesses item at index X:2 Y:2 Z:2 and sets it = to 7
 +
 +
Console.WriteLine("{0} AT X:2 Y:2 Z:2", _2DArray[2,2,2]);
 +
</syntaxhighlight>
 +
</tabber>
 +
 +
To define the items contained in a three dimensional array during runtime, you must use multiple curly braces ({CURLY}) for each dimension however this becomes impractical for any arrays possesing a dimension greater then 2.
 +
 +
<tabber>
 +
C#=
 +
<syntaxhighlight lang="csharp" line>
 +
int[,] _2DArray = new [,] {
 +
    {1, 2, 3, 4, 5}
 +
    {6, 7, 8, 9, 10}
 +
};
 +
 +
// Creates new array with 2 rows and 5 columns
 +
</syntaxhighlight>
 +
</tabber>
  
 
==Arrays & Repetition==
 
==Arrays & Repetition==
Line 38: Line 78:
 
A for loop can be used to access every element within a array, this could be to read or write from the array:
 
A for loop can be used to access every element within a array, this could be to read or write from the array:
 
<syntaxhighlight lang=csharp line>
 
<syntaxhighlight lang=csharp line>
  int[] NumList = new int[MaxListSize];
+
  int[] NumList = new int[1000];
            // program loop
+
Random RanNum = new Random();
            do
+
for (int i = 0; i < 1000; i++)  
            {
+
{
                // display menu and get menu choice
+
      NumList[i] = RanNum.Next(1,10000);
                MenuItem = GetMenuChoice();
+
}
                // do something with the menu choice
+
</syntaxhighlight>
                switch (MenuItem)
+
 
                {
+
This declares an array of 1000 integers. The for loop will cycle from 0 to the thousandth item and each cycle will add a random number into that element of the array. You could use a for loop to read the list also:
                    case "1":
+
 
                        Array.Clear(NumList,0,0);
+
<syntaxhighlight lang=csharp line>
                        Random RanNum = new Random();
+
for (int i = 0; i < 1000; i++)  
                        for (int i = 0; i < MaxListSize; i++) {
+
{
                            NumList[i] = RanNum.Next(1,10000);
+
      Console.WriteLine(NumList[i]);
                        }
+
}
 +
</syntaxhighlight>
 +
 
 +
If you only wish to read from the array you could also use a foreach loop:
 +
 
 +
<syntaxhighlight lang=csharp line>
 +
foreach (int value in NumList)
 +
{
 +
      Console.WriteLine(value);
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
==Downsides==
 +
*An array can only be of a single data type
 +
*Once declared and initialised, memory is allocated for the whole structure even if it is empty
 +
*Normally they can't be resized once created, ie a Static Data Type.
 +
*Operations such as removing an element or inserting a value in the middle of the array require excessive processing and movement.
 +
 +
 +
For a more detailed examination of these points see: [[Array Downsides extended]]

Latest revision as of 08:21, 23 August 2023

TRC Video

https://www.youtube.com/watch?v=J6E4Neml7Ho

Understanding arrays

People queuing.

Arrays are like a queue of people, but person number 0 is the first person in the queue. Imagine it like the picture on the right. As you can see, you know that if you wanted the 3rd person in the queue you would select them something like "queue person 2" and if you wanted the last person in the queue you would say something like "queue person 4" and if you wanted the first person in the queue, you would say "queue person 0". Essentially, instead of starting counting from 1, you start counting from 0.

You should also remember that it is possible to create an array within an array. Think of it as a queue, and in each queue is another queue. These special arrays are called Multidimensional Arrays

Arrays in C#

There are several ways of declaring arrays in C#. There are several ways shown below:

Creates an empty array that will return integers.

int[] arrayname; // Allocates Array to Memory But Doesn't Initalize It

Creates an empty array that will return integers, but it's size is 10. (10 people in a queue).

arraynumbers = new int[10]; // Allocates &amp Initalises

Creates an array with values for each number in the queue.

int[] numbers = {1, 2, 3, 4, 5};

Don't forget that it can be any data type.

string[] strings = {"one", "two", "three", "four", "five"};

To get something from an array, you can simply do the same as I explained before.

1 string[] strings = {"one", "two", "three", "four", "five"}; //Declares strings as an array of strings.
2 Console.WriteLine(strings[0]); //Writes the first thing in the array
3 Console.WriteLine(strings[2]); //Writes the third thing in the array
4 Console.WriteLine(strings[4]); //Writes the fifth thing in the array
5 //Output:
6 //one
7 //three
8 //five

To create a Multidimensional Array add a comer in the square brackets forming the array, for example:

1 int[,] _2DArray = new int[5,5]; // Creates a 2dimensional Array
2 _2DArray[2,2] = 7; // Accesses item at index X:2 Y:2 and sets it = to 7
3 
4 Console.WriteLine("{0} AT X:2 Y:2", _2DArray[2,2]);

For 3Dimensional Arrays the same process applies, just with another commer, and the same continues for 4, 5, 6... dimensional arrays.

1 int[,,] _3DArray = new int[5,5,5]; // Creates a 3dimensional Array
2 _3DArray[2,2,2] = 7; // Accesses item at index X:2 Y:2 Z:2 and sets it = to 7
3 
4 Console.WriteLine("{0} AT X:2 Y:2 Z:2", _2DArray[2,2,2]);

To define the items contained in a three dimensional array during runtime, you must use multiple curly braces ({CURLY}) for each dimension however this becomes impractical for any arrays possesing a dimension greater then 2.

1 int[,] _2DArray = new [,] {
2     {1, 2, 3, 4, 5}
3     {6, 7, 8, 9, 10}
4 }; 
5 
6 // Creates new array with 2 rows and 5 columns

Arrays & Repetition

A for loop can be used to access every element within a array, this could be to read or write from the array:

1  int[] NumList = new int[1000];
2  Random RanNum = new Random();
3  for (int i = 0; i < 1000; i++) 
4  {
5       NumList[i] = RanNum.Next(1,10000);
6  }

This declares an array of 1000 integers. The for loop will cycle from 0 to the thousandth item and each cycle will add a random number into that element of the array. You could use a for loop to read the list also:

1  for (int i = 0; i < 1000; i++) 
2  {
3       Console.WriteLine(NumList[i]);
4  }

If you only wish to read from the array you could also use a foreach loop:

1  foreach (int value in NumList) 
2  {
3       Console.WriteLine(value);
4  }

Downsides

  • An array can only be of a single data type
  • Once declared and initialised, memory is allocated for the whole structure even if it is empty
  • Normally they can't be resized once created, ie a Static Data Type.
  • Operations such as removing an element or inserting a value in the middle of the array require excessive processing and movement.


For a more detailed examination of these points see: Array Downsides extended