![Page 1: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/1.jpg)
Java ArraysLecturer : Sakuni Sellapperuma
![Page 2: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/2.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
2
IntroductionAn 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.
![Page 3: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/3.jpg)
Sakuni 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.
![Page 4: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/4.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
4
Disadvantages
An array has fixed size.
An array can hold only one type of data (either primitive or reference).
![Page 5: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/5.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
5
Creating Arrays
There are 3 steps when creating an array
Declare an ArrayConstruct an ArrayInitialize an Array
![Page 6: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/6.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
6
Declare ArraysDeclare 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;
![Page 7: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/7.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
7
Construct ArraysUse ‘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];
![Page 8: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/8.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
8
Initialize ArrayJava arrays are zero based.By using the index you can access the array.
![Page 9: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/9.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
9
Initialize Arrayint 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“;
![Page 10: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/10.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
10
Anonymous ArrayJava 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} ;
![Page 11: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/11.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
11
Copy Array DataThe 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)
![Page 12: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/12.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
12
Copying Arraychar[] copyFrom = {'e', 'c', 'a', 'f', 'f', 'e', 'i',
'n', 'a', 't', 'e'};
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 1, copyTo, 0, 7);
![Page 13: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/13.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
13
Multi Dimensional ArraysJava, as with most languages, supports multi-
dimensional arrays.Ex :2 D Array3 D Array
In this lesson we discuss about 2-dimensional arrays.
But same principles apply for higher dimensions also.
![Page 14: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/14.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
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];
![Page 15: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/15.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
15
![Page 16: 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](https://reader036.vdocument.in/reader036/viewer/2022082611/56649ed85503460f94be6c6c/html5/thumbnails/16.jpg)
Sakuni Sellapperuma [Bsc. Spec.(Hon) in IT, SCJP]
16
End of Chapter 01Part 05Thank You!