Вы находитесь на странице: 1из 22

Java

9.
20 2003
:
( Sun ) <vyazovick@itc.mipt.ru>
( Sun ) < gene@itc.mipt.ru>
Copyright 2003 Sun , , .

Java.
,
.
Java ,
, .
. ,
Java ( ) .
,
.
Java,
,
.

Rendered by www.RenderX.com

ii

9. ..............................................................................................................1
1. .......................................................................................................................... 1
2. , Java.................................................................................... 1
2.1. ........................................................................................... 2
2.2. ...................................................................................... 4
2.3. .......................................................................................... 6
2.4. .........................................................................................................7
3. ............................................................................9
3.1. ArrayStoreException................................................................................ 10
3.2. , ........................................................... 11
4. ................................................................................................................ 12
4.1. ...................................................................................... 15
5. .................................................................................................................... 16
6. ...................................................................................................17

Java

Rendered by www.RenderX.com

. 1 21

9.
.
1. ......................................................................................................................... 1
2. , Java....................................................................................1
2.1. ................................................................................................. 2
2.2. ............................................................................................ 4
2.3. ................................................................................................ 6
2.4. ...............................................................................................................7

3. ........................................................................... 9
3.1. ArrayStoreException...................................................................................... 10
3.2. , .................................................................11

4. ................................................................................................................12
4.1. ............................................................................................ 15

5. ................................................................................................................... 16
6. .................................................................................................. 17

1.
. Java
, ,
. .
Java, , .

2. , Java
, , (arrays)
.
, - .
, , .
, .
n, ,
0 n-1. , ,
. (,
100 ), (,
100 Car).
Java

Rendered by www.RenderX.com

. 2 21

, Java

, Java char[] String


.
, .
, Java ( Java
, ), Object,
-.
. ,
.
,
- , , ..
.

2.1.
,
int
int a[];

, . ,
, .
:
int[] a;

.
:
int[] a[];

a " , int".
:
Point p, p1[], p2[][];

.
,
null ( ; ,
). ,
new,
.
int a[]=new int[5];
Point[] p = new Point[10];

, .
, ,
. ,
Java

Rendered by www.RenderX.com

. 3 21

, ,
.
int array[]=new int[5];
for (int i=0; i<5; i++) {
array[i]=i*i;
}
for (int j=0; j<6; j++) {
System.out.println(j+"*"+j+"="+array[j]);
}

:
0*0=0
1*1=1
2*2=4
3*3=9
4*4=16

,
. ,
, , ..
, , , :
int i[]=new int[5];
i[-2]=0; // !

.
,
, . ,
:
int i[]=new int[5];
...
i=new int[7]; // ,

,
.
.
,
length, . :
Point p[]=new Point[5];
for (int i=0; i<p.length; i++) {
p[i]=new Point(i, i);
}

Java

Rendered by www.RenderX.com

. 4 21

, Java

int.
byte, short char, int.
long .
, length int,
2^31-1, 2 .
, ,
Java, :

. null
, .

. null
-.

,
Object , , Object. ,
Object o = new int[4];

, Object,
:
Object arr[] = new Object[3];
arr[0]=new Object();
arr[1]=null;
arr[2]=arr; // !

2.2.
, , , ,
.
,
, 0.
boolean,
false.
for.
. ,
Point. new
Point, .
null.
:
Point p[]=new Point[5];
for (int i=0; i<p.length; i++) {
System.out.pritnln(p[i]);
}

null.
Java

Rendered by www.RenderX.com

. 5 21

, , .
, n n ,
( p[i])
.
, - .
new , ,
. ,
:
int i[]={1, 3, 5};
int j[]={}; // new int[0]

, .
,
.
, :
Point p=new Point(1,3);
Point arr[]={p, new Point(2,2), null, p};
//
String sarr[]={"aaa", "bbb", "cde"+"xyz"};

,
, , ,
.
:
public class Parent {
private String[] values;
protected Parent(String[] s) {
values=s;
}
}
public class Child extends Parent {
public Child(String firstName, String lastName) {
super(???); //
}
}

Child
. null,
.
new String[2], firstName lastName
. {firstName, lastName} ,
.
Java

Rendered by www.RenderX.com

. 6 21

, Java

:
new String[]{firstName, lastName}

, new
. .

2.3.
. ,
:
int i[][]=new int[3][5];

i ,
35. 15 ,
(0, 0) (2, 4).
:
int pithagor_table[][]=new int[5][5];
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++) {
pithagor_table[i][j]=i*j;
System.out.print(pithagor_table[i][j]+ "\t");
}
System.out.println();
}

:
0
0
0
0
0

0
1
2
3
4

0
2
4
6
8

0
3
6
9
12

0
4
8
12
16

, .
, Java , ,
, , . ,
int[] " ", int[][] " ".
.
x, ,
x (, x[0][0]),
. x
, ,
.
, -
. ,
null.
Java

Rendered by www.RenderX.com

. 7 21

int x[][]=new int[3][5]; //


x[0]=new int[7];
x[1]=new int[0];
x[2]=null;

, x
. ,
null.
, new int[3][5].
: (1 ) 3 ,
5 (3 ). , 4 .
3 ( )
.
:
int x[][]=new int[3][];

- , null.
, , x.length
3 - . x[i].length
, i
x.length, x[i] null.
.
new
, .
,
. ,
null,
. ,
.
, .
, :
int i[][] = {{1,2}, null, {3}, {}};

4 . , -, 4, , 3 2, 1, 0 .
,
, .

2.4.
,
, .
- . ,
Java

Rendered by www.RenderX.com

. 8 21

, Java

,
, .
,
Class,
.
,
Element.
,
.
, . .., Element public-,
public. public.
final,
.
, , ..
,
.
.
Object. , .
Cloneable Serializable.
, .
public final length int.
clone() Cloneable.
:
[public] class A implements Cloneable, java.io.Serializable {
public final int length; //
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e.getMessage());
}
}
}

, , , ,
, Object, , toString(), hashCode()
.
:
// toString()
System.out.println(new int[3]);
System.out.println(new int[3][5]);
System.out.println(new String[2]);

Java

Rendered by www.RenderX.com

. 9 21

// hashCode()
System.out.println(new float[2].hashCode());

:
[I@26b249
[[I@82f0db
[Ljava.lang.String;@92d342
7051261

3.
, Java, ,
.
(
) .
, -
.
,
.
Object Cloneable Serializable.
3 , ,
. ,
. , ,
, ,
, .
, ,
, ,
.
. ,
, Child, ,
Parent.
Child c[] = new Child[3];
Parent p[] = c;

, : , A,
, B, A B.
// :
B b = (B) new A();
// :
B b[]=(B[]) new A[3];

Java

Rendered by www.RenderX.com

. 10 21

.
, Child[][] Parent[][],
(Child[] Parent[]) ( Child Parent
).
, ( ),
- .
, .
, ,
. ,
. ,
. , :
//
byte b[]={1, 2, 3};
int i[]=b;

, b[0] i[0] .

. ,
.
.
new.
, , .
,
, System.arrayCopy(),
.

3.1. ArrayStoreException
, ,
, .
:
Child c[] = new Child[5];
Parent p[]=c;
p[0]=new Parent();

.
. Parent
.
. ,
, .
"", .
, Child
Parent, .

Java

Rendered by www.RenderX.com

. 11 21

, ,
:
c[0].onlyChildMethod();

onlyChildMethod() Child.
, , c[0] ,
Child.
, ,

. , ,
,
. , , ArrayStoreException.
, -
?
. :
public void process(Parent[] p) {
if (p!=null && p.length>0) {
p[0]=new Parent();
}
}

,
if-.
:
process(new Child[3]));

ArrayStoreException.

3.2. ,
, ,
, .
, , , .

, null.
:

null;

, ;

, ,
.

Java

Rendered by www.RenderX.com

. 12 21


.
Object.
, , .
:

null

null

,
: -
A,
B , B
A.

Object

null
,

4.
, ,
, ,
. , , , x,
, x.clone(),
x != x.clone()

,
x.clone().getClass() == x.getClass()

, ,
x.equals(x.clone())

. clone()
, :

, , ,
(, private);

, ,
, ,
, ;

,
. - ,
;

Java

Rendered by www.RenderX.com

. 13 21

, ,
.

.
Object clone(). :
protected native Object clone() throws CloneNotSupportedException;

. .
-,
, ,
. ,
, ,
, .
clone()
Object. , native, ,
. ,
JVM, .
clone() ,
.
Object.clone(),
Cloneable. ,
, .
,
CloneNotSupportedException
.
Cloneable ,
, .
, .
, , , ,
. .
, Object Cloneable,
new Object().clone() .
clone() ,
super.clone().
:

public;

CloneNotSupportedException;

, Cloneable , ,
.
, ,
. :
Java

Rendered by www.RenderX.com

. 14 21

public class Test implements Cloneable {


Point p;
int height;
public Test(int x, int y, int z) {
p=new Point(x, y);
height=z;
}
public static void main(String s[]) {
Test t1=new Test(1, 2, 3), t2;
try {
t2=(Test) t1.clone();
} catch (CloneNotSupportedException e) {}
t1.p.x=-1;
t1.height=-1;
System.out.println("t2.p.x=" + t2.p.x + ", t2.height=" + t2.height);
}
}

:
t2.p.x=-1, t2.height=3

,
. .
,
Point. , ,
.
, clone() Test.
public Object clone() {
Test clone=null;
try {
clone=(Test) super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e.getMessage());
}
clone.p=(Point)clone.p.clone();
return clone;
}

, Object.clone()
Test, , ,
.
Object, .

Java

Rendered by www.RenderX.com

. 15 21

main :
public static void main(String s[]) {
Test t1=new Test(1, 2, 3);
Test t2=(Test) t1.clone();
t1.p.x=-1;
t1.height=-1;
System.out.println("t2.p.x=" + t2.p.x + ", t2.height=" + t2.height);
}

:
t2.p.x=1, t2.height=3

, .
"" Object.clone() ,

,
, .
, ,
Cloneable, . ,
.

4.1.
, .
, - , Object.clone() .
:
int a[]={1, 2, 3};
int b[]=(int[])a.clone();
a[0]=0;
System.out.println(b[0]);

, ,
, . :
int a[][]={{1, 2}, {3}};
int b[][]=(int[][]) a.clone();
if (...) {
// :
a[0]=new int[]{0};
System.out.println(b[0][0]);
} else {
// :
a[0][0]=0;

Java

Rendered by www.RenderX.com

. 16 21

System.out.println(b[0][0]);
}

, . ,
, 2 , 3 . ,
,
.
.
, ,
. 1.
,
. 0.
, if-else, ,
, , 1,
,
.
, Java ,
, .
,
.

5.
Java.
, .
, Java . ,
, , ,
, 0 .
, . ,
,
.
new, ,
, .
, .
(0, false, null).
Java . , ,
, .
.
, .
,
. .
, Object . ,
, .
.

Java

Rendered by www.RenderX.com

. 17 21

ArrayStoreException,
.

.
, , Java
, ,
Cloneable.
,
.

6.
9-1.

?
int x[], y[][];
byte[] a, b[][];
String s, s1[], s2={{}, {a, b}, null};

a.) :

9-2.

x int[], y int[][]. ,
.

a byte[], b byte[][][].
, .

s , s1 String[], s2 String[][].
s2, 3.
0, 2,
- null.

, , ,
?
int b[]=new int[5];
for (int i=1; i<=b.length(); i++) {
b[i]=Math.sqrt(i);
}

a.) , . -,
length(), length. -, 3-
double int
Math.sqrt .
,
.
,
5,

Java

Rendered by www.RenderX.com

. 18 21


, 4.
, , 1,
0.
9-3.

? ? ,
?
a.) . ,
,
.

9-4.

, ,
?
int x[][]=new int[2][3];

a.) :
new int[][]{{0, 0, 0}, {0, 0, 0}}

9-5.

? , ?
byte b[]={1, 2, 3};
Object o=b;
o=new String[]{, a, b};
String s[]=o;

a.) . 4 Object
String[]. :
String s[]=(String[])o;

9-6.

new int[3][4]? new


int[3][][]?
a.) 3 4
, 4 .
1 ,
null, 1 .

9-7.

? ?

?
a.) java.lang.Object.
2 java.lang.Cloneable java.io.Serializable.

Java

Rendered by www.RenderX.com

. 19 21

public final int length


public Object clone().
9-8.

, ?
a.) -,
.
-, , A ( , A[])
B[], , A B.
, A B ,
(
).

9-9.

,
int[][] byte[][] ,
.
?
a.) . ,
int[] byte[],
.

9-10. ArrayStoreException ?
public void setCars(Car c[]) {
c[0]=new Car();
}
public void setCars2(Car c[]) {
if (c[0] instanceof Car) {
c[0]=new Car();
}
}
public void setNumbers(int x[]) {
x[0]=0;
}

a.) setCars setCars2,


, - Car.
, , instanceof
true.
,
, .
9-11. ?
public class Point {
private int x, y;

Java

Rendered by www.RenderX.com

. 20 21

public Point(int nx, int ny) {


x=nx;
y=ny;
}
public Object clone() {
return new Point(x, y);
}
}

a.) , clone .
, Cloneable,
Object.clone(),
.
9-12.
JVM?
a.) .
9-13. ?
Point p1[][]={null, {new Point(1, 1)}};
Point p2[][] = (Point[][])p1.clone();
p2[0]= new Point[]{new Point(2, 2)};
System.out.println(p1[0][0]);

a.) ,
p2,
(p1) null. ,
p1[0][0] (NullPointerException).

Java

Rendered by www.RenderX.com