Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.

Similar presentations


Presentation on theme: "1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods."— Presentation transcript:

1 1

2 Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods 5 Write program using single and multidimensional array 6 2

3 3 Collection of data values of the same type. Each element in the array is referred by its subscript (position number/index). in Java, array is an object – need to use the new method during declaration.

4 To declare an array, write the data type, followed by a set of square brackets[], followed by the identifier name. Example 1: Declaring array int[] ages; or int ages[]; Example 2: Creating array object/instance float[] studsMark = new float[5]; //create object/instance and refer the object as studMarks. //studMarks can be used to store up to 5 float values 4

5 5

6 6 String[] names; names = new String[3]; names[0] = "Georgianna"; names[1] = "Jen"; names[2] = "Simon"; String[] names; names = new String[3]; names[0] = "Georgianna"; names[1] = "Jen"; names[2] = "Simon"; MyDate[] dates; dates = new MyDate[3]; dates[0] = new MyDate(22, 7, 1964); dates[1] = new MyDate(1, 1, 2000); dates[2] = new MyDate(22, 12, 1964); MyDate[] dates; dates = new MyDate[3]; dates[0] = new MyDate(22, 7, 1964); dates[1] = new MyDate(1, 1, 2000); dates[2] = new MyDate(22, 12, 1964); String[] names = { "Georgianna", "Jen", "Simon" }; String[] names = { "Georgianna", "Jen", "Simon" }; MyDate[] dates = { new MyDate(22, 7, 1964), new MyDate(1, 1, 2000), new MyDate(22, 12, 1964) }; MyDate[] dates = { new MyDate(22, 7, 1964), new MyDate(1, 1, 2000), new MyDate(22, 12, 1964) };

7 7 When a single element of an array is passed to a method it is handled like any other variable. public class PassElements { public static void main(String[]arg) { int [] num={5,10,15,20,25,30,35,40}; for(int i=0;i<num.length;i++) showValue(num[i]); } public static void showValue(int n) { System.out.println(n+ " "); } public class PassElements { public static void main(String[]arg) { int [] num={5,10,15,20,25,30,35,40}; for(int i=0;i<num.length;i++) showValue(num[i]); } public static void showValue(int n) { System.out.println(n+ " "); }

8 8 public class PassElements { public static void main(String[]arg) { int [] num={5,10,15,20,25,30,35,40}; PassElements pass = new PassElements(); for(int i=0;i<num.length;i++) pass.showValue(num[i]); } public void showValue(int n) { System.out.println(n+ " "); } public class PassElements { public static void main(String[]arg) { int [] num={5,10,15,20,25,30,35,40}; PassElements pass = new PassElements(); for(int i=0;i<num.length;i++) pass.showValue(num[i]); } public void showValue(int n) { System.out.println(n+ " "); }

9 9 A method can return a reference to an array. The return type of the method must be declared as an array of the right type. public static double[] getArray() { double[] array = { 1.2, 2.3, 4.5, 6.7, 8.9 }; return array; } The getArray method is a public static method that returns an array of doubles.

10 10 Are arrays with more than one dimension. Is a collection of a number of one-dimensional arrays placed one below the other. Two-dimensional arrays represent data in terms of rows and columns. It has two subscripts. Example: int[][] twoDim = new int[4][5]; Array of four arrays of five integers each. Multidimensional Array:

11 11

12 Assume that there are 5 students in a class and each of them study three different subjects, for example Mathematics, Physics and Chemistry. The marks of each student in all three subjects can be represented in a single row. 12

13 13

14 14

15 Syntax [][] = new int[Row][Column]; Example int marks_table[][] = new int[5][3]; 15


Download ppt "1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods."

Similar presentations


Ads by Google