logging in or signing up Lecture3 aSGuest72704 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 6 Category: Education License: All Rights Reserved Like it (0) Dislike it (0) Added: October 24, 2010 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Slide 1: Array Java arrays are objects Arrays must be accessed via name Defining an array 1. In two stepDataType [] name OR DataType name [] Where: DataType specifies the data type the brackets [ ] indicate this is an array name is the array name to access the array Slide 2: Array For Example int c []; c= new int[10]; Primitives elements are initialized to zero or false. Non Primitives elements are initialized to null. Slide 3: double [] scores = new double [10]; The above statement will: allocate block of memory to hold 10 doubles initialize block with zeros returns the address of the block store address of the block in scores Slide 4: Java provides a few operations to use with arrays, including assignment Consider:int [] alist = { 11, 12, 13, 14};int [] blist;blist = alist; Recall that alist and blist are array names. alist contains an address of where the numbers are blist now contains that same address blist does not have a copy of alist Slide 5: To actually create another array with its own values, Java provides the .clone() method int [] alist = { 11, 12, 13, 14}; int [] blist; blist = alist.clone(); Now there are two separate lists of numbers, one with name as alist, the other as blist Slide 6: Methods can accept arrays via parameters Use square brackets [ ] in the parameter declaration:static int sum(int [] array){ for (int i=0; i < array.length; i++) . . . } This works for any size array use the .length attribute of an array to control the for loop Slide 7: Methods can return Arrays static int [] sum1() { int x[] ={10, 20,30}; for(int i1=0;i1<x.length;i1++) x[i1]=x[i1]+1; return x; } Slide 8: How to call in a main Function public static void main(String[] args) { int b[]={10, 20, 30}; int a[]; for(int i=0;i<b.length;i++) System.out.println("B is " + a[i]); a=sum1(); for(int i=0;i<a.length;i++) System.out.println("B is " + a[i]); } Slide 9: Methods can take array as an Argument and Can return Array public class FirstApp { public static void main(String[] args) { int b[]={10, 20, 30}; int a[]; a=sum1(b); for(int i=0;i<a.length;i++) System.out.println(“a is " + a[i]); } static int [] sum1(int x[]) { for(int i1=0;i1<x.length;i1++) x[i1]=x[i1]+1; return x; } } Slide 10: Array Equality Java has an equals() method for classesif (a1.equals(a2)) … If a1 and a2 are arrays, the equals() method just looks at the addresses the handles point to They could be pointing to different addresses but the contents of the array still be equal It is the contents that we really wish to compare Slide 11: We must write our own method to compare the arrays they both must have the same length then use a for( ) loop to compare element by element for equality if (list1.length==list2.length){ for (int i = 0; i < list1.length; i++) if (list1[i] != list2[i]) return false; return true; }else return false; Array Equality Slide 12: Multiple-Subscripted Arrays Represent tables Arranged by m rows and n columns (m by n array) Can have more than two subscripts Java does not support multiple subscripts directly Create an array with arrays as its elements Array of arrays Declaration Double brackets int b[][];b = new int[ 3 ][ 3 ]; Declares a 3 by 3 array Slide 13: Declaration (continued) Initializer lists int b[][] = { { 1, 2 }, { 3, 4 } }; Slide 14: Each row can have a different number of columns: int b[][];b = new int[ 2 ][ ]; // allocate rowsb[ 0 ] = new int[ 5 ]; // allocate columns for row 0b[ 1 ] = new int[ 3 ]; // allocate columns for row 1 Notice how b[ 0 ] is initialized as a new int array You can use b[0].length Slide 15: Class class emp { int id; int age; int exp; } Slide 16: Class public class FirstApp { public static void main(String[] args) { emp e = new emp(); e.age =30; e.exp =5; e.id=101; System.out.println(“Age is " + e.age); } Note: If emp e; then try e.age then it will give the compile error Slide 17: Class public class FirstApp { public static void main(String[] args) { emp e; e = new emp(); e.age =30; e.exp =5; e.id=101; System.out.println(“Age is " + e.age); } Slide 18: Assigning Object Reference Variables emp e1 = new emp(); emp e2= e1; After the execution of this statement, both e1 and e2 refers to the same object. Thus any changes made to the object through b2 will affect the object to which b1 is referring since they are the same object. If e1=null then e2 still points to the original object. You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
Lecture3 aSGuest72704 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 6 Category: Education License: All Rights Reserved Like it (0) Dislike it (0) Added: October 24, 2010 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Slide 1: Array Java arrays are objects Arrays must be accessed via name Defining an array 1. In two stepDataType [] name OR DataType name [] Where: DataType specifies the data type the brackets [ ] indicate this is an array name is the array name to access the array Slide 2: Array For Example int c []; c= new int[10]; Primitives elements are initialized to zero or false. Non Primitives elements are initialized to null. Slide 3: double [] scores = new double [10]; The above statement will: allocate block of memory to hold 10 doubles initialize block with zeros returns the address of the block store address of the block in scores Slide 4: Java provides a few operations to use with arrays, including assignment Consider:int [] alist = { 11, 12, 13, 14};int [] blist;blist = alist; Recall that alist and blist are array names. alist contains an address of where the numbers are blist now contains that same address blist does not have a copy of alist Slide 5: To actually create another array with its own values, Java provides the .clone() method int [] alist = { 11, 12, 13, 14}; int [] blist; blist = alist.clone(); Now there are two separate lists of numbers, one with name as alist, the other as blist Slide 6: Methods can accept arrays via parameters Use square brackets [ ] in the parameter declaration:static int sum(int [] array){ for (int i=0; i < array.length; i++) . . . } This works for any size array use the .length attribute of an array to control the for loop Slide 7: Methods can return Arrays static int [] sum1() { int x[] ={10, 20,30}; for(int i1=0;i1<x.length;i1++) x[i1]=x[i1]+1; return x; } Slide 8: How to call in a main Function public static void main(String[] args) { int b[]={10, 20, 30}; int a[]; for(int i=0;i<b.length;i++) System.out.println("B is " + a[i]); a=sum1(); for(int i=0;i<a.length;i++) System.out.println("B is " + a[i]); } Slide 9: Methods can take array as an Argument and Can return Array public class FirstApp { public static void main(String[] args) { int b[]={10, 20, 30}; int a[]; a=sum1(b); for(int i=0;i<a.length;i++) System.out.println(“a is " + a[i]); } static int [] sum1(int x[]) { for(int i1=0;i1<x.length;i1++) x[i1]=x[i1]+1; return x; } } Slide 10: Array Equality Java has an equals() method for classesif (a1.equals(a2)) … If a1 and a2 are arrays, the equals() method just looks at the addresses the handles point to They could be pointing to different addresses but the contents of the array still be equal It is the contents that we really wish to compare Slide 11: We must write our own method to compare the arrays they both must have the same length then use a for( ) loop to compare element by element for equality if (list1.length==list2.length){ for (int i = 0; i < list1.length; i++) if (list1[i] != list2[i]) return false; return true; }else return false; Array Equality Slide 12: Multiple-Subscripted Arrays Represent tables Arranged by m rows and n columns (m by n array) Can have more than two subscripts Java does not support multiple subscripts directly Create an array with arrays as its elements Array of arrays Declaration Double brackets int b[][];b = new int[ 3 ][ 3 ]; Declares a 3 by 3 array Slide 13: Declaration (continued) Initializer lists int b[][] = { { 1, 2 }, { 3, 4 } }; Slide 14: Each row can have a different number of columns: int b[][];b = new int[ 2 ][ ]; // allocate rowsb[ 0 ] = new int[ 5 ]; // allocate columns for row 0b[ 1 ] = new int[ 3 ]; // allocate columns for row 1 Notice how b[ 0 ] is initialized as a new int array You can use b[0].length Slide 15: Class class emp { int id; int age; int exp; } Slide 16: Class public class FirstApp { public static void main(String[] args) { emp e = new emp(); e.age =30; e.exp =5; e.id=101; System.out.println(“Age is " + e.age); } Note: If emp e; then try e.age then it will give the compile error Slide 17: Class public class FirstApp { public static void main(String[] args) { emp e; e = new emp(); e.age =30; e.exp =5; e.id=101; System.out.println(“Age is " + e.age); } Slide 18: Assigning Object Reference Variables emp e1 = new emp(); emp e2= e1; After the execution of this statement, both e1 and e2 refers to the same object. Thus any changes made to the object through b2 will affect the object to which b1 is referring since they are the same object. If e1=null then e2 still points to the original object.