lecturer : sakuni sellapperuma. introduction an array is a container object that holds a fixed...

Post on 02-Jan-2016

217 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Java ArraysLecturer : Sakuni Sellapperuma

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.

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.

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). 

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

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;

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];

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

8

Initialize ArrayJava arrays are zero based.By using the index you can access the array.

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“;

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} ;

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)

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);

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.

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];

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

15

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

16

End of Chapter 01Part 05Thank You!

top related