0% found this document useful (0 votes)
5 views27 pages

Lec 29 Java Fundamental of Computing

The document covers fundamental concepts of multi-dimensional arrays and characters in computing, specifically in Java. It explains the declaration, creation, and manipulation of arrays, including arrays of objects and arrays of arrays, as well as the Unicode character set and string operations. Additionally, it discusses how to compare strings lexicographically and provides examples of character manipulation.

Uploaded by

abhishekhamida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views27 pages

Lec 29 Java Fundamental of Computing

The document covers fundamental concepts of multi-dimensional arrays and characters in computing, specifically in Java. It explains the declaration, creation, and manipulation of arrays, including arrays of objects and arrays of arrays, as well as the Unicode character set and string operations. Additionally, it discusses how to compare strings lexicographically and provides examples of character manipulation.

Uploaded by

abhishekhamida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ESc101 : Fundamental of Computing

I Semester 2008-09

Lecture 29

• Multi-dimensional arrays
• Characters and Srings

1
Multi-dimensional Arrays

2
Recall the definition of array

Array : An Object which is an ordered collection of data items. These data items
could be

• primitive types.

• references to objects of a class.

3
Array : declaration

Memory

A
int[ ] A ;
?

4
Array : declaration and creation

Memory

A
int[ ] A ;
0 0 0 0

A = new int[4];

Expression of type int

5
What about array of “Objects”

: array whose data items are


references to objects of a class

6
What about array of “Objects”

array whose data items are


references to objects of a class

7
Array of Points

Memory

A
Point[ ] A ; ?

8
Array of Points

Memory

A
Point[ ] A ;
A = new Point[3];
null null null

9
Array of Points

Memory

A
Point[ ] A ;
A = new Point[3];

A [0] = new Point(1,2); null null

Point
x=1
y=2

10
Array of arrays

The arrays whose data items are


references to objects which are arrays.

11
Array of integer arrays : declaration

Memory

A
int[ ] [ ] A ;
?

12
Array of integer arrays

Memory

A
int[ ][ ] A ;
A = new int[3][ ];
null null null

13
Array of integer arrays

Memory

A
int[ ][ ] A ;
A = new int[3][ ];
null null
A [0] = new int[4];

0 0 0 0

14
Array of integer arrays

Memory

A
int[ ][ ] A ;
A = new int[3][ ];

A [0] = new int[4];


A [1] = new int[2]; 0 0 0 0 0

A [2] = new int[5];


0 0

0 0 0 0

15
Array of integer arrays

Memory

A
int[ ][ ] A ;
A = new int[3][ ];

A [0] = new int[4];


A [1] = new int[2]; 67 0 0 0 0

A [2] = new int[5];


0 0
A [2][0] = 67;
0 0 0 0

16
Array of integer arrays with predefined size

Memory

A
int[ ][ ] A ;
A = new int[3][5];

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

17
Matrix : array of “arrays of same length”

1. How to add two matrices ?


(Do it as exercise)

2. How to multiply two matrices ?


(Code available on homepage : matrix mul.java)

18
Characters : ’a’,’%’,’@’,...

1. Unicode defines a character set that can represent all of characters found in
Human languages.

2. Java uses Unicode.


char is an unsigned 16-bit type in the range 0 to 65,535. such that each
character has a unique code in this range

3. unicode of ’a’ is 97.

4. unicode of ’z’ is 122.

19
Characters

• unicode of ’A’ to ’Z’ is from 65–90.


• unicode of ’a’ to ’z’ is from 97–122.
• unicode of ’0’ to ’9’ is 48–57.
Please go through the following simple programme to print characters with
unicode from 0 to 127.
character example2.java

20
Characters can be manipulated like integers

char firstletter = 97;


int i = firstletter;
char secondletter = (char)(firstletter+1);
System.out.println(firstletter);
System.out.println(i);
System.out.println(secondletter);
Output :
a
97
b

21
Characters can be manipulated like integers

char firstletter = 97;


int i = firstletter;
char secondletter = (char)(firstletter+1);
System.out.println(firstletter);
System.out.println(i);
System.out.println(secondletter);
Output :
a
97
b

22
Characters can be manipulated like integers

Please go through the following programme provided on the course website. They
are very useful.
character example1.java

23
String class

1. Its objects are sequence of characters. Example : “abc”, “world”,...

2. Constructors :

• String() :
It creates an empty string

• String(String value) : It ceates a copy of the string referenced by value


In addition : there is a direct way to create a string.

String s = “my name”;

24
Operations available on String objects

Let str be a reference to a string object

• str.length() : length of the string


• str.charAt(i) : the character at position i
• str.indexOf(charc) : returns the first position where charc appears
and -1 if it does not appear.

25
Comparing two String objects

str.compareTo(anotherString)
returns int which is less than or equal to or greater than 0 if the String str is less
than or equal to or greater than string anotherString.

Note that the strings are compared lexicographically (like in dictionary). So

• “Z” is less than “ABC”


• “Y” is less than “YA”;
• “abc” is less than “aabcdef”
What if there are special characters ?

26
Comparing two string obects

General algorithm :
Let s and t are two strings to be compared.

• start scanning s and t from left to right character by character until you find a
mismatch. The order (less than or greater than) between the two strings is
determined by the comparison between the unicode of the two characters.

27

You might also like