Transcript

Java I--Copyright © 2000 Tom Hunter

Java I--Copyright © 2000 Tom Hunter

Chapter 7

Arrays

Java I--Copyright © 2000 Tom Hunter

• An Array consists of data items of the same type, all with the same name.

• The size of an Array is “static”.

• Once you have created an “array”, you cannot change the number of elements it holds.

• If you create an array with 42 occurrences, then it will always have 42 elements.

An Array is A “Data Structure”

Java I--Copyright © 2000 Tom Hunter

• If you wish to create an array where the number of elements can be changed, you use class Vector. (Not covered in Java I)

An Array is A “Data Structure”

Java I--Copyright © 2000 Tom Hunter

• In Java, an array is a group of contiguous memory locations that all have the same name and same type.

• This declares that x is an array of char variables.

char[] x;

An Array is A “Data Structure”

Java I--Copyright © 2000 Tom Hunter

An Array is A “Data Structure”

• The statement below defines a reference.

char[] x;• Right now, I don’t know how many of them there’s going to be.

X

Java I--Copyright © 2000 Tom Hunter

An Array is A “Data Structure”

• Allocating the array decides the number of elements.

x = new char[5];• If you remember “x” is a reference, the syntax above is more understandable.

X

Java I--Copyright © 2000 Tom Hunter

When you allocate an array, the elements are automatically initialized.

Primitives:

• numeric primitives are zeroed,

• char primitives are made spaces,

• boolean primitives are made false

References:

For an array of any other type--the

“references” are made null.

Java I--Copyright © 2000 Tom Hunter

An Array is A “Data Structure”

x[0]= ‘Z’;

• This assigns the char ‘Z’ to the first array position.

X

Z

Java I--Copyright © 2000 Tom Hunter

An Array is A “Data Structure”

x[1]= ‘q’;

• This assigns the char ‘q’ to the second array position.

X

Z q

Java I--Copyright © 2000 Tom Hunter

An Array is A “Data Structure”

ZqPmRtKTA

x[0]x[1]x[2]x[3]x[4]x[5]x[6]x[7]x[8]

Notice how the first element is the

“Zeroth.” “Z” is the first element, with a subscript of “0”

Java I--Copyright © 2000 Tom Hunter

An Array is A “Data Structure”

ZqPmRtKTA

x[0]x[1]x[2]x[3]x[4]x[5]x[6]x[7]x[8]

• To change an element, you assign it this way.

X[3] = ‘Y’;

• This changes the value of the array location, not the subscript value.

• If your array is declared with a data type of char, then you must use single quotes.

• Double quotes are used for String objects.

Java I--Copyright © 2000 Tom Hunter

An Array is A “Data Structure”

ZqPmRtKTA

x[0]x[1]x[2]x[3]x[4]x[5]x[6]x[7]x[8]

• The compiler will complain if you try to assign a double-quoted String to a char array.

X[0] = “Z”;

“Incompatible type:

Can't convert java.lang.Stringto char.

x[0] = "T"; ^

1 error”

Java I--Copyright © 2000 Tom Hunter

• An array is a full-fledge object.

• You can declare an array either of two ways:

char[] x;

or

char x[];

An Array is A “Data Structure”

• Either wayworks just as well.

Java I--Copyright © 2000 Tom Hunter

• If you put the brackets on the data type, you can declare multiple references as all being the same type of array:

char[] x, y, z;

• In this case, x, y and z are all declared as char arrays.

An Array is A “Data Structure”

Java I--Copyright © 2000 Tom Hunter

An Array is A “Data Structure”

• To learn the number of elements in the array, you call a property called “length.” For example:

int y = 0;

int[] x;

x = new int[5];

y = x.length;

• Notice, “length” is NOT a method, it is a property.

Java I--Copyright © 2000 Tom Hunter

An Array is A “Data Structure”

• You can use an “Initializer List” enclosed in braces to quickly insert values in your array:

int n[] = { 12, 44, 98, 1, 28 };

• Because this list has 5 values in it, the resulting array will have 5 elements.

Java I--Copyright © 2000 Tom Hunter

An Array is A “Data Structure”

• If we had a char array, it would be like this:

char m[] = { ‘t’, ‘I’, ‘M’, ‘Y’ };

Java I--Copyright © 2000 Tom Hunter

Constant Variables & the “final” Qualifier

• When used in a variable’s declaration, the keyword “final” indicates that the variable can never be changed.

final int ARRAY_SIZE = 10;

Java I--Copyright © 2000 Tom Hunter

Constant Variables & the “final” Qualifier

• If you have chosen to make your variable “final” then you must initialize it in the same statement that declares it--you have no other alternative.

final int ARRAY_SIZE;

ARRAY_SIZE = 10;

Java I--Copyright © 2000 Tom Hunter

Call By Reference/Value

• When we call a method and pass the method a primitive-data-type variable, we are always passing a copy of the original, not the original.

• Thus, if the method changes the variable it receives, only the copy is changed--not the original.

Java I--Copyright © 2000 Tom Hunter

X 3

int x = 3;

x is a primitive variable.If we call a method with x as an argument, we make a copy of x and the original copy is not affected by any changes

we make in the copy.

Java I--Copyright © 2000 Tom Hunter

X 3

y 3

int x = 3;

yMethd( x )

public int yMethd( int y ){

return y++; } If I pass x to a method, a copy y is made and any changes happen to the copy.

Java I--Copyright © 2000 Tom Hunter

out

Now, we have char[] out = {‘H’,’e’,’l’,’p’};If we pass the reference out to a method, we again pass a copy, but the copy points back to the original, and so our

method can change the original.

H e l p

Java I--Copyright © 2000 Tom Hunter

H e l p

out

data

char[] out = {‘H’,’e’,’l’,’p’};strMethd( out );

public void strMethd( char[] data ){ data[3] = { ‘l’ };}

Java I--Copyright © 2000 Tom Hunter

out

data

char out = {‘H’,’e’,’l’,’p’};strMethd( out );

public void strMethd( char[] data ){ data[3] = { ‘l’ };}

H e l p

Java I--Copyright © 2000 Tom Hunter

out

data

char out = {‘H’,’e’,’l’,’p’};strMethd( out );

public void strMethd( char[] data ){ data[3] = { ‘l’ };}

H e l l

Java I--Copyright © 2000 Tom Hunter

out

data

H e l l

Java I--Copyright © 2000 Tom Hunter

Java I--Copyright © 2000 Tom Hunter

Passing An Array to a Method

• As you saw in the previous example, when we wish to pass an array to a method, the array’s signature must be expecting to receive an array.

• Thus, the method was declared:

public void strMethd( char[] data ) {

}

Java I--Copyright © 2000 Tom Hunter

Passing An Array to a Method

• The method was clearly designed to receive a char array, which it names data.

public void strMethd( char[] data ) {

}

• But when we called the method, we only used the bare name of the array we were sending. We didn’t use the square brackets.

strMethd( out );

Java I--Copyright © 2000 Tom Hunter

Passing An Array to a Method

• If we pass an entire array--meaning just the array name without any square brackets--then we are passing the reference and the original array can be changed in the method.

• But, if we pass just one array value (and include the square array brackets) then we are just passing a copy and the original cannot be changed.

Java I--Copyright © 2000 Tom Hunter

Passing An Array to a Method

• If we pass just the naked array name, the original array can be accessed and changed.

char[] out;...

wholeArray( out )

public void wholeArray( char[] data ){

data[2] = ‘L’; // will change original array

}

Java I--Copyright © 2000 Tom Hunter

Passing An Array to a Method

• If we pass just a single array value, we can’t change the original table.’

char[] out;...

pieceOfArray( out[2] )

public void pieceOfArray( char d ){

d = ‘x’; // won’t change original array

}

Java I--Copyright © 2000 Tom Hunter

• If we pass just a single array value, we can’t change the original table. The single array value is passed by value.

char[] out;...

pieceOfArray( out[2] )

public void pieceOfArray( char datum ){

datum = ‘W’; // won’t change original array

}

Passing An Array to a Method

When we pass the single element, we include the

square brackets.

When the method’s argument is received, it is not recognized as an array element. At this point, the method only

sees it as a single variable that was passed by value.

w

Java I--Copyright © 2000 Tom Hunter

public void start(){ char[] out = { 'H', 'e', 'l', 'p' }; display.append( "Before out=" + out[0] + out[1] ); wholeArray( out ); pieceOfArray( out[1] ); display.append( "\nAfter out=" + out[0] + out[1]

+ out[2] + out[3] );}

public void wholeArray( char[] data ){ char change = 'l'; data[3] = change; display.append( "\nIn wholeArray: " + data[0] + data[1]

+ data[2] + data[3] );}

public void pieceOfArray( char datum ){ datum = 'W';}

Java I--Copyright © 2000 Tom Hunter

Passing An Array to a Method

The call using the entire array was able to change the original.

The call using just a single element was not able to change the original.

Java I--Copyright © 2000 Tom Hunter

Double-Subscripted Arrays• This is the syntax for a double-subscripted array

char demo[][]

• Java does not directly support two-dimensional arrays--however, it lets you define an array where each element is an array--thereby achieving the same effect.

top related