Sei sulla pagina 1di 3

Notes on JAVA 10.2005.

8: Arrays
Refer to the Array class of Java 1.1 which we are using (i.e. consult the API for the Array class.) [Java 2 contains an Arrays class in the java.util package with lots more useful methods! import java.util.*; IF you use/have Java 2] Reading in 100 or so numbers and storing them in memory using individual variables would be an arduous task. However, if these could be stored in a single structure there would be no problem. Such a structure for elements of a common type is an array. When an array is declared in a program, instead of a single memory allocation being assigned to each individual variable, a whole block of memory locations is assigned to the array identifier. An index (subscript, in square brackets) is added to this common identifier in order to assign unique names to the individual elements of the array, e.g. scores[1], scores[2], etc. An array data structure is required when a large number of data elements need to be stored in primary memory at the same time. An ARRAY is an ordered collection of a finite number of elements of the same type. The name of the array refers to the whole collection of elements as a unit. Elements of an array are referred to by the arrays name and their index, i.e. their relative positions in the array (in square brackets). This naming convention is the reason why an array is such a useful data structure. It enables the programmer to use a loop control structure (usually the FOR loop) in an algorithm to process the elements in an array. Using an index beyond the length of an array, i.e. trying to access something outside the array, will cause an exception (error!). The square brackets [] are used when declaring an array or when accessing individual elements within the array. The elements of an array may be a primitive data type, i.e. an array of double elements; another array, in which case the whole array is multi-dimensional; an object. Each element of an array can be accessed by its index in the array. The index is always an integer, the first element has the index 0 and the last element the index (arrayName.length 1), e.g. names [3] = Joe; stores the name Joe in the 4th element of the names array. To visit each element of an array in turn, a loop (usually the FOR-loop) is used, each element being arrayName [i], e.g. to display all the names in the names2 array (see below): for (int i = 0; i < names2.length; i++) System.out.println (names2 [i]); or to increase all the marks in a marks array (of integers) by 10 marks: for (int i = 0; i < marks.length; i++) marks [i] += 10; Once an array has been created (by either method see below), its number of elements is finite (fixed)! It cannot at a later stage be increased or decreased, but the data stored in it can be changed. (And, of course, you can leave certain fields empty i.e. containing the default value: 0 for numerical data, empty string for strings, etc.) The first names array below is created with 10 empty elements of type String which have to be filled in due course (some elements may remain empty!). The second names2 array below has been created with 7 (no need to specify, the 7 elements listed do the job) elements of type String and the data is automatically filled with the data given in curling brackets (no empty elements). Technically, an array is an object (surprise, surprise!!). Objects (as opposed to variables or primitive data types) are passed call-by-reference to methods (this means that a method can modify an object passed to it as a parameter). By contrast, simple variables are passed call-by-value to methods a method cannot modify a simple variable passed to it as a parameter.

Notes on JAVA: Arrays

Page 2 of 3

Since an array is an object, it is passed call-by-reference to a method, which can therefor modify the array.

SYNTAX:
Constructor String [] names; OR String names []; names = new String [10]; OR the two stages can be combined: String [] names = new String [10]; OR creates a new array called names of 10 items of type String, each item still being empty. The type of the elements, the array name (and the fact that it is an array) is declared. Memory for each element in the array is allocated.

String [] names2 = {Pete, Josh, Ron, Joe, Larry, Carl, Sid}; creates a new array called names2, each item immediately being filled with one of the names. names [2] = Stdin.readLine (); reads a string from keyboard and stores it in the array (in 3rd position!).

Input

When all elements in the array have to be filled: int [] numbers = new int [20]; for (int i = 0; i < numbers.length; i++) { System.out.print ("Please enter a number: "); numbers [i] = Stdin.readInt (); } OR, if you want the array to be filled with random integers between 1 and 100: for (int i = 0; i < numbers.length; i++) numbers [i] = (int) (Math.random () * 100) + 1;

Output

System.out.println (names [2]); prints string (the 3rd element) of names to screen

Example: System.out.println (names2 [2]); will print Ron to the screen. To print the contents of a whole array to screen: for (int i = 0; i < numbers.length; i++) System.out.print ("Element " + i + " = " + numbers [i]);

The length attribute

int len = names.length;

returns an integer NB: This is NOT a method, NO () when you call it!

gives an integer representing the number of elements in the array. Example: int len = names2.length; will return a 7.

Conversion of String to Character Array

char [] a = s.toCharArray ();

returns an array of characters

so that the characters can be accessed individually by means of the index, e.g. to print them backwards, one below the other, etc. Alternately the s.charAt (i) method can be used.

Notes on JAVA: Arrays

Page 3 of 3

EXERCISES:
1. Write a program reading in 30 test scores (percentages) for a class and then offering a menu to do the following: 1. print out the whole array 2. reverse the order of the elements 3. sort the elements from smallest to largest 4. print the average of all the scores in the array 5. increase/decrease all elements in the array by a certain score or certain percentage (making sure no score goes over 100 or below 0) 4. print out only those scores greater than a certain percentage, i.e. 50% 5. print out only a certain number (i.e. ten or so) of the highest/lowest scores. Using 2.1 two arrays and 2.2 only one array, cycle all the elements of the array one place to the left. For example, if the array is as follows: 1 2 3 4 5 6 7 8 Index Array 2 4 3 7 10 5 17 8 After moving all elements to the left, the array should look as follows: 1 2 3 4 5 6 7 8 Index Array 4 3 7 10 5 17 8 2

2.

Good luck, enjoy it!


_________________________________________________

mb

Potrebbero piacerti anche