int size 5 int my Array my Array

  • Slides: 39
Download presentation

 הקצאת זכרון באמצעות משתנה int size = 5; int[] my. Array; my. Array

הקצאת זכרון באמצעות משתנה int size = 5; int[] my. Array; my. Array = new int[size]; 8

length – גודל המערך int size = 5; int[] my. Array; my. Array =

length – גודל המערך int size = 5; int[] my. Array; my. Array = new int[size]; System. out. println(my. Array. length); // prints 5 size = 6; ? מה לדעתכם תדפיס כעת השורה הבאה System. out. println(my. Array. length); 9

 שאלה : • ובמקרה int[] my. Array = new int[0]; System. out. println(my.

שאלה : • ובמקרה int[] my. Array = new int[0]; System. out. println(my. Array. length); ? מה יודפס למסך 0 : • תשובה 13

 הגדרה והקצאה בשורה אחת • int[] my. Array = new int[5]; my. Array

הגדרה והקצאה בשורה אחת • int[] my. Array = new int[5]; my. Array 0 0 0 • int[] small. Primes ={2, 3, 5, 7, 11}; small. Primes 2 3 5 7 11 14

 פנייה לאיבר )תא( במערך int[] small. Primes = {2, 3, 5, 7, 11};

פנייה לאיבר )תא( במערך int[] small. Primes = {2, 3, 5, 7, 11}; index 0 1 2 3 4 value 2 3 5 7 11 : • קריאת ערך מתא במערך int num = small. Primes[3] * 2; // num = 7*2 = 14 16

 פנייה לאיבר במערך index value 0 2 1 3 2 5 3 13

פנייה לאיבר במערך index value 0 2 1 3 2 5 3 13 4 11 • small. Primes[5] = 17; • num = small. Primes[-1]; Run Time Error 18

 פעולות על מערך //Demonstrates basic array operations class Basic. Array { public static

פעולות על מערך //Demonstrates basic array operations class Basic. Array { public static void main(String[] arg) { int[] small. Primes = {2, 3, 5, 7, 11}; System. out. println(small. Primes[0]); 2 small. Primes[0] = 13; System. out. println(small. Primes[0]); 13 System. out. println(small. Primes. length); 5 for(int i=0; i<small. Primes. length; i=i+1) System. out. print(small. Primes[i]+", "); 13, 3, 5, 7, 11, } //main } //class Basic. Array 20

 איתחול מערך ע"י לולאה //Demonstrate array init with a loop class Int. Array

איתחול מערך ע"י לולאה //Demonstrate array init with a loop class Int. Array { public static void main(String[] arg) { int[] int. Array = new int[5]; System. out. println(); for(int i=0; i < int. Array. length ; i=i+1) int. Array [i] = i*3; for(int i=0; i<int. Array. length; i=i+1) System. out. print(int. Array[i]+", "); } //main } //class Int. Array 0, 3, 6, 9, 12, 21

 משתנה שאינו פרימיטיבי int[] x. Array = new int[5]; int[] y. Array =

משתנה שאינו פרימיטיבי int[] x. Array = new int[5]; int[] y. Array = x. Array; x. Array[1] = 6; x. Array 0 0 6 0 0 0 y. Array 23

 שאלה ? • מה יקרה בזכרון בזמן הרצת הקוד הבא int[] my. Array;

שאלה ? • מה יקרה בזכרון בזמן הרצת הקוד הבא int[] my. Array; my. Array = new int[5]; my. Array = new int[10]; my. Array 0 0 0 0 24

null כתובת int[] arr 1 = null; int[] arr 2 = null; System. out.

null כתובת int[] arr 1 = null; int[] arr 2 = null; System. out. println(arr 1==arr 2); true 26

 מימדי - מערך דו int[][] array 1 = new int [3][2]; array 1

מימדי - מערך דו int[][] array 1 = new int [3][2]; array 1 0 0 0 28

length – אורך של מערך array 1 array 2 1 3 5 6 7

length – אורך של מערך array 1 array 2 1 3 5 6 7 4 • • • array 1. length 3 array 1[0]. length 2 array 2. length 3 array 2[2]. length 1 array 2[1]. length 3 array 2[3]. length Run time Error 30

 פנייה לאיבר array 2 1 5 3 6 7 4 9 System. out.

פנייה לאיבר array 2 1 5 3 6 7 4 9 System. out. println(array 2[1][0]); // prints 5 array 2[2][0] = 9; 31

class Array. Ref{ public static void main(String []args){ int[][] x; x = new int[3][];

class Array. Ref{ public static void main(String []args){ int[][] x; x = new int[3][]; x[0] = new int[3]; x[1] = x[0]; x[0][1] = 7; //prints the 2 D array for (int i = 0 ; i < x. length ; i= i+1){ for(int j=0 ; j < x[i]. length; j = j+1){ System. out. print(x[i][j]+” ”); } System. out. println(); } } } 0 7 0 Null. Pointer. Exception 32

 העתקת מערך class Copy 2 d. Array{ public static void main(String[] args){ int[][]

העתקת מערך class Copy 2 d. Array{ public static void main(String[] args){ int[][] a = {{1, 3}, {5, 6, 7}, {4}}; int[][] c; //Create the 2 d. Array row by row c = new int[a. length][]; int i, j; for ( i=0 ; i < c. length; i = i+1) c[i] = new int[a[i]. length]; for ( i=0; i < c. length; i = i+1) for (j=0 ; j < c[i]. length ; j = j+1) c[i][j] = a[i][j]; } //main } 34

 משולש פסקל 1 11 121 1331 14641 1 5 10 10 5 1

משולש פסקל 1 11 121 1331 14641 1 5 10 10 5 1 מה החוקים לחישוב משולש פסקל Pascal[j][0] = Pascal[j][j]=1 Pascal[j][i] = Pascal[j-1][i] + Pascal[j-1][i-1] Wikipedia http: //en. wikipedia. org/wiki/Pascal%27 s_triangle 35

 הדפסת לוח הכפל class Multiplication. Table { public static void main(String[] arg) {

הדפסת לוח הכפל class Multiplication. Table { public static void main(String[] arg) { final int ROW = 13; final int COLUMN = 13; int[][] mat = new int[ROW][COLUMN]; for (int i=0; i < ROW; i=i+1) for (int j=0; j < COLUMN; j=j+1) mat[i][j] = i*j; //prints the matrix for (int i=0; i < ROW; i=i+1) { for (int j=0; j < COLUMN; j=j+1) System. out. print(mat[i][j]+"t"); System. out. println(); } } //main } //class 36

 חיבור מטריצות // Adds two matrices public class Add{ public static void main(String[]

חיבור מטריצות // Adds two matrices public class Add{ public static void main(String[] args){ int[][] a = { /* insert data here */ }; int[][] b = { /* insert data here */ }; int[][] c; c = new int[a. length][a[0]. length]; for (int i = 0 ; i<a. length; i=i+1) for (int j =0; j< a[i]. length; j= j+1) c[i][j] = a[i][j] + b[i][j]; } //main } //class Add 37

//Multiplies two matrices pbulic class Mul{ public static void main(String[] args){ int[][] a =

//Multiplies two matrices pbulic class Mul{ public static void main(String[] args){ int[][] a = { /* insert data here */ }; int[][] b = { /* insert data here */ }; int[][]c; //Create the matrix row by row c = new int[a. length][]; for (int i = 0 ; i < c. length ; i = i+1) c[i] = new int[b[0]. length]; //For each element in the matrix for (int i=0; i<a. length ; i=i+1) for(int j=0; j<b[0]. length; j=j+1){ //NOTICE that b. length is equal to a[i]. length //Calculate the sum c[i][j] = 0; for (int k=0; k<a[i]. length ; k= k+1) c[i][j] = c[i][j] + (a[i][k] * b[k][j]); } //for } //main 39 } //class Mul