Sie sind auf Seite 1von 5

Chapter 6 Single-dimensional Arrays

1. See the section "Declaring and Creating Arrays."


2. You access an array using its index.
3. No memory is allocated when an array is declared. The memory is allocated when
creating the array.
x is 60
The size of numbers is 30
. !ndicate true or "alse "or the "ollowing statements#
1. $%ery element in an array has the same ty&e.
Answer: True
2. The array si'e is "ixed a"ter it is declared.
Answer: (alse
3. The array si'e is "ixed a"ter it is created.
Answer: True
. The element in the array must )e o" &rimiti%e data ty&e.
Answer: (alse
*. +hich o" the "ollowing statements are %alid array declarations,
int i = new int(30);
Answer: !n%alid
double d[] = new double[30];
Answer: -alid
char[] r = new char(1..30);
Answer: !n%alid
int i[] = (3, 4, 3, 2);
Answer: !n%alid
float f[] = {2.3, 4.5, 5.6};
Answer: -alid
char[] c = new char();
Answer: !n%alid
.. The array index ty&e is int and its lowest index is /.
a[2]
0.
1a2
dou)le34 list 5 new dou)le31/46
1)2
list3list.length 7 14 5 *.*6
1c2
System.out.&rintln1list3/4 8 list31426
1d2
dou)le sum 5 /6
"or 1int i 5 /6 i 9 list.length6 i882
sum 85 list3i46
1e2
dou)le min 5 list3/46
"or 1int i 5 16 i 9 list.length6 i882
i" 1min : list3i42 min 5 list3i46
1"2
System.out.&rintln1list31int21;ath.random12 < list.length226
1g2
dou)le34 5 =3.5, 5.5, 4.52, 5.6};
>. A runtime exce&tion occurs.
?. @ine 3# the array declaration is wrong. !t should )e dou)le34. The array needs to )e
created )e"ore its )een used. e.g. new dou)le31/4
@ine *# The semicolon 162 at the end o" the "or loo& heading should )e remo%ed.
@ine *# r.length12 should )e r.length.
@ine .# random should )e random12
@ine .# r1i2 should )e r3i4.
1/. System.arraycopy(source, 0, t, 0, source.length);
11. The second assignment statement my@ist 5 new int32/4 creates a new array and
assigns its re"erence to my@ist.

my@ist new int31/4

Array
my@ist new int31/4

Array
new int32/4

Array
12. False. +hen an array is &assed to a methodA the re"erence %alue o" the array is
&assed. No new array is created. Both argument and &arameter &oint to the same
array.
13.
num)ers is / and num)ers3/4 is 3
1.

1A2 $xecuting
createArray in @ine .
S&ace reCuired "or the
main method
char34 chars# re"

Dea&
Array o" 1//
characters
S&ace reCuired "or the
createArray method
char34 chars# re"

1B2 A"ter exiting
createArray in @ine .
S&ace reCuired "or the
main method
char34 chars# re"

Dea&
Array o" 1//
characters
StacE StacE

1C2 $xecuting
dis&layArray in @ine 1/
S&ace reCuired "or the
main method
char34 chars# re"

Dea&
Array o" 1//
characters
S&ace reCuired "or the
dis&layArray method
char34 chars# re"

1D2 A"ter exiting
dis&layArray in @ine
1/
S&ace reCuired "or the
main method
char34 chars# re"

Dea&
Array o" 1//
characters
StacE StacE

1$2 $xecuting
count@etters in @ine 13
S&ace reCuired "or the
main method
int34 counts# re"
char34 chars# re"

Dea&
Array o" 1//
characters
S&ace reCuired "or the
count@etters method
int34 counts# re"
char34 chars# re"

1(2 A"ter exiting
count@etters in @ine 13
S&ace reCuired "or the
main method
int34 counts# re"
char34 chars# re"

Dea&
Array o" 1//
characters
StacE StacE
Array o" 2.
integers
Array o" 2.
integers

1F2 $xecuting
dis&layCounts in @ine 1>
S&ace reCuired "or the
main method
int34 counts# re"
char34 chars# re"

Dea&
Array o" 1//
characters
S&ace reCuired "or the
dis&layCounts
method
int34 counts# re"

1D2 A"ter exiting
dis&layCounts in @ine 1>
S&ace reCuired "or the
main method
int34 counts# re"
char34 chars# re"

Dea&
Array o" 1//
characters
StacE StacE
Array o" 2.
integers
Array o" 2.
integers
1*. Only one variable-length parameter may be specified in
a method and this parameter mst be the last parameter. !he
method retrn type cannot be a variable-length parameter.
1.. !he last one
printMax(new int[]{1, 2, 3});
is incorrect, becase the array mst of the doble"#
type.
10. Gmitted
1>. Gmitted
1?. Gmitted
20 Simply change (currentMax < list[j]) on Line 10 to
(currentMax > list[j])
21 Simply change list"$# % crrent&lement on Line 9 to
list"$# ' crrent&lement
22. You can sort an array o" any &rimiti%e ty&es exce&t )oolean. The sort method is %oidA
so it does not return a new array.
23. To a&&ly Ha%a.util.Arrays.)inarySearch1arrayA Eey2A the array must )e sorted in
increasing order.
2. @ine 1# list is =2A A 0A 1/I
@ine 2# list is =0A 0A 0A 0I
@ine 3# list is =0A >A >A 0I
@ine # list is =0A >A >A 0I

Das könnte Ihnen auch gefallen