0% found this document useful (0 votes)
7 views26 pages

LEC 09 Annotated

The document provides an overview of arrays in Object Oriented Programming, explaining that arrays are objects that hold references and can be indexed. It discusses how to create, manipulate, and pass arrays as parameters in methods, as well as the implications of using assignment and equality operators with arrays. Additionally, it covers privacy concerns with array instance variables and introduces multidimensional arrays.

Uploaded by

f20220329
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)
7 views26 pages

LEC 09 Annotated

The document provides an overview of arrays in Object Oriented Programming, explaining that arrays are objects that hold references and can be indexed. It discusses how to create, manipulate, and pass arrays as parameters in methods, as well as the implications of using assignment and equality operators with arrays. Additionally, it covers privacy concerns with array instance variables and introduces multidimensional arrays.

Uploaded by

f20220329
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

BITS Pilani

Pilani Campus

Object Oriented Programming

Arrays
Last Class

• Arrays

BITS Pilani, Pilani Campus


Arrays and References
• Like class types, a variable of an array type
holds a reference
– Arrays are objects stack
- >
-
– A variable of an array type holds the address of
-
-

where the array object is stored in memory heap


-
-

– Array types are (usually) considered to be class


types

BITS Pilani, Pilani Campus


Arrays are Objects
• An array can be viewed as a collection of indexed variables
• An array can also be viewed as a single item whose value is a
collection of values of a base type
– An array variable names the array as a single item
double[] a;
– A new expression creates an array object and stores the
object in memory
new double[10]
– An assignment statement places a reference to the
memory address of an array object in the array variable
a = new double[10];
– The previous steps can be combined into one statement


=> C double[] a = new double[10];
(continued)

i
primiting ne
BITS Pilani, Pilani Campus
Arrays with a Class Base Type
• The base type of an array can be a class type
=>
Date[] holidayList = new Date[20];
objec
non-primeste object datatype
or no . Date

• The above example creates 20 indexed reference


variables of type Date. It does not create 20
objects of the class Date
– Each of these indexed variables are automatically
initialized to null
– Any attempt to reference any them at this point would
result in a "null pointer exception" error message

a (continued)

BITS Pilani, Pilani Campus


Arrays with a Class Base Type
• Like any other object, each of the indexed variables requires a
separate invocation of a constructor using new (singly, or
perhaps using a for loop) to create an object to reference

holidayList[0] = new Date();


. . .
holidayList[19] = new Date();
OR

#
&

for (int i = 0; i < holidayList.length; i++)

-
- -

holidayList[i] = new Date();


heap
• Each of the indexed variables can now be referenced since
each holds the memory address of a Date object

BITS Pilani, Pilani Campus


Array Parameters
• Both array indexed variables and entire arrays
can be used as arguments to methods
-

– An indexed variable can be an argument to a


method in exactly the same way that any variable
of the array base type can be an argument

BITS Pilani, Pilani Campus


Array Parameters
- double n = 0.0;
- double[] a = {2.3, 4.5, 6.7, 8.9};
int i = 2;
- =

Given the method declaration


public void myMethod (double x)
>
-
-
-

then all of the following are legal:


- myMethod(n); double //n evaluates to
=
>
- 0.0
-
myMethod(a[3]);- double//a[3] evaluates to 8.9
myMethod(a[i]); //i evaluates to 2,
T
//a[2] evaluates to 6.7
ith
entry
End enby in the array
BITS Pilani, Pilani Campus
Array Parameters
• An argument to a method may be an entire array
• Array arguments behave like objects of a class
– Therefore, a method can change the values stored in the
indexed variables of an array argument
• A method with an array parameter must specify the
base type of the array only
BaseType[]
– It does not specify the length of the array

BITS Pilani, Pilani Campus


Array Parameters
The following method, doubleElements, specifies an array of
double as its single argument: double array

#
public class SampleClass{
-

public static void doubleElements(double[] a){


- -

int i; -
-

l
for (i = 0; i < a.length; i++)
-

a[i] = a[i] * 2;
. . .
}
. . .
}

(continued)

BITS Pilani, Pilani Campus


Array Parameters
• Arrays of double may be defined as follows:
- double[] ⑨ a = new double[10];
> -
-

- double[] b = new double[30];


- -

• Given the arrays above, the method doubleElements


-

from class SampleClass can be invoked- as follows:


> 10 element
SampleClass.doubleElements(a); array
-

SampleClass.doubleElements(b); 30 element array


=>
– Note that no square brackets are used when an entire
array is given as an argument
– Note also that a method that specifies an array for a
parameter can take an array of any length as an
argument

BITS Pilani, Pilani Campus


