CSCI1402 Introductory Java For Internet Computing Arrays II

Arrays II

-containing Objects

- in Classes and Methods

Arrays can contain primitive Java types or any type of objects.

Arrays can be used

in class definitions as attributes

in method definitions as parameters or return types

Array Revision

-collection of variables all of the same type (primitive types, Strings, objects), usually related in some way

-allows manipulation of data (easy sorting )

-Java arrays are implemented as objects (use of operator "new")

Syntax

elementType arrayName[ ] = new elementType[size];

Array declaration and creation

int[ ] numbers;

numbers = new int[10]; // 0 <= index <= 9

alternatively

a)int[] numbers = new int[10];

//using array initialiser

b)int[] numbers = {12,3,5,6,3,0,66,45,9,10};

general array element

int i;numbers[i];

Operating on arrays

Filling an array elements with values

a) numbers[ 0 ] = 55; //direct assignment

b) using a loop

int i;

for( i=0; i < length; i++) {

numbers[i] = KeyBoardIn.readInt();

System.out.println(numbers[i]); }

Arrays containing Objects

Example 1 (array with instances of Java classes)

An array called shoppingList can contain elements of type String

//array declaration

String[] shoppingList;

For the array shoppingList

a) assign the values "milk", "eggs", "bread", "butter", "cheese" into theelements of the array

b) display the value in the 3rd element of the array

c) find the length of the array

d) assign value "cake" into the last element of the array

Example 2

Array of user defined Objects (e.g. BankAccount objects)

The components of an array may be primitive types or object references. But only the type of values in the array declaration may be stored in the array. The following declarations are for an array of four BankAccount objects (using simple BankAccount class from a previous lecture )

BankAccount[] account;

account = new BankAccount[4] ;

account

/ 0 / 1 / 2 / 3
/ null / null / null / null
  • The array of BankAccountobjects is automatically initialised with null references.
  • The BankAccount objects have not been yet created.
  • To construct each BankAccount object, we need to use the constructor on each component of the array.

account[0] = new BankAccount(101, "Jon" );

account[1] = new BankAccount(102, "Ann" );

account[2] = new BankAccount(103, "Ian" );

account[3] = new BankAccount(104, "Liz" );

The array elements are instances of the BankAccount class and can be used with all its methods.

For example :account[0].deposit(70);

will deposit £70.0 into the Bank Account object stored in the first element of the array.

(See lab Exercise1 :

StudentArray an array with objects of the Student class)

Arrays as Class attributes

Arrays can be used as attributes to hold data in the user defined classes. Add an array which will hold student's marks from 8 modules as another attribute to the class Student.

Example 3: Student class containing an array which holds student's module marks

Several changes are necessary in the class definition:

//attributes

String name;

int age;

int[] marks; // 1) an array attribute is added

// 2) an empty array is created in the constructor

//constructor

public Student2(String aName, int anAge){

name = aName;

age = anAge;

marks=new int[8];

}

Page 1 of 12

CSCI1402 Introductory Java For Internet Computing Arrays II

// 3. methods manipulating the array are developed as necessary

// method setMarks( ) reads the individual module marks from

//the keyboard and stores them in the array one by one

public void setMarks(){

//for each array element

for (int i=0; i<marks.length;i++){

int mark = KeyBoardIn.readInt(); //read mark in

marks[i]= mark; //assign mark to the array

}

}

Other methods can be developed: displayMarks( ) , getTotal( ), getAverage() etc.

4) Method toString( ) can be modified to include the information

about the module marks

Arrays as method parameters and return types

class ArrayUtility

//this class has no attributes

ArrayUtility() : ArrayUtility

initialise(int[] a): void

fill(int[]a,int value):void

display(int[]a): String

sum(int[] a): int

average(int[] a) : int

findSmallest(int[] x): int

findLargest(int[] x) : int

copy(int[]a) : int[]

subarray(int[] a,int left,int right): int[]

areEqual(int[] a,int[] b): boolean

Lab Exercises no 3

CODE IN THE LECTURE (include in the Lab exercises)

Example 3 (array with user defined objects)

An array called students containing objects of type Student

Note:different from class Student defined in Lecture 6

class Student //class documentation

name: String

age : int

Student(String aName, int anAge): Student

getName() : String

getAge() : int

toString(): String

1. Declare an array named students to hold five Student objects

Student[] students = new Student[5];

2. Create Student objects and assign them to the students array

Student s1 = new Student("Jon", 18 );

Student s2 = new Student("Ann", 19 );

Student s3 = new Student("Ian", 18 );

Student s4 = new Student("Liz", 20 );

Student s5 = new Student("Liz", 19 );

students[0] = s1;

students[1] = s2;

students[2] = s3;

students[3] = s4;

students[4] = s5;

Each array element will be treated as an object of class Student:

//Display the age of the 1st student

System.out.println(students[0].getAge( ));

//Display the name of the last student

//Display all students

//Display names of all students

for (int i=0;i<students.length;i++){

String name = students[i].getName( );

System.out.println(" " + name); }

------

A)

Class ArrayUtility (utility class)

//assigns value zero to each element of the array

public static void initialise(int [] a){ ..}

// assigns the specified value to each element of the array

public static void initialise(int [] a, int value){ …}

public static int sum(int[] x){ …….}

public static void doubleValues(int[] x){ …….}

public static void display(int[] x){ …….}

public static void reverse(int[] x){ …….}

public static int findSmallest(int[] x){ …….}

public static int[] subarray(int[] a,int left, int right){ …. }

B)

ALTERNATIVELY USE without static methods:

ArrayUtility util = new ArrayUtility();

util.

C)

OLD IDEA

Class MyArray

doubleValue() in an array method

MyArrays myArrays = new MyArrays();

int[] a = new int[5];

int[] b = new int[5];

//declarations

public void initialise(int [] x){ fill array with zeros …}

public void doubleValues(int[] x){ …….}

public void display(int[] x){ …….}

public int[] copy(int[] x){ …….} ?????? copy(int[] x, int[] y) probably

//usage

myArrays.fill(a, 8);

myArrays.initialise[b];//will initialise to zeros

myArrays.doubleValues(a[]);

myArrays.copy(int[]x, int length);

Page 1 of 12