Arrays

From TRCCompSci - AQA Computer Science
Revision as of 11:48, 17 December 2016 by Mohkale (talk | contribs) (D1: DataType Suprression)
Jump to: navigation, search

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

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

D1: DataType Suprression

A Given array Can Only contain objects of a given data-type (which is set during initialisation of the array). If a person tries to ignore this, such as in the following case (a TraceBack 'DataType' Error will be produced).


1 int[] intArray = new int[1]; // Creates new array which holds Integers
2 intArray[0] = "String"; // Attempts to add string to intArray
3 // Producing an error


However using Object Oriented Programming Concepts (namely Inheritance & Polymorphism) an array can be made to contain Psuedo multiple data types. For example if an interface of type Animal, has two inherited classes Duck & Tiger, both can be added to an array of type Animal with no clipping involved (however if Animal was a class and not an interface, the inherited classes object will be forcefully casted to the class Animal and have some data lossed).


 1 Interface Animal {
 2     public Output(); // Method
 3 
 4     public int numOfLegs; // instance Value
 5     public String animalName; // instance Value
 6 }
 7 
 8 class Duck : Animal {
 9     public Duck(int numOfLegs) {
10         this.numOfLegs = numOfLegs;
11         this.animalName = "Duck";
12     }
13 
14     public Output() {
15         Console.WriteLine("{0} has {1} Legs",
16             this.animalName, this.numOfLegs);
17     }
18 }
19 
20 class Tiger : Animal {
21     public Tiger(int numOfLegs) {
22         this.numOfLegs = numOfLegs;
23         this.animalName = "Tiger";
24     }
25 
26     public Output() {
27         Console.WriteLine("{0} has {1} Legs",
28             this.animalName, this.numOfLegs);
29     }
30 }
31 
32 public Animal[2] animals; // Array Which Can Hold Animals
33 public Tigers[2] tigers; // Array Which Can Hold Tigers
34 public Duck[2] ducks; // Array Which Can Hold Ducks
35 
36 duck[0] = new Duck(2) // NoError: Adds duck to duck array; Duck Is Duck
37 duck[1] = new Tiger(4); // Error: Adds tiger to duck array; Tiger Isn't Duck
38 
39 Tiger[0] = new Duck(2) // Error: Adds duck to Tiger array; Duck Isn't Tiger
40 Tiger[1] = new Tiger(4); // NoError: Adds tiger to Tiger array; Tiger Is Tiger
41 
42 animals[0] = new Tiger(4); // NoError: Adds tiger to animal array; Tiger Is Animal
43 animals[1] = new Duck(2); // NoError: Adds duck to animal array; Duck Is Animal


Expanding on the example above, to prove no data loss has occured you can attempt to use the built in Output function. to view wether any data loss has occured.


1 animals[0].Output(); // Outputs "Tiger has 4 Legs"
2 animals[1].Output(); // Outputs "Duck has 2 Legs"