= and0
Use ofo == with Arrays
• Because an array variable contains the memory
address of the array it names (it’s a reference), the
assignment operator (=) only copies this memory
address
– It does not copy the values of each indexed variable
– Using the assignment operator b = a; will make two array
variables be different names for the same array
– The memory address in a is now the same as the memory
address in b: They reference the same array
double (10]
double 2) a = new
;
(continued)

sat
a= =
b Twe
ne -

double [D bea;

BITS Pilani, Pilani Campus


Use of = andO
== with Arrays

• A for loop is usually used to make two different arrays


-
-

have the same values in each indexed


double
position:

*
new
(10
double [I0]
;


new

for (int i = 0;(i < a.length) && (i < b.length); i++){


-

b[i] = a[i]; a = b
=
- false
=

}
*

– Note that the above code will not make b an exact copy
of a, unless a and b have the same length

-
w (continued)

BITS Pilani, Pilani Campus


Use of = and O
== with Arrays
• For the same reason, the equality operator (==) only
tests two arrays to see if they are stored in the same
location in memory
– (a == b) does not test two arrays to see if they contain
the same values

– The result of the above boolean expression will be true if


a and b share the same memory address (and, therefore,
reference the same array), and false otherwise

(continued)

BITS Pilani, Pilani Campus


Use of = and == with Arrays

• In the same way that an equals method can


be defined for a class, an equalsArray
-

method can be defined for a type of array


– This is how two arrays must be tested to see if they
contain the same elements

BITS Pilani, Pilani Campus


Use of = and ①
== with Arrays

-
public static boolean equalsArray(int[] a,
int[] b){

if (a.length != b.length) return false;


--
else{
int i = 0;

I
while (i < a.length){

-
if (a[i] != b[i])
return false;
i++;
}
}

}
E
return true;

BITS Pilani, Pilani Campus


Arguments for the Method main
• The heading for the main method of a program has
a parameter for an array of String
– It is usually called args by convention
-
public static void main(String[] args)
-

– Note that since args is a parameter, it could be replaced


by any other non-keyword identifier
• If a Java program is run without giving an argument
to main, then a default empty array of strings is
automatically provided

BITS Pilani, Pilani Campus


Arguments for the Method main

• If a program requires that the main method


be provided an array of strings argument,
each element must be provided from the
command line when the program is run
#
java SomeProgram Hi ! there
– This will set args[0] to "Hi", args[1] to "!",
and args[2] to "there"
String() args
– It will also set args.length to 3
3
-

length
-

"Hi Y =

args [0) E arge


= .

1/ L

(
augs there
a
BITS Pilani, Pilani Campus
Methods That Return an Array
• In Java, a method may also return an array
– The return type is specified in the same way that an array
parameter is specified. This method returns an array of int

&
public static int[] incrementArray(int[] a,
---

int increment){
e
=

int[] temp = new int[a.length];


for (int i = 0; i < a.length; i++)
temp[i] = &

=
a[i] + increment;
return temp;
-

}
main()
our [] b = MovementArray (a
S
,
5)
[0]
=>

b E
BITS Pilani, Pilani Campus
Privacy Leaks with Array Instance Variables
• If an accessor method does return the contents of an
array, special care must be taken
– Just as when an accessor returns a reference to any private
object las PrivateClass &
public double[] getArray(){

portadouble
es
return anArray;
-

}
– The example above will result in a privacy leak.

dolcio
– Why is this so?
Private Class ③ new Private Class()
;
3
-
b .

get Array2)
< (0) = 10
= BITS Pilani, Pilani Campus
Privacy Leaks with Array Instance Variables
• The previous accessor method would simply return a reference
to the array anArray itself

• Instead, an accessor method should return a reference to a deep


copy of the private array object
– Below, a is an array which is an instance variable of the class containing
the getArray method

public double[] getArray(){


> create
-

a
> double[] temp = new double[a.length]; -

new
for (int i = 0; i < a.length; i++)
temp[i] = E
a[i]; array]
}
return temp
E Y
copy
the
original
into new
array
array
BITS Pilani, Pilani Campus
Privacy Leaks with Array Instance Variables
• If a private instance variable is an array that has a class as its
base type, then copies must be made of each class object in
the array when the array is copied. Here b is an array of class
types and an instance variable of the class containing the
getArray method

·
public ClassType[] getArray(){
-

ClassType[] temp = new ClassType[b.length];


&
for (int i = 0; i < b.length; i++)
&
temp[i] = new ClassType(b[i]);
return temp;
-

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];

7
=> int[][][] figure = new int[10][20][30];

-
x
Person[][] = new Person[10][100];
p
=

SDor
-

Bor (0 10) [
to

10 to 100) 100 10

new Person(s
10 X 20x30
3
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


THANK YOU

BITS Pilani, Pilani Campus

You might also like