Fundamentals of Software Development CT010-3-1: Introduction To Collections of Data and Associated Operations: Arrays
Fundamentals of Software Development CT010-3-1: Introduction To Collections of Data and Associated Operations: Arrays
Development
CT010-3-1
Introduction to collections of data
and associated operations: Arrays
Prepared by: SZK First Prepared on:30 th July 2005 Last Modified on:14th December 2005
Quality checked by: GTK
Copyright 2005 Asia Pacific University College of Technology and Innovation
Arrays
Learning Outcomes
At the end of this topic, you should be able to:
Define, initialise and access array data types
Create, edit, compile, run and debug simple
procedural - based Java programs using arrays
Arrays
Key Terms
If you have mastered this topic, you should be able to
use the following terms correctly in your assignments
and exams:
Array data types
Arrays
Array Basics
Definition
An array contains a fixed number (called its length) of
variables of identical type.
In Java, it is a specialized kind of object and it must be
declared in 2 steps :
1. Declare the object :
type[] array_name;
Eg.
int[] counts;
double[] scores;
Time[] appointmentTime;
Arrays
Array Basics
2. Allocate the object :
array_name = new type [size];
Eg.
Combination of 2 steps :
type [] array_name = new type [size];
Eg.
CT010-3-1 Fundamentals of Software Development
Array Basics
The bracketed no. is the length (which states how many
variables in each array.
0 1 2 3 4 5 6 7 8 9 } index /
subscript
counts
int variables
Arrays
Array Basics
counts[0]
// the first variable in counts
counts[1]
// the second variable in counts
counts[9]
// the last variable in counts
counts[10]
// error :
ArrayIndexOutOfBounds
Arrays
Array Basics
Example 1 :
Write and run a program that read 10 integers and print
it in reverse order.
class Simple {
public static void main (String[] arg) {
int[] counts = new int[10];
System.out.print("Enter ten numbers: ");
counts[0]=Keyboard.readInt();
counts[1]=Keyboard.readInt();
counts[2]=Keyboard.readInt();
counts[3]=Keyboard.readInt();
counts[4]=Keyboard.readInt();
counts[5]=Keyboard.readInt();
counts[6]=Keyboard.readInt();
counts[7]=Keyboard.readInt();
counts[8]=Keyboard.readInt();
counts[9]=Keyboard.readInt();
Sample program :
Array Basics
Sample program:
class Simple {
Arrays
Array Basics
Array Features To Be Noted:
Array subscripts can be expression, not just constant.
Eg.
counts[1]
// constant array subcript
}
counts[i]
array subcript in expression
counts[i*2]
form
counts[i/2]
Arrays
Array Basics
Arrays can be initialized by giving a list of all their
elements :
int[] primes = { 2,3,5,9,11,13,17,19,23,29 };
However, this can only be used at the time the array is
declared. Thus,
int[] prime;
primes = { 2,3,5,9,11,13,17,19,23,29 };
Arrays
} ILLEGAL
Array Basics
Array Features To Be Noted (contd) :
The above method can also be used to initialized an array of
Strings such as :
final String NAME[] = { ; Sunday; Monday,
Tuesday, Wednesday, Thursday, Friday,
Saturday);
int day=1;
void printName (int day) {
while (day <=7) {
System.out.print( NAME[day] );
day++;
}
}
CT010-3-1 Fundamentals of Software Development
Arrays
Array Basics
Javas array have an important instant variable : length,
which tells the no of elements in the array.
while (day <= NAME.length) {
System.out.print( NAME[day] );
day++;
}
Arrays
0 1
2 3 4 5 6 7 8 9
0 10 20 30 40 50 60 70 80 90
Arrays
Sample program :
class Simple2 {
public static void main (String[] arg) {
int[] counts = new int[10];
System.out.print("Enter ten numbers: ");
for (int i=0; i<10; i++)
counts[i]=Keyboard.readInt();
for (int i=0; i<10; i++)
System.out.print(counts[9-i] + " ");
System.out.println();
}
}
CT010-3-1 Fundamentals of Software Development
Arrays
Sample program :
class ReadAndEcho {
static final int INPUT_MAX = 1000;
public static void main (String[] arg) {
Collection c = new
Collection(INPUT_MAX);
c.readAndEcho();
}
}
CT010-3-1 Fundamentals of Software Development
Arrays
Collection
class
class Collection {
int[] item;
int size = 0;
public Collection (int number) {
item = new int[number];
}
public void readAndEcho () {
// Input up to 'number' numbers
System.out.print("Enter first number: ");
int n = Keyboard.readInt();
while (!Keyboard.eof()) {
// The first size inputs are stored in item[0] .. item[size-1]
item[size] = n;
size++;
System.out.print("Enter next number: ");
n = Keyboard.readInt();
}
System.out.println();
Arrays
Average
Arrays
63
90
83
Gradebook class
class GradeBook {
String[] names;
int[] exam1, exam2;
int size = 0;
public GradeBook(int number) {
names = new String[number];
exam1 = new int[number];
exam2 = new int[number];
}
public void readAndAverage() {
// Read names and exam grades; size counts inputs
while (true) {
System.out.print("Enter name and two exam grades:
");
names[size] = Keyboard.readString();
if (Keyboard.eof()) break;
exam1[size] = Keyboard.readInt();
exam2[size] = Keyboard.readInt();
size++;
}
}
CT010-3-1 Fundamentals of Software Development
System.out.println();
System.out.println("\tName\tAverage");
for (int i=0; i<size; i++) {
System.out.println("\t" + names[i] +
"\t\t" + (exam1[i] + exam2[i])/2);
Arrays
Student class
class Student {
private String name;
private int exam1, exam2;
public Student () {}
public void setName (String s) { name =
s; }
public void setExam1 (int s) { exam1 = s; }
public void setExam2 (int s) { exam2 = s; }
public String getName () { return name; }
public int getExam1 () { return exam1; }
public int getExam2 () { return exam2; }
public int getAvg () { return
(exam1+exam2)/2; }
}
Arrays
Main Program
class ReadAndAvg2 {
static final int INPUT_MAX = 100;
public static void main (String[] arg)
{
GradeBook grades = new
GradeBook(INPUT_MAX);
grades.readAndAverage();
}
}
Gradebook class
Arrays
Arrays
A[i]
12
23
56
34
90
78
B[i]
12
35
91
125
215
293
C[i]
293
281
258
202
168
78
Arrays
D[i]
11
33
-22
56
-12
E[i]
30
37
60
67
F[i]
12
23
56
90
Arrays
Next Lesson
Packages
Introduction to Packages
API Packages
Using import
Sample programs
Arrays