Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSI 3125, Preliminaries, page 1 Arrays. CSI 3125, Preliminaries, page 2 Arrays Group of related typed variables that referred to a common name Each data.

Similar presentations


Presentation on theme: "CSI 3125, Preliminaries, page 1 Arrays. CSI 3125, Preliminaries, page 2 Arrays Group of related typed variables that referred to a common name Each data."— Presentation transcript:

1 CSI 3125, Preliminaries, page 1 Arrays

2 CSI 3125, Preliminaries, page 2 Arrays Group of related typed variables that referred to a common name Each data items are same type Each element in an array can be accessed by its index

3 CSI 3125, Preliminaries, page 3 One-Dimensional Arrays To create an array Syntax data type array name[]; example int arr[]; This declaration states that arr is an array variable no array actually exist. The value of arr is set to null Which represents an array with no values To create physical array of integers using new operator

4 CSI 3125, Preliminaries, page 4 One-Dimensional Arrays The general form of new as it applies to one- dimensional arrays appears as follows: array-var = new type[size]; Example arr=new int[5]; After this statement executes, arr will refer to an array of 5 integers. All elements in the array will be initialized to zero. arr[0]=10 arr[1]=20

5 CSI 3125, Preliminaries, page 5 One-Dimensional Arrays It is possible to combine the declaration of the array variables with the allocation of the array itself int arr[] = new int[5]; Array can be initialized when they are declared with a list of values separated with comma The array will automatically be created large enough to hold the number of values. There is no need of new

6 CSI 3125, Preliminaries, page 6 One-Dimensional Arrays Example int arr []={1,10,20,30}; Then arr[0]=1 arr[1]=10 arr[2]=20 arr[3]=30

7 CSI 3125, Preliminaries, page 7 Multidimensional Arrays A two D array can be declared as Int twoD[][]= new int [2][3]; This allocates a 2 by 3 array Ie 2 rows and 3 cols, it can hold 2X3=6 elements

8 CSI 3125, Preliminaries, page 8 Multidimensional Arrays Manually allocate different size in 2D int towd[][]=new int [][4]; towd[0]=new int[1]; twod[1]=new int[2]; twod[2]=new int[3]; twod[3]=new int[4]; int i,j,k=0; for(i=0;i<4;i++) { for(j=0;j<i+1;j++) { towd[i][j]=k; } k++; } 0 1 2 3 4 5 6 7 8 9 for(i=0;i<4;i++) { for(j=0;j<i+1;j++) { System.out.print(twoD[i][j]+” “); } System.out.println(); }

9 CSI 3125, Preliminaries, page 9 Multidimensional Arrays It is possible to initialize multidimensional arrays. To do so, simply enclose each dimension’s initialize within its own set of curly braces. int twod[][]={ {1,2},{3,4}}; twoD[0][0]=1 twoD[0][1]=2 twoD[1][0]=3 twoD[1][1]=4

10 CSI 3125, Preliminaries, page 10 String String can be declared as Sting str=“this is popo”; System.out.print(str); o/p this is popo


Download ppt "CSI 3125, Preliminaries, page 1 Arrays. CSI 3125, Preliminaries, page 2 Arrays Group of related typed variables that referred to a common name Each data."

Similar presentations


Ads by Google