Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array.  ARRAYS ALLOW US TO STORE ELEMENTS OF SINGLE DATA TYPE CONTAGUISLY IN MEMORY  EACH ELEMENT IS ASSOCIATED WITH AN INDEX OR LOCATION  WE CAN ACCESS.

Similar presentations


Presentation on theme: "Array.  ARRAYS ALLOW US TO STORE ELEMENTS OF SINGLE DATA TYPE CONTAGUISLY IN MEMORY  EACH ELEMENT IS ASSOCIATED WITH AN INDEX OR LOCATION  WE CAN ACCESS."— Presentation transcript:

1 Array

2  ARRAYS ALLOW US TO STORE ELEMENTS OF SINGLE DATA TYPE CONTAGUISLY IN MEMORY  EACH ELEMENT IS ASSOCIATED WITH AN INDEX OR LOCATION  WE CAN ACCESS ANY ELEMENT BY INDEXING INTO THE ARRAY

3 Array Type[]Array name= new Array Type[size] int[] array1 = new int[4]; double[] array2 = new double[4]; String[] array3 = new String[4];

4 int[] a1 = new int[4]; double[] a2 = new double[4]; String[] a3 = new String[4]; a1[1]=4; a2[1]=4; a3[1]="4";

5 int[] a1 = new int[4]; double[] a2 = new double[4]; String[] a3 = new String[4]; a1[2]=(int)5.6; a2[2]=5.6; a3[2]="5.6";

6 Array Type[]Array name ={n1,n2,n3,….}; int[] array1 = {1,2,3,4}; double[] array2 = {1,2,4.2,6.3,4.12}; String[] array3 = {“ab”,”cd”,”ef”,”gh”};

7 int[] a4 = {5,8,9,2,6,1}; for(int i=0;i<a4.length;i++) { System.out.print(a4[i]+" ");

8 int[] a4 = {5,8,9,2,6,1}; for(int i=0;i<a4.length;i+=2) { System.out.print(a4[i]+" ");

9 int[] a4 = {5,8,9,2,6,1}; for(int i=1;i<a4.length;i+=2) { System.out.print(a4[i]+" ");

10 int[] a4 = {5,8,9,2,6,1}; for(int i=0;i<a4.length;i++) { if (a4[i]%2==0){ System.out.print(a4[i]+" "); }

11 int[] a4 = {5,8,9,2,6,1}; int total = 0; for (int i=0; i<a4.length; i++) { total += a4[i]; } System.out.print(total);

12 Array Type[]Array name ={n1,n2,n3,….}; int[] array1 = {1,2,3,4,8};

13 Array Type[]Array name ={n1,n2,n3,….}; double[] array1 = {1,2,4.2,6.3,4.12,8};

14 Array Type[]Array name ={n1,n2,n3,….}; String[] array1 = {“ab”,”cd”,”ef”,”gh”,”sw”};

15 for(int i = 0; i < size; i++) array1[i] = i + 5; Int size = 5; int[] array1 = new int[size];

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32 You want to initialize all of the elements of a double array a to the same value equal to 1.5. What could you write? Assume that the array has been correctly initialized. A. for(int i=1; i<a.length; i++) a[i] = 1.5; B. for(int i=0; i<=a.length; i++) a[i] = 1.5; C. for(int i=0; i<a.length; i++) a[i] = 1.5; D. for(int i=0; i<a.length+1; i++) a[i] = 1.5; E. for(int i=0; i<a.length-1; i++) a[i] = 1.5;

33 Refer to the following code that finds the smallest value in an array. int min = /* some value */ int index; while (index < arr.length) { if(arr[index] < min) min = arr[index]; index++; } System.out.println(min); Which replacement(s) for /* some value */will always result in correct execution of the code. I Integer.Min_Value II Integer.Max._Value III arr[0] (A)I only (B) II only (C)III only (D)I and III only (E) II and III only

34 A matrix(two-dimensional array) is declared as int [][] mat = new int[2][2]; Consider the following code: 10for (int r = 0; r < mat.length; r++) 11 for(int c = 0; c < mat[r].length; c++) 12 if(r == c) 13 mat[r][c] = Math.abs(mat[r][c]);

35

36

37

38

39

40

41

42


Download ppt "Array.  ARRAYS ALLOW US TO STORE ELEMENTS OF SINGLE DATA TYPE CONTAGUISLY IN MEMORY  EACH ELEMENT IS ASSOCIATED WITH AN INDEX OR LOCATION  WE CAN ACCESS."

Similar presentations


Ads by Google