Array Downsides extended

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

D1: DataType Supression

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"

D2: ArrayLength Restriction

An Array is forcefully set to have a given length, and this value cannot be implicitly ignored. for example if an array of length 5, has a value added at index 5 (remember array indexes begin at 0, not 1) an error will be produced.

 1 int[] intArray = new int[5]; // creates new array which can hold 5 items of type int
 2 
 3 intArray[0] = 5; // Value 1 Set
 4 intArray[1] = 4; // Value 2 Set
 5 intArray[2] = 3; // Value 3 Set
 6 intArray[3] = 2; // Value 4 Set
 7 intArray[4] = 1; // Value 5 Set
 8 
 9 // Note At this point all values in array have been stored
10 intArray[5] = 0; // Error Produced

Now this length restriction is entirely circumventable (avoidable), and to that end there are 2 option.

  • Use the builtin Array.Resize() method which automatically resizes a given array
    • The upside to this is it's short, efficient and reliable as a solution
    • The downside is that it has to be manually done, and is not an automatic process.
  • The alternative (which may not seem like much of one) is to use a generic List type instead of arrays
    • The upside is that a list offers a lot more functionality such as near infinite length (upto the limit of available memory), with a lot less hassle.
    • The secondary upside is that a List can easily be converted back to an array by using the builtin instance method .ToArray();
    • The downside to this is more obviously the slight variations posessed by Lists towards arrays, both in syntax & available methods (for example to access the Length of a list you have to use the dot notation with the identifier Size instead of Length).

 1 using System.Collections.Generic;
 2 
 3 public static void Main(String[] Args) {
 4     int[] intArray = new intArray[10];
 5     int incrementValue = 5;
 6 
 7     for (int X = 0; X < 1000; X++) {
 8         if (intArray.Length == X) { 
 9           // If all memoryBlocks in Array full
10             Array.Resize<int>(intArray, // Lengthen Array
11                 intArray.Length + incrementValue);
12         }; intArray[X] = X; // Add to IntArray
13     } // Array.Resize() example;
14 
15     List<int> list = new List<int>();
16 
17     for (int X = 0; X < 1000; X++) {
18         list.Add(X); // Adds to List
19     } // List Based Example
20 }

D3: Removal-Inability

An Array can be used to hold numerous blocks of memory, but because of this they dont offer much in terms of removing values added to an array. There are two ways to remove values added to an array:

  • Set the value of an entry you wish to remove to Null.
    • The upside to this is that this is simple and efficient way to mark a given value in an array as empty (as be default all unassigned values in an array are Null).
  • Alternatively one could just use a List, again :).
    • Lists come with the builtIn Pop method, which can remove any value in a list.


 1 using System.Collections.Generic;
 2 
 3 public static void Output(String[] array) {
 4     Console.WriteLine(String.join(" ", array));
 5 }
 6 
 7 public static void Main(String[] Args) {
 8     String[] sArray = new String[] {"One", "Two", "Three", "Four", "Five"};
 9     List<String> sList = new List<String>("One", "Two", "Three", "Four", "Five");
10 
11     sArray[2] = Null; // Removes "Three"
12     sList.Pop(2); // Removes "Three"
13 
14     Output(sArray); // Outputs "One Two  Four Five" // Notice the gap after Two
15     Output(sList); // Outputs "One Two Four Five" // Notice no gap after Two
16 }