arrayand method

Page 1

1

ARRAYS & METHODS

Array ArrayList Class Method Method Overloading Parameter Passing

Watcharin Puangplia : 5/12/53


What’s an Array 2

Array ?

ďœ’ ďœ‹ ďœ‹ ďœ‹ ďœ‹ ďœ‹ ďœŠ "# ďœ’ ďœ‹ # $ ďœ‹ "# % ŕš‰ % ďœ’ Primitive type 0 Object reference ďœ‹ 7 8 Java ŕš‰ Array Object ( ďœ’ instance class Array)

ďœ‹ "# ďœŠ Array %@ # $ďœŠ Array

Array 0 % $ Array ŕš‰ ďœ‹ A Array A" ďœ‹ @ B "# Array # "# ďœŠ $$ďœŠ index Aďœ‹ Array # ďœŠ "ďœŠ N F ďœ‹$ index Array ďœŠ $%@ # ďœŠ G ŕš‰ FGďœŠ 0 A N-1


What’s an Array 3

Scores Reference name of Array

index Array

0

1

2

3

4

5

6

58

76

49

83

66

77

61

()*+,- Array

Array ďœŠ integer 7 ďœŠ ŕš‰ Array = 7 index Array # ďœŠ G ŕš‰ FGďœŠ 0 A 6

Last index

First index

Array ?


- - Array 4

Declaration & Creating & Initialization Syntax -*5657-*89+ďœ‹ Array (*)*5;657-*8<=ďœ‹ => ?@A <type> [] <ref name>; <ref name> = new <type> [<size>]; 0 <type> [] <ref name> = new <type> [<size>]; 0 <type> [] <ref name> = {<value 1>..<value n>};


- - Array 5

Declaration & Creating & Initialization

Scores

0

1

2

3

4

5

6

58

76

49

83

66

77

61

<type> [] <ref name> = new <type> [<size>]; int

[]

scores

= new

int

[7];

scores[0] = 58; scores[1]=76; ‌, scores[6]= 61; int

[]

scores

= {58,76,49,83,66,77,61};


Length of Array 6

Scores

0

1

2

3

4

5

6

58

76

49

83

66

77

61

ďœ‹ Array Gďœ‹ # @ B Array "B ŕš‰ Object Array "# A ďœ‹ ŕš‰ "B Object %@ # ďœŠ "# (public constant) # $ďœŠ ‘length’ "# ďœŠ Array % G . x = scores.length; // x value is 7 * GB$ďœŠ ‘length’ %@ % $ " ŕš‰ 0

Array ďœŠ ďœŠ ďœŠ index B "ďœ‹ (last index)


Array 7

Reference : -*5 ďœ‹* ,

Scores

0

1

2

3

4

5

6

58

76

49

83

66

77

61

Aďœ‹ Array # ďœŠ "ďœŠ N F ďœ‹$ A ďœ‹ 0 ďœ‹ A ďœŠ GďœŠ "# ďœŠ Array ďœ‹ ďœ‹ 0 Array F ďœ‹$G ďœ‹$ index "# Gďœ‹ ďœ‹ g index ďœŠ $ Gďœ‹ # ďœŠ 0 ‘[’ F @ ‘]’ F @ # ďœŠ ďœŠ @ $ďœŠ 0 A N-1 x = scores[5]; // x ďœŠ 77 scores[5] = 70; // ďœŠ index "# 5 # % 77 ďœ’ 70


Arrays Objects 8

Words

0

1

2

3

4

5

6

A

B

C

D

E

F

G

Array A ďœ’ Object ref ďœ‹ ďœŠ # $

ďœ‹ "# ďœ’ Primitive type 8h@ @ i ďœ‹ String [] words = new String [7]; % Statement G $ ďœŠ ďœ’ ďœ‹ Object ref Array words FGďœŠ ďœŠ ďœŠ ďœ‹ Object String


Copying Arrays 9

-*5 >=G - Arrays

ŕš‰ F "# ďœ‹ ŕš‰ %Gďœ‹ Array 0 ďœŠ$ Array F ďœ‹ ďœ?l ďœŠ $ A " ďœ‹ #ŕš‰ - ďœ‹ Pointer 0 Reference (Copy pointer) ďœ‹$ ďœ‹ 0 (=) o0 # ďœ‹ Object % Object ďœ’ Object 0 - ďœ‹ ďœŠ (Copy value) % ďœŠ Array ďœ‹ Array 0


Copying Arrays by copy pointer 10

Before

-*5 >=G - Arrays =ďœ‹HIJ 5 K L)*I =

list1 list 1

A

B

C

D

E

F

G

list2 list 2

H

I

J

K

L

M

N

After

list2 list 2 = list1 list1 list1 list 1

A

B

C

D

E

F

G

H

I

J

K

L

M

N

list2 list 2 Garbage


Copying Arrays by copy value 11

-*5 >=G - Arrays =ďœ‹HI-*59+ďœ‹ Loop

source

1

3

5

7

9

target int [] source = {1, 3, 5, 7, 9, 11, 13}; int [] target = new int [source.length]; ďœŠ Gďœ‹

0

o ďœŠ / ďœŠ

for (int i= 0; i < source.length; i++) target[i] = source[i];

11

13


Copying Arrays by copy value 12

-*5 >=G - Arrays =ďœ‹HI-*59+ďœ‹ arraycopy

source

1

3

5

7

9

11

13

target ďœ‹ "q arraycopy # Sysntax ďœ‹ #ŕš‰ System.arraycopy (srcArr, start_pos_src, targetArr, start_pos_taget, length) int [] source = {1, 3, 5, 7, 9, 11, 13}; int [] target = new int [source.length]; System.arraycopy (source, 0, target, 0, source.length)


Copying Arrays by copy value 13

int elements [ ] = { 1,2,3,4 }; // original array int hold [ ] = { 10,9,8,7,6,5 }; // new larger array System.arraycopy(elements, 0, hold, 0, elements.length); Origin

Target

Length of copy


Multidimensional Arrays 14

Array UVVLG*I),W, 7 8 Java A ďœ‹ Array "# # $ďœŠ G ďœ‹ o ‘[’ F @ ‘]’ ďœ‹ FGďœŠ @ G

int int int int

[] a = new int [3]; [][] a2D = new int [3][4]; [][][] a3D = new int [3][4][2]; []‌[] aND = new int [n]‌[n];


Array 2 15

int[][] a2D = new int[2][3]; a2D[0][0] = 1 a2D[0][1] = 2 a2D[0][2] = 3 R0 a2D[1][0] = 4 a2D[1][1] = 5 R1 a2D[1][2] = 6

C0

C1

C2

1

2

3

4

5

6

int[][] a2D = { {1, 2, 3}, {4, 5, 6} };


Array 3 16

int[][][] a3D = new int[2][3][2]; a2D[0][0][0] = 1 a2D[1][0][0] = 7 a2D[0][0][1] = 2 a2D[1][0][1] = 8 a2D[0][1][0] = 3 a2D[1][1][0] = 9 0 a2D[0][1][1] = 4 a2D[1][1][1] = 10 1 a2D[0][2][0] = 5 a2D[1][2][0] = 11 a2D[0][2][1] = 6 a2D[1][2][1] = 12 int[][] a2D = { { {1, 2}, {3, 4}, {5, 6} }, { {7, 8}, {9, 10}, {11, 12} } };

C0 C1 R0

1

2

R1

3

4

R2

5

6

C0 C1 R0

7

8

R1

9

10

R2

11

12


Multidimensional Arrays 17

One dimension

row

Two dimension [3][4 ][4]

Three dimension [3][4 ][4][2 ][2]

column


The ArrayList Class 18

ďœ’ ďœŠ$ package java.util # 8h@ ďœ‹ Array 0 ďœ’ ďœ‹ ďœ‹ "# ďœ‹

ďœ‹ ďœ‹ ďœŠ F @ ďœ‹ A ďœ‹ ďœ‹ index FG GďœŠ % Array G "# ArrayList ďœŠGďœ‹ ďœ‹ F @ ďœ‹G Gďœ‹ # Methods GďœŠ "# ďœŠ GďœŠ " ďœ‹ ďœ‹ # ďœ‹

18


- - ArrayList 19

Declaration & Creating & Initialization Syntax @ i ďœ‹ ArrayList A @ i ďœ‹ #ŕš‰ ArrayList<type> <ref name>; <ref name> = new ArrayList<type>(); 0 ArrayList<type> <ref name> = new ArrayList<type>();


- - ArrayList 20

Declaration & Creating & Initialization Syntax @ i ďœ‹ ArrayList A @ i ďœ‹ #ŕš‰ ArrayList <type> <ref name> = new ArrayList <type>(); ArrayList <Integer> numList numList.add(new Integer(1)); numList.add(new Integer(2)); ‌ numList.add(new Integer(n));

= new ArrayList <Integer>();


Method of ArrayList 21

Method ArrayList add(object o) : boolean add(int index, object o) : void o Object ďœ‹ ďœŠ ArrayList remove(object o) : boolean remove(int index) : object Object % ArrayList size() : int 0 ďœŠ % $ ArrayList get(int index) : object 0 ďœŠ Object G index "# @ B


Method of ArrayList 22

Method ArrayList clear() : void " ŕš‰ ArrayList contains(object o) : boolean 0 ďœŠ true Aďœ‹ Object "# @ B # ďœŠ ArrayList isEmpty() : boolean 0 ďœŠ true 0 ArrayList ďœŠ # ďœŠ


Method of Array & ArrayList 23

Method Array UG7 ArrayList Array

List

array.length

ďœ‹ A List array[index]

ArrayList arrayList.size() arrayList.get(index)

o List

array[index] = * arrayList.add(object)

List

array[index] = * arrayList.remove(object)


Excemple : Method of Array & ArrayList

24


Excemple : Method of Array & ArrayList !" -- Example for Get Value from ArrayList -Get ArrayList value: value1 Remove ArrayList value: value3 Contains ArrayList: true Contains ArrayList: false

25


Excemple : Array

10 (Array) %& ' ( )

26


Lab Assignment

Problem 1 : AssignArray1.java

! " #$ % & 10 '( & ) * + (!, -./ .0 1( 2 " ! ) * + Enter score [1] = ??? Enter score [2] = ??? Enter score [3] = ??? Enter score [4] = ??? Enter score [5] = ??? Enter score [6] = ??? Enter score [7] = ??? Enter score [8] = ??? Enter score [9] = ??? Enter score [10] = ??? Summary = ??? Average = ??? 27


Lab Assignment

Problem 1 : AssignArray1.java

! " #$ % & 10 (! ! 3 4+ Enter score [1] = ??? Enter score [2] = ??? Enter score [3] = ??? Enter score [4] = ??? Enter score [5] = ??? Enter score [6] = ??? Enter score [7] = ??? Enter score [8] = ??? Enter score [9] = ??? Enter score [10] = ??? Maximum value is = ??? 28


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.