Sie sind auf Seite 1von 38

Java I--Copyright © 2000 Tom Hunter

Chapter 7
Arrays

Java I--Copyright © 2000 Tom Hunter


An Array is A “Data Structure”
• 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.

Java I--Copyright © 2000 Tom Hunter


An Array is A “Data Structure”

• If you wish to create an array where the number


of elements can be changed, you use class
Vector. (Not covered in Java I)

Java I--Copyright © 2000 Tom Hunter


An Array is A “Data Structure”

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

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.

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.

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.

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.

Z q

Java I--Copyright © 2000 Tom Hunter


An Array is A “Data Structure”
x[0] Z
x[1] q
x[2] P
Notice how the first x[3] m
element is the x[4] R
“Zeroth.”
“Z” is the first x[5] t
element, with a x[6] K
subscript of “0”
x[7] T
x[8] A
Java I--Copyright © 2000 Tom Hunter
An Array is A “Data Structure”
• To change an element, you x[0] Z
assign it this way.
x[1] q
X[3] = ‘Y’; x[2] P
• This changes the value of the x[3] m
array location, not the subscript
value. x[4] R
• If your array is declared with x[5] t
a data type of char, then you x[6] K
must use single quotes.
x[7] T
• Double quotes are used for x[8] A
String objects.
Java I--Copyright © 2000 Tom Hunter
An Array is A “Data Structure”

• The compiler will complain


x[0] Z
if you try to assign a double- x[1] q
quoted String to a char array. x[2] P
X[0] = “Z”; x[3] m
“Incompatible type: x[4] R
x[5] t
Can't convert java.lang.String
to char. x[6] K
x[0] = "T"; x[7] T
^
1 error” x[8] A
Java I--Copyright © 2000 Tom Hunter
An Array is A “Data Structure”
• An array is a full-fledge object.
• You can declare an array either of two ways:
char[] x;
or • Either way
works just as well.
char x[];

Java I--Copyright © 2000 Tom Hunter


An Array is A “Data Structure”
• 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.

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
int x = 3;

yMethd( x )
y 3
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

H e l p

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.
Java I--Copyright © 2000 Tom Hunter
out

H e l p

data char[] out = {‘H’,’e’,’l’,’p’};


strMethd( out );

public void strMethd( char[] data )


{
data[3] = { ‘l’ };
}
Java I--Copyright © 2000 Tom Hunter
out

H e l p

data char out = {‘H’,’e’,’l’,’p’};


strMethd( out );

public void strMethd( char[] data )


{
data[3] = { ‘l’ };
}
Java I--Copyright © 2000 Tom Hunter
out

H e l l

data char out = {‘H’,’e’,’l’,’p’};


strMethd( out );

public void strMethd( char[] data )


{
data[3] = { ‘l’ };
}
Java I--Copyright © 2000 Tom Hunter
out

H e l l

data

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
When the
Passing Anmethod’s
Array toargument
a Methodis received,
it• is not recognized as an array element.
If we pass just a single array value, we can’t
Atchange
this point, the method only sees it as a
the original table. The single array value is
single variable
passed by value. that was passed by value.
char[] out;
...
pieceOfArray( out[2] )

public
When wevoid pieceOfArray( char datum )
pass the
{
single element,
datum = ‘W’;
we include the
// won’t change original array
}square brackets.
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.

Java I--Copyright © 2000 Tom Hunter

Das könnte Ihnen auch gefallen