BITS Pilani
Pilani Campus
Object Oriented Programming
Arrays and Strings
Last Class
• Arrays
– Arrays are objects
– Array Parameters
• Passing Arrays to methods
– Use of = and ==
– String args[] in main()
– Privacy leaks (avoid it using deep copy)
– Multi-dimensional arrays
BITS Pilani, Pilani Campus
Multidimensional Arrays
• It is sometimes useful to have an array with more
than one index
• Multidimensional arrays are declared and created in
basically the same way as one-dimensional arrays
– You simply use as many square brackets as there are
indices
– Each index must be enclosed in its own brackets
double[][]table = new double[100][10];
- -
int[][][] figure = new int[10][20][30];
-
Person[][] = new Person[10][100];
BITS Pilani, Pilani Campus
Multidimensional Arrays
• Multidimensional arrays may have any number of
indices, but perhaps the most common number is
two
– Two-dimensional array can be visualized as a two-
dimensional display with the first index giving the row, and
the second index giving the column
char[][] a = new char[5][12];
– Note that, like a one-dimensional array, each element of a
multidimensional array is just a variable of the base type
(in this case, char)
BITS Pilani, Pilani Campus
Multidimensional Arrays
• In Java, a two-dimensional array, such as a, is
-
actually an array of arrays
– The array a contains a reference to a one-dimensional
-
array of size 5 with a base type of char[]
– Each indexed variable (a[0], a[1], etc.) contains a
reference to a one-dimensional array of size 12, also with a
base type of char[]
• A three-dimensional array is an array of arrays of
arrays, and so forth for higher dimensions
BITS Pilani, Pilani Campus
Two-Dimensional Array as an
Array of Arrays (Part 1 of 2) char (I b = new char
-↑
Y
-
-
-
- -
- -
-
-
BITS Pilani, Pilani Campus
int [37
Ent 21 now
;
#
stack
-
or 112] @new
Heap
];
·
array of arrays
-
Two-Dimensional Array as an
Array of Arrays (Part 2 of 2)
E
=
2 -
=
I
BITS Pilani, Pilani Campus
Using the length Instance Variable
page- length
30 x 100
=> -
char[][] page = new char[30][100];
• The instance variable #
-
length does not give the total
number of indexed variables in a two-dimensional array
– Because a two-dimensional array is actually an array
of arrays, the instance variable length gives the
number of first indices (or "rows") in the array
•= [Link] is equal to 30 10 XX
– For the same reason, the number of second indices
(or "columns") for a given "row" is given by
referencing length for that "row" variable
&
• page[0].length is equal to 100
-
-
-
-
In element character array
BITS Pilani, Pilani Campus
Using the length Instance Variable
• The following program demonstrates how a nested for
loop can be used to process a two-dimensional array
– Note how each length instance variable is used
int row, column;
-
for (row = 0; row < [Link]; row++)
&
for (column = 0; column < page[row].length;
column++)
page[row][column] = 'Z';
BITS Pilani, Pilani Campus
Multidimensional Array Parameters
and Returned Values
• Methods may have multidimensional array
parameters
– They are specified in a way similar to one-dimensional
arrays
– They use the same number of sets of square brackets as
they have dimensions
public void myMethod(int[][] a)
-
{ . . . }
– The parameter a is a two-dimensional array
BITS Pilani, Pilani Campus
Multidimensional Array Parameters
and Returned Values
• Methods may have a multidimensional array
type as their return type
– They use the same kind of type specification as for
a multidimensional array parameter
public -
double[][] aMethod()
{ . . . } -
– The method aMethod returns an array of
double
BITS Pilani, Pilani Campus
Aways
Ragged
--
ear 23
(3 =
new
int
(3;
T
(2] ;
= new
int
stack Heap -
(17 ; E
-
our
/1J
#
new
~ =
ent [5];
w (2) =
new
STagged
F
in C
Arrays
-
I
-
Away a pointers
use mallos (size) Don
sow
each ,
I size
well vary
for
cat
A Grade Book Class
• As an example of using arrays in a program, a class
>
-GradeBook is used to process quiz scores
• Objects of this class have three instance variables
2D –&&grade: a two-dimensional array that records the grade of
-
-
--
each student on each quiz
-
T
–-
-
[
ID studentAverage: an array used to record the average
- -
- quiz score for each student 400
-
ID –#
quizAverage: an array used to record the average
↓
score for each quiz 3
UG It
year
(quiz] Couder 5x400
#endent]
-
CP =>
5 quizzes
- 2
(quis 70X5
= Yo0
Students
-
-
BITS Pilani, Pilani Campus
A Grade Book Class [students [quis]
=>
• The score that student 1 received on quiz number 3
is recorded in grade[0][2]
-
-
• The average quiz grade for student 2 is recorded in
O
- - -
studentAverage[1]
-
• The average score for quiz 3 is recorded in
&
=
quizAverage[2]
-
• Note the relationship among the three arrays
BITS Pilani, Pilani Campus
The 2-Dimensional Array grade
-
000
&-
BITS Pilani, Pilani Campus
Strings
• Learn about literal strings
• Learn about String constructors
• Learn about commonly used methods
• Understand immutability of strings
• Learn to format numbers into strings
BITS Pilani, Pilani Campus
String class facts
• An object of the String class represents a string of
characters.
-o
-
• The String class belongs to the [Link] package,
which does not require an import statement.
• Like other classes, String has constructors and
methods.
• Unlike other classes, String has two operators,& + and
&
+= (used for concatenation). ↓
BITS Pilani, Pilani Campus
=>
Literal Strings
• are anonymous objects of the String class
-
-
• are defined by enclosing text in double quotes. “This
=
is a literal String”
• don’t have to be constructed.
• can be assigned to String variables.
-
-
-
• can be passed to methods and constructors as
-
-
parameters.
-
• have methods you can call.
-
BITS Pilani, Pilani Campus
Literal String examples
//assign a literal to a String variable
②
String name = “Robert”;
anonymous
& ↑ object bed
anonymous
//calling a method on a literal String X
27
⑳
char firstInitial = “Robert”.charAt(0);
-
=>
First Inficl= R ↑
string method
//calling a method on a String variable
char firstInitial = [Link](0);
BITS Pilani, Pilani Campus
maintains a
-
2
...
X
Robert
"
already exists or ?
not
& whether
came
-
of exists it re used
the very
>
- up ,
object
.
em
new
PB(a = = b)
point (same) ;
· Ros Bere/Calsa
THANK YOU
BITS Pilani, Pilani Campus