Presentation is loading. Please wait.

Presentation is loading. Please wait.

03/16/101. 2 What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.

Similar presentations


Presentation on theme: "03/16/101. 2 What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics."— Presentation transcript:

1 03/16/101

2 2 What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics of Arrays, - All the array elements should be in same type - Arrays have a fixed length. They cannot be expanded or shrunk. - Elements of the array are accessed by the index

3 03/16/103 Declaring one Dimensional Arrays... data_type array_name[]; or data_type[] array; Data type indicates the type of the elements that are going to be stored. Ex: int arr[]; String arr[]; char arr[]; float arr[];

4 02/10/104 Initialize 1D arrays... Method 1: The arrays are initialized by using the ‘new’ keyword. The number of elements that the array will hold should be specified. int arr[]=new int[5]; 01234 arr[0]=10; arr[3]=56; 10 01234 56 01234

5 02/10/105 Initialize 1D arrays (Cont)... Method 2: The array can be initialized by specifying the elements in the array. int arr[]={12, 78, 65, 90, 10}; 1278659010 01234

6 03/16/106 Passing 1D arrays to methods... class ArrayExample { void calculate(int arr[]) { for(int i=0;i<5;i++)‏ { System.out.println(arr[i]); } class ArrayExample { void calculate(int arr[]) { for(int i=0;i<5;i++)‏ { System.out.println(arr[i]); } Calling Statement: int num[]={4,6,8,1,4}; ArrayExample arr=new ArrayExample(); arr.calculate(num); Calling Statement: int num[]={4,6,8,1,4}; ArrayExample arr=new ArrayExample(); arr.calculate(num);

7 02/10/107 Array of Objects... Arrays not only hold the elements with primitive data types. They can also hold the objects. Ex: Suppose there are 5 students in a class. Student stu[]=new Student[5];

8 03/16/108 Example... Suppose that there are 5 students in a class in a school. Modify the Example2 (Student Example) to hold the information of 5 students using an array. class Example2 { public static void main(String arg[]) { Student stu[]=new Student[5]; stu[i].age=10; } class Example2 { public static void main(String arg[]) { Student stu[]=new Student[5]; stu[i].age=10; }

9 9 Example... Write a JAVA program to store the information of two students. Each student have a Age, Gender, Marks for mathematics, Marks for statistics, Marks for computer science and average of three subjects. AttributeStudent 1Student 2 Age 2123 GenderFM Marks for Mathematics6789 Marks for Statistics7654 Marks for Computer Science7895

10 03/16/1010 Declaring two Dimensional Arrays... data_type array_name[][]; or data_type[][] array; Data type indicates the type of the elements that are going to be stored. Ex: int arr[][]; String arr[][]; char arr[][]; float arr[][];

11 02/10/1011 Initialize 2D arrays... Method 1: The arrays are initialized by using the ‘new’ keyword. The number of elements that the array will hold must be specified. int arr[]=new int[3][5]; 01234 arr[1][1]=10; 0 1 2 01234 10 0 1 2

12 02/10/1012 Initialize arrays (Cont)... Method 2: The array is initialized by specifying the elements in the array. int arr[][]={{12, 78,54}, {65, 90,22}, {10,8,11}, {3,9,15}, {8,1,90}}; 659022 012 10811 3915 8190 127854 0 1 2 3 4

13 03/16/1013 Example... Create a multiplication table using 2D array.


Download ppt "03/16/101. 2 What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics."

Similar presentations


Ads by Google