28
ARRAYS & METHODS 1 Array ArrayList Class Method Method Overloading Parameter Passing Parameter Passing Watcharin Puangplia : 5/12/53

arrayand method

Embed Size (px)

DESCRIPTION

arrayand method

Citation preview

ARRAYS & METHODS

1

� Array

� ArrayList Class

� Method

� Method Overloading

Parameter Passing� Parameter Passing

Watcharin Puangplia : 5/12/53

What’s an Array

����������� ������������������������������ ����������

Array ��� ?2

� ����������� ������������������������������ ���������� ��"#��������� �����#�$���

� ����� �� ���"#�%���������้ �%��� Primitive type ��0 Object reference ����

� ��7�8� Java ���้ Array Array ��� ��� ObjectObject (��� instance � � class Array)

� � ���"#����� ���� Array %@��#��$������� � 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

Array ��� ?

index index ��� ��� ArrayArrayFirst index

First index

Last index

Last index

3

ScoresScores 5858 7676 4949 8383 6666 7777 6161

00 11 22 33 44 55 66

Reference name Reference name of Arrayof Array ()*+,-��� ()*+,-��� ArrayArray

index index ��� ��� ArrayArrayFirst index

First index

Last index

Last index

� Array ������ integer 7 �� �����้� ����� � Array = 7� index � � Array �#��G�้�FG 0 A�� 6

of Arrayof Array ()*+,-��� ()*+,-��� ArrayArray

����������� - ��� - ��������� Array

Declaration & Creating & Initialization4

Syntax -*5657-*89+ Array (*)*5;657-*8<= =>�?@A

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

��0 ��0

<type> [] <ref name> = new <type> [<size>];��0 <type> [] <ref name> = {<value 1>..<value n>};

����������� - ��� - ��������� Array

00 11 22 33 44 55 66

5

Declaration & Creating & Initialization

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

ScoresScores 5858 7676 4949 8383 6666 7777 6161

00 11 22 33 44 55 66

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

00 11 22 33 44 55 66

6

� ������ Array G ��#����@�B����� � Array "B����้�� Object � � Array "#�A�������้�"B� Object %@�#����"#�

(public constant) ��#��$� ‘length’ "#������������ � Array� %�� G�. x = scores.length; // x value is 7

ScoresScores 5858 7676 4949 8383 6666 7777 6161

� %�� G�. x = scores.length; // x value is 7� *����GB$� ‘length’ %@����%���$������"�้������0 ����

� � Array ���� ������ index B�"�� (last index)

����� �� �� �������� Array

Reference : -*5�*��,�

00 11 22 33 44 55 66

7

� A������ � Array ������#���"���� N F�$ �������A �� ����0 ���A����G��� "#����� ���� Array �� ���������0� � � Array F�$G���$� index "#�G ���� �� �� g��� index ������$

ScoresScores 5858 7676 4949 8383 6666 7777 6161

00 11 22 33 44 55 66

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 �� Objects8

00 11 22 33 44 55 66

� ������ � Array ����A��� Object ref �������#�$���� ���"#���� Primitive type���8h@�����@��i��

WordsWords AA BB CC DD EE FF GG

� ���8h@�����@��i��String [] words = new String [7];

� %�� Statement G�$ ��� ��������� Object ref � � Array words FG ���� ������ Object � � String

Copying Arrays9

-*5�>=G�- Arrays

� ������้������F���"#������้� �%G �������� � Array ��0 ��������$��� Array �����F��l��������$ �������A"���� ����#้- ��������� � Pointer ��0 Reference (Copy pointer) �$���������0� ����� (=) �o0� ���#������ �� ��� � Object %�� Object ���������� Object 0��Object ���������� Object 0��- ��������� ��� (Copy value) %����� �������� Array ����� ���� ������������� Array 0��

Copying Arrays by copy pointer10

-*5�>=G�- Arrays =HIJ�5�K��L)*I =

listlist11 AA BB CC DD EE FF GG

listlist22 HH II JJ KK LL MM NNBefore

Before

listlist2 2 = list= list11

listlist11 AA BB CC DD EE FF GG

GarbageGarbage HH II JJ KK LL MM NN

After

After

listlist22

Copying Arrays by copy value11

-*5�>=G�- Arrays =HI-*59+ Loop

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

sourcesource 11 33 55 77 99 1111 1313

targettarget

int [] target = new int [source.length];

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

�������G� ��0� ��� �o�����/����

Copying Arrays by copy value12

-*5�>=G�- Arrays =HI-*59+ arraycopy

����� ��"q � arraycopy �# Sysntax ������� ����#้System.arraycopy

sourcesource 11 33 55 77 99 1111 1313

targettarget

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)

13

Copying Arrays by copy value

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

Length of copyOrigin Target

������

Multidimensional Arrays14

Array UVVLG*I),W,

7�8� Java ����A��� Array "#��#����$��������G��� �������o��� ‘[’ F�@ ‘]’ �������FG�@��G�

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

int[][] a2D = new int[2][3];

������� Array 2 ����15

C C 00 C C 11 C C 22

R R 00 11 22 33

R R 11 44 55 66

a2D[0][0] = 1a2D[0][1] = 2a2D[0][2] = 3a2D[1][0] = 4a2D[1][1] = 5a2D[1][2] = 6a2D[1][2] = 6

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

C C 00 C C 11int[][][] a3D = new int[2][3][2];

������� Array 3 ����16

C C 00 C C 11

R R 00 11 22

R R 11 33 44

R R 22 55 66

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

00

11 C C 00 C C 11

R R 00 77 88a2D[0][2][1] = 6 a2D[1][2][1] = 12int[][] a2D = {

{ {1, 2}, {3, 4}, {5, 6} },{ {7, 8}, {9, 10}, {11, 12} }

};

R R 00 77 88

R R 11 99 1010

R R 22 1111 1212

OneOne

17

Multidimensional Arrays

OneOnedimensiondimension

TwoTwo columncolumn

ThreeThreedimensiondimension[[33][][44][][22]]

TwoTwodimensiondimension

row

row

columncolumn

[[33][][44]]

The ArrayList Class18

� ���$������� � package java.util� �#���8h@������� Array �0 ����������� ���"#�������

� ���������� �� F�@���A��� �������� index� FG�G��%�� Array G��"#� ArrayList ��G �������������

������ F�@����������G��G ����� �# Methods G��� "#����G ���"��������� �������#����

18

Declaration & Creating & Initialization

����������� - ��� - ��������� ArrayList19

Syntax �����@��i�� ArrayList ����A��@��i�� ����#้

ArrayList<type> <ref name>;<ref name> = new ArrayList<type>();��0 ArrayList<type> <ref name> = new ArrayList<type>();

����������� - ��� - ��������� ArrayList

Declaration & Creating & Initialization20

Syntax �����@��i�� ArrayList ����A��@��i�� ����#้

ArrayList <type> <ref name> = new ArrayList <type>();

ArrayList <Integer> numList = new ArrayList <Integer>();

numList.add(new Integer(1));numList.add(new Integer(2));…numList.add(new Integer(n));

Method ��� ArrayList

add(object o) : boolean

Method of ArrayList21

add(object o) : booleanadd(int index, object o) : void�o��� Object ���� ArrayListremove(object o) : booleanremove(int index) : object�� Object %�� ArrayList�� Object %�� ArrayListsize() : int�0���%���$�������� ArrayListget(int index) : object�0��� Object G�� index "#��@�B

Method of ArrayList

Method ��� ArrayList22

clear() : void�������"�้������ ArrayListcontains(object o) : boolean�0��� true A� Object "#��@�B�# ���� ArrayListisEmpty() : boolean�0��� true ��0� �� ArrayList ���#�������� ��

Method ��� Array UG7 ArrayList

Method of Array & ArrayList23

Array ArrayList

����� � List array.length arrayList.size()

���A��������� List array[index] 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: value1Remove ArrayList value: value3Contains ArrayList: trueContains ArrayList: false

25

Excemple : Array

���������� ��������������������������������� 10 ��(Array) ��%&� '�(�� )

26

Lab AssignmentProblem 1 : AssignArray1.java

� ����������������������� ��!����" ���#$�%�����&� 10 �� ��'(�&�)�* ��+ �(!,�-./�.0� ��1(�2��" �!���)�* ��+)�* ��+ �(!,�-./�.0� ��1(�2��" �!���)�* ��+

Enter score [1] = ???Enter score [2] = ???Enter score [3] = ???Enter score [4] = ???Enter score [5] = ???Enter score [6] = ???Enter score [7] = ???

27

Enter score [7] = ???Enter score [8] = ???Enter score [9] = ???Enter score [10] = ???

Summary = ???Average = ???

Lab AssignmentProblem 1 : AssignArray1.java

� ����������������������� ��!����" ���#$�%�����&� 10 �� �(!��� ��!����3 �4+�!����3 �4+

Enter score [1] = ???Enter score [2] = ???Enter score [3] = ???Enter score [4] = ???Enter score [5] = ???Enter score [6] = ???Enter score [7] = ???

28

Enter score [7] = ???Enter score [8] = ???Enter score [9] = ???Enter score [10] = ???

Maximum value is = ???