Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecturer : Sakuni Sellapperuma. Introduction An array is a container object that holds a fixed number of values of a single type. The length of an array.

Similar presentations


Presentation on theme: "Lecturer : Sakuni Sellapperuma. Introduction An array is a container object that holds a fixed number of values of a single type. The length of an array."— Presentation transcript:

1 Lecturer : Sakuni Sellapperuma

2 Introduction An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. 2Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]

3 Introduction cont… An array can hold either primitive or reference type data. An array has its size that is known as array length. An array knows only its type that it contains. Array type is checked at the compile-time. Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]3

4 Disadvantages An array has fixed size. An array can hold only one type of data (either primitive or reference). Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]4

5 Creating Arrays There are 3 steps when creating an array Declare an Array Construct an Array Initialize an Array Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]5

6 Declare Arrays Declare an array variable by specifying the type of data to be stored, followed by square brackets [] DataType[] variableName; DataType variableName[]; Ex : int marks[]; int[] marks; Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]6

7 Construct Arrays Use ‘new’ keyword when constructing arrays. Mention the data type, and the array size in between square brackets. DataType objName[] = new DataType[Size]; Ex : 1. int[] marks; marks = new int[10]; String[] names = new String[3]; Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]7

8 Initialize Array Java arrays are zero based. By using the index you can access the array. Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]8

9 Initialize Array int marks[] = new int[1]; marks[0] = 100; char chr[] = new char[2]; chr[0] = 65; chr[1] = 'D'; String names[] = new String[3]; names[0] = "Java"; names[1] = "SCJP"; names[2] = "ESOFT“; Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]9

10 Anonymous Array Java can directly create an array and initialize values. int[] numbers = { 2, 3, 4, 5, 6, 7, 8 }; Do not need ‘new’ keyword. Ex: new int[] { 11, 12, 13, 14, 15, 16, 17}; numbers = new int[] { 11, 12, 13, 14, 15, 16, 17 }; int[] anonymousarr = { 11, 12, 13, 14, 15, 16} ; Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]10

11 Copy Array Data The System class has an arraycopy method that you can use to copy data efficiently from one array into another: public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]11

12 Copying Array char[] copyFrom = {'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e'}; char[] copyTo = new char[7]; System.arraycopy(copyFrom, 1, copyTo, 0, 7); Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]12

13 Multi Dimensional Arrays Java, as with most languages, supports multi- dimensional arrays. Ex : 2 D Array 3 D Array In this lesson we discuss about 2-dimensional arrays. But same principles apply for higher dimensions also. Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]13

14 2 Dimensional Arrays 2-D arrays are usually represent a row-column approach on paper. The term "rows" and "columns" are used in computing. Ex : int[][] a2 = new int[10][5]; Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]14

15 Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]15

16 End of Chapter 01 Part 05 Thank You! 16Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]


Download ppt "Lecturer : Sakuni Sellapperuma. Introduction An array is a container object that holds a fixed number of values of a single type. The length of an array."

Similar presentations


Ads by Google