|
|
||
| You are here: | ||
|
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; With arrays we can use a loop: total = 0; 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; That way it can be used in subsequent loops used to process the array: while (pos < SIZE) 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. 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
You should understand the operation of this algorithm before going further. You could try copying and completing the following trace table:
Extension exercises:
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. | ||||||||||||||||||||||||||||||||||||||||||
|
|
|||
|
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. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: July 29, 200823, 2008 |