Title: Program Design and Programming
1Arrays in Java
Fundamentals of Programming IT1202
2Agenda
- Arrays
- One dimensional arrays
- Multi dimensional arrays
3Lets take an example.
Write a simple Java program to store the
students marks of a class of 3
4Nature of the given problem
- Store Marks
- Number of students in the class is 3
- One student can gain marks ranging from 0 100
- They can score either 30 or 89.5
5Lets write a Java program to store students marks
class StudentMarks public static void
main(String args) float student1,
student2, student3
6Lets write a Java program to store students marks
class StudentMarks public static void
main(String args) float student1,
student2, student3
7Lets improve the example.
Write a simple Java program to store the
students marks of a class of 100
8Nature of the given problem
- Store Marks
- Number of students in the class is 100
- One student can gain marks ranging from 0 100
- They can score either 30 or 89.5
9How do we tackle this problem..
Are we going to reserve/declare separate memory
locations..?
10Arrays
What are Arrays.
- Arrays are data structures that hold multiple
values of the same type. (Primitive types or
class types) - Each item on the list goes into its own slot
- Each slot in the array is numbered so you can
access the information easily - In Java Arrays are real objects unlike in other
languages like C
ICT
11There are two types of arrays in Java
- One dimensional arrays
- Arrays of arrays(Multi dimensional Arrays)
- In Java there is no multidimensional arrays. By
using array of arrays we can get the same
functionality
12One dimensional Arrays
- One dimensional arrays has only one
index/subscript
13One dimensional Arrays
ICT
14One dimensional Arrays
Lets try to handle the students marks problem
- Name of the array is studentMarks
- Size of the array is 100( Index/subscript)
- Remember index starts from 0
- Marks are stored in the array
- Decimal values are expected
15One dimensional Arrays
Lets map our problem to the visual below
StudentMarks
Elements
34.5
10.4
24.3
67.2
89.6
50.5
45.6
4
1
2
3
5
0
99
Index - (Starts with zero)
16One dimensional Arrays
- Declaration
- Arrays are declared using enclosing square
brackets.
ICT
17One dimensional Arrays
Declaration
ICT
18One dimensional Arrays
Declaration
- we can declare multiple arrays of the same type
in the same line.
int firstArray, secondArray
1.
2.
int firstArray , secondArray
ICT
19One dimensional Arrays
Creating
- Array Objects can be created using two methods.
- Using the new operator
- Directly Initializing the contents
ICT
20One dimensional Arrays
Number of elements
Creating
- Using the new operator
- Then our studentMarks array ?
String firstName new String 5
float studentMarks new String 100
ICT
21One dimensional Arrays
Creating
- When creating an array using the new operator all
the slots/elements in the array are initialize - Zero for numeric arrays
- False for boolean arrays
- \0 character arrays
- Null for objects
ICT
22One dimensional Arrays
Creating
- Directly Initializing the contents
String firstName Kamal, Amal ,
Nimal Saman, Sunil
ICT
23One dimensional Arrays
Storing data after creating with new operator
class studentMarks public static void
main(String args) float
studentMarksnew float100
for(int i0iltstudentMarks.lengthi)
studentMarksii
24One dimensional Arrays
Storing data after creating with new operator
StudentMarks
Elements
5.0
1.0
2.0
3.0
4.0
0.0
99.0
4
1
2
3
5
0
99
Index - (Starts with zero) i
ICT
25One dimensional Arrays
Accessing data
class studentMarks public static void
main(String args) float
studentMarksnew float100
for(int i0iltstudentMarks.lengthi)
studentMarksii for(int
i0iltstudentMarks.lengthi)
System.out.println(studentMarksi)
26One dimensional Arrays
Accessing data
27One dimensional Arrays
Accessing data
- Java Run Time will check to verify that the Array
bounds are not exceeded - Each array object has a property called length
which will yield the size of Array
studentMarks100 will throw an Exception
studnetMarks.length will yield 100 maximum
subscript is always StudnetMarks.length -1
28One dimensional Arrays
Changing Array elements.
- To change an Array Element,
- Just use an assignment statement after the Array
Access Expression
Example Number ? studentMarks756
String ? firstName3 Kamala
29Multi Dimensional Arrays
- In Java Multi Dimensional Arrays are just Array
of Arrays.
Array 1
Array 2
Of type Numbers
Array 3
Array 4
30Multi Dimensional Arrays
Columns
00
Individual Arrays of int
Rows
23
31Multi Dimensional Arrays
- Matrix manipulation can be done using Array of
Arrays
32Multi Dimensional Arrays
- Lets consider a simple problem
- Create the following 3 2 matrix in Java
3 2
33Multi dimensional Arrays
Creating
- Array Objects can be created using two methods.
- Using the new operator
- Directly Initializing the contents
ICT
34Multi Dimensional Arrays
Creating
- Using the new operator
- So our problem
int twoDArrays new int 46
Rows
Columns
int matrix new int 32
35Multi Dimensional Arrays
Creating
- Directly Initializing the contents
int twoDArrays 7,1,2,8,6,9,
8,4,1,6,2,1,
3,9,2,5,8,9, 1,7,3,6,4,3
Note twoDArrays.length ? 4 (Number of
rows) twoDArrays0.length ? 6 (Length of first
row/Array) twoDArrays1.length ? 6
36Multi Dimensional Arrays
Creating
- Directly Initializing the contents our problem
int matrix 7,1,
8,5, 6,3,
Note matrix.length ? 3 (Number of
rows) matrix0.length ? 2 (Length of the first
row/Array) matrix1.length ? 2
37Multi Dimensional Arrays
Creating
class Matrix
public static void main(String args)
int matrix
7,1,
8,5,
6,3,
System.out.println(matrix.length)
System.out.println(matrix0.length)
System.out.println(matrix1
.length)
38Multi Dimensional Arrays
Creating
- Since Java Multi Dimensional Arrays are Array of
Arrays It is possible to create Ragged Arrays
int twoDMatrix 5, 1, 2, 8,
6, 9, 8, 4, 1, 1, 3, 9, 2,
5, 8, 1, 7, 3, 6, 4, 3,7,
Ragged Arrays are Arrays which are having varying
sizes of rows
39Multi Dimensional Arrays
Creating
int twoDMatrix 5, 1, 2, 8, 6,
9, 8, 4, 1, 1, 3, 9, 2, 5,
8, 1, 7, 3, 6, 4, 3,7
twoDMatrix1.length ? 4 (length of second row
Array) twoDMatrix2.length ? 5 (length of
third row Array)
40Multi Dimensional Arrays
Creating
- Alternatively It is possible to declare the same
Ragged Array as follows
int twoDMatrix new int 4
twoDMatrix 0 new int 6 twoDMatrix
1 new int 4 twoDMatrix 2 new
int 5 twoDMatrix 3 new int 6
Size of the second dimension is not specified
41Multi dimensional Arrays
Storing data after creating with new operator
- We have to manage two subscript
- Two for control structures recommended to control
the rows and columns - Outer for controls structure to manage rows
- Inner control structure to manage columns
42Multi dimensional Arrays
Storing data after creating with new operator
class studentMarks public static void
main(String args)
for( Outer to control rows )
for ( Inner to control columns )
43Multi Dimensional Arrays
Storing/accessing data
Columns
Rows
44Multi Dimensional Arrays
00
Rows
Columns
45Multi Dimensional Arrays
01
Rows
Columns
46Multi Dimensional Arrays
02
Rows
Columns
47Multi Dimensional Arrays
03
Rows
Columns
48Multi Dimensional Arrays
04
Rows
Columns
49Multi Dimensional Arrays
05
Rows
Columns
50Multi Dimensional Arrays
Columns
Rows
51Multi Dimensional Arrays
10
Rows
Columns
52Multi Dimensional Arrays
11
Rows
Columns
53Multi Dimensional Arrays
12
Rows
Columns
54Multi Dimensional Arrays
13
Rows
Columns
55Multi Dimensional Arrays
14
Rows
Columns
56Multi Dimensional Arrays
15
Rows
Columns
57Multi Dimensional Arrays ..
- Each element can be accessed
- Each element can be changed
twoDMatrix 00 twoDMatrix 01
twoDMatrix 00 6 twoDMatrix 01 8
58Some useful URLs
- http//www.vovisoft.com/java/JavaLecture/Week02/37
20Declaring20Arrays20in20Java.htm - Declaring array variables
- http//cs.gmu.edu/jdoughty/cs161/week03/java0311.
html - Creating array objects
- http//www.cs.ucsb.edu/mcguire/teaching/10/lectur
es/09/ - Multidimensional arrays
59Past Paper Questions
60 Feb 2004 Question Number 36
Consider the following statements in Java in
connection with arrays.
- (i) An array is an object that consists of a
sequence of numbered elements which have the same
type. - (ii) The elements are indexed beginning with 0
and can reference by their number using subscript
operator . - (iii) Arrays are widely used.
- (iv) The element type of an array can be any one
of the eight primitive types or a reference type.
61 Feb 2004 Question Number 36
Which of the following is/are correct in
connection with the above statements?
- (a) (i) and (iv) only
- (b) (i),(ii) and (iv) only
- (c) (i),(iii) and (iv) only
- (d) (iii) and (iv) only
- (e) All the statements
62 Feb 2004 Question Number 36
Which of the following is/are correct in
connection with the above statements?
- (a) (i) and (iv) only
- (b) (i),(ii) and (iv) only
- (c) (i),(iii) and (iv) only
- (d) (iii) and (iv) only
- (e) All the statements
Answer is e