Arrays are known as a data structure as they can hold more than one value. In order to fully understand arrays it is best to start off with an example.

Name

Test 1

Test 2

Bert

8

3

Fiona

5

7

Sally

7

6

George

7

7

Fred

5

4

We could store these in variables as follows :-

  1. String name1;
  2. int test1a;
  3. int test1b;
  4. Name1 = "Bert";
  5. Test1a = 8;
  6. Test1b = 3;
  7. String name2;
  8. int test2a;
  9. int test2b;
  10. Name1 = "Fiona";
  11. Test1a = 5;
  12. Test1b = 7;

Etc-

This means that by the end we will have 15 separate variables. This is clearly not a good way to store this information. What happens if there was 100 students? We would have 300 variables. By using arrays we can store the above information in just 3 arrays.

  1. String names[] = new String[5];
  2. int test1[5] = new int[5];
  3. int test2[5] = new int[5];
  4. Names[1] = "Bert";
  5. Test1[1] = 8;
  6. Test2[1] = 3;
  7. Names[2] = "Fiona";
  8. Test1[2] = 5;
  9. Test2[2] = 7;

Etc

This is much more efficient. What we have effectively done is created a one dimensional table (or just a single column) to store our data in. We have assigned a name to the array, given it a size and then placed data within it.

Arrays work very well with itteration.

Next define array >>

 
Basics
Menu
Search