Program Design and Programming - PowerPoint PPT Presentation

1 / 62
About This Presentation
Title:

Program Design and Programming

Description:

... possible to create Ragged Arrays. Ragged Arrays are Arrays ... Alternatively It is possible to declare the same Ragged Array as follows: Size of the second ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 63
Provided by: niyomal
Category:

less

Transcript and Presenter's Notes

Title: Program Design and Programming


1
Arrays in Java
Fundamentals of Programming IT1202
2
Agenda
  • Arrays
  • One dimensional arrays
  • Multi dimensional arrays

3
Lets take an example.
Write a simple Java program to store the
students marks of a class of 3
4
Nature 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

5
Lets write a Java program to store students marks
class StudentMarks public static void
main(String args) float student1,
student2, student3
6
Lets write a Java program to store students marks
class StudentMarks public static void
main(String args) float student1,
student2, student3
7
Lets improve the example.
Write a simple Java program to store the
students marks of a class of 100
8
Nature 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

9
How do we tackle this problem..
Are we going to reserve/declare separate memory
locations..?
10
Arrays
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
11
There 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

12
One dimensional Arrays
  • One dimensional arrays has only one
    index/subscript

13
One dimensional Arrays
ICT
14
One 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

15
One 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)
16
One dimensional Arrays
  • Declaration
  • Arrays are declared using enclosing square
    brackets.

ICT
17
One dimensional Arrays
Declaration
ICT
18
One 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
19
One dimensional Arrays
Creating
  • Array Objects can be created using two methods.
  • Using the new operator
  • Directly Initializing the contents

ICT
20
One 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
21
One 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
22
One dimensional Arrays
Creating
  • Directly Initializing the contents

String firstName Kamal, Amal ,
Nimal Saman, Sunil
ICT
23
One 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
24
One 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
25
One 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)

26
One dimensional Arrays
Accessing data
27
One 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
28
One 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
29
Multi Dimensional Arrays
  • In Java Multi Dimensional Arrays are just Array
    of Arrays.

Array 1
Array 2
Of type Numbers
Array 3
Array 4
30
Multi Dimensional Arrays
Columns
00
Individual Arrays of int
Rows
23
31
Multi Dimensional Arrays
  • Matrix manipulation can be done using Array of
    Arrays

32
Multi Dimensional Arrays
  • Lets consider a simple problem
  • Create the following 3 2 matrix in Java

3 2
33
Multi dimensional Arrays
Creating
  • Array Objects can be created using two methods.
  • Using the new operator
  • Directly Initializing the contents

ICT
34
Multi Dimensional Arrays
Creating
  • Using the new operator
  • So our problem

int twoDArrays new int 46
Rows
Columns
int matrix new int 32
35
Multi 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
36
Multi 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
37
Multi 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)
38
Multi 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
39
Multi 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)
40
Multi 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
41
Multi 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

42
Multi 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 )

43
Multi Dimensional Arrays
Storing/accessing data
Columns
Rows
44
Multi Dimensional Arrays
00
Rows
Columns
45
Multi Dimensional Arrays
01
Rows
Columns
46
Multi Dimensional Arrays
02
Rows
Columns
47
Multi Dimensional Arrays
03
Rows
Columns
48
Multi Dimensional Arrays
04
Rows
Columns
49
Multi Dimensional Arrays
05
Rows
Columns
50
Multi Dimensional Arrays
Columns
Rows
51
Multi Dimensional Arrays
10
Rows
Columns
52
Multi Dimensional Arrays
11
Rows
Columns
53
Multi Dimensional Arrays
12
Rows
Columns
54
Multi Dimensional Arrays
13
Rows
Columns
55
Multi Dimensional Arrays
14
Rows
Columns
56
Multi Dimensional Arrays
15
Rows
Columns
57
Multi Dimensional Arrays ..
  • Each element can be accessed
  • Each element can be changed

twoDMatrix 00 twoDMatrix 01
twoDMatrix 00 6 twoDMatrix 01 8
58
Some 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

59
Past 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
Write a Comment
User Comments (0)
About PowerShow.com