hts.StevenWood.com

Array Concepts

Home ] Up ][ Last Page ]

Array Concepts

Arrays :

  • A table of values of the same type and a fixed size.
  • Arrays are Java objects
  • The table may have one or more dimensions.All Java arrays are technically one-dimensional. Two-dimensional arrays are arrays of arrays.
  • Declaring an array does not create an array object or allocate space in memory; it creates a variable with a reference to an array
  • Array variable declarations must indicate a dimension by using []

Elements :

The data within the array.

Index :

The position of a component in an array. The index must be of an ordinal type.

 

Example:

Given the array declaration in Java

 

 

 

// declare BUT no space allocated (no numbers allowed yet)
int[] marks;         // OR this:    int marks[]
// allocate space and allow numbers
marks[] = new int[10];  
// can be done in ONE line:
int[] marks = new int[10];
// may be able to add values in one step with initalizer list:
int marks[ ] = { 72, 44, 66, 87, 54, 85, 92, 65, 71, 63 };

If we were to read into the array the following data:

 

marks

Index ->

0

1

2

3

4

5

6

7

8

9

Element ->

72

44

66

87

54

85

92

65

71

63

The elements are the marks themselves.

You may wish to conceptualize an array as a series of boxes with each box holding one number:

. . . . . .

The elements can be accessed with      marks[1] or marks[5].

The indices are 0,2,3...9

Therefore,       marks[2] has a value of 66

                    marks[6] has a value of 92

 

Note: all arrays know how many elements (or boxes) they contain. This can be found by the property named length.

e.g. marks.length                             //  counts # of boxes

System.out.println( marks.length );     // would display 10

Manipulating Values in Arrays

Assuming the following declaration:

Suppose the user enters each value into the array marks using the following:

We could find the average with the code:

We could print out the marks with the code:


Source: Public and Catholic District School Board Writing Partnership Course Profile: Computer and Information Science, Grade 11, University/College Preparation ICS3U, Queen’s Printer for Ontario, 2001, adapted.

Home ] Up ][Last Page]
All material on this site is copyright © 1997- by Steven Wood or as credited. All right reserved. Use of this site indicates you agreement with the  terms of use.
Send comments or questions to about this page.
Every attempt has been made to credit work under copyright. If there are any claims that copyright has been missed please contact the .
+SDG+