Google
 
Site navigation: [ Home | Theory | Java | About ]

Arrays

 

 

 

Try not to mix up elements (which give the " address" of the data) with the value which IS the data.

If you live in an apartment block, your mailbox has a number, that's exactly like an element .

 

 

 

 

 

One test is required for every value.

 

 

 

 

 

 

 

 

 

 

 

Arrays are objects , that is why the new keyword is required.

 

 

This seems like a waste of time but it does improve readability of programs and is excellent practice.

 

 

0, 1 and 2 are usually considered acceptable in code - they are not considered magic numbers since they are used so often.

Sticking to the conventions helps other people understand your code.

 

 

 

largest keeps track of the largest found so far. Initially we set it to the first element .

That's why pos is set to 1 (the second element).

Each element is checked in turn to see if it is larger then the current largest. If it is, it becomes the current largest.

At the end of the loop, the largest is in largest .

This Applet implements the algorithm and adds some output statements so you can see what is happening.

The source code is here .

The numbers in the array are:

2, 56, -2, 99, 3, 4

 

 

 

 

 

  On this page:     [ Arrays | using constants | find the largest ]

  Related links:    [ arrays of objects | parallel arrays | 2D arrays | chomp ]

A array is a group of identifiers (primitives or objects), all of the same type , which share a common name.

The array element SCORES(4) has the value 43. Arrays can hold any of the primitive data types or equally well be collections of objects. For example a boolean array of equivalent size could be:

The great advantage of arrays over simple data types is that they can be easily processed inside a loop. For example, if I wanted to count the number of true values in a set of variables called pass1, pass2 etc:

    total = 0;
   if (pass1)
   {
     total = total + 1;
   }
   if (pass2)
   {
     total = total + 1;
   }

   etc

With arrays we can use a loop:

     total = 0;
   pos = 0;
   while (pos < 6)
   {
     if (pass[pos])
     {
       total = total + 1;
     }
   }

Notice that the array elements are accessed using square brackets and that there are 6 elements in the array (0 to 5).

Arrays are declared as follows:

  boolean[] pass = new boolean[6];

Using static final (constant ) identifiers

Usually a constant is used to define the array size:

  static final int SIZE = 6;
   boolean[] pass = new boolean[SIZE];

That way it can be used in subsequent loops used to process the array:

  while (pos < SIZE)
   {
     if (pass[pos])
     {
       total = total + 1;
     }
   }

This practice means that you are less likely to make mistakes in writing code to process loops and that the array size can be altered at only one place in your code. Numbers that appear in the middle of code with no reference are usually referred to as "magic numbers" and are considered highly undesirable.

The convention with constants (or static final identifiers) is to use ALL CAPITAL letters and to separate words with underscores: ARRAY_SIZE, MAX_ELEMENTS.

Back to top

Find the Largest

A simple algorithm involving a search for the largest item in an array could be as follows:

    // algorithm to find and display largest item in array
    int largest = numbers[0];
   int pos = 1;

   while (pos < SIZE)
   {
    if (numbers[pos] > largest)
     {
       largest = numbers[pos];
     }
     pos = pos + 1;
   }
   messages.setText("The largest value is: " + largest);

 

You should understand the operation of this algorithm before going further. You could try copying and completing the following trace table:

pos
largest
numbers[pos] > largest
numbers
0
1
2
3
4
5
1
2
true
2
56
-2
99
3
4
2
56
false
           
                 

Extension exercises:

  • Modify the program to find the smallest value as well as the largest.
  • Modify the program to print the position in the array of the largest value.
  • Modify the program to find the arithmetic mean of the numbers.

Back to top

Related: [ java home | Next: arrays of objects ]

Vectors are similar to arrays but they are able to grow during program execution.

IB wants students to understand the limitations of static data structures so arrays are considered important.

For mastery aspects, ArrayLists and Vector Casses are not acceptable.


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: October 28, 2013