חלקPart 2: מתודות ומערכיםMethods & arrays
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
מערכים ומערכים דו-ממדייםArrays & 2D arrays
יצירה, גישה ואורך, לולאות על מערכים, Arrays.toString ו-sort, ומערכים דו-ממדיים כמו לוח משחק.
Creating, accessing and length, looping over arrays, Arrays.toString and sort, and 2D arrays like a game board.
הנקודות החשובותKey points
- מערך: שורה של תאים צמודים מאותו טיפוס, בגודל קבוע:array: A row of cells of the same type, with a fixed size:
- length: מספר התאים במערך:length: The number of cells in an array:
- מערך בגודל קבוע.An array has a fixed size.
- התא הראשון באינדקס 0 והאחרון ב-
length - 1.The first cell is index 0 and the last islength - 1. - for רגיל כשצריך את האינדקס, for-each כשצריך רק את הערכים.A plain for when you need the index, a for-each when you only need the values.
- למקסימום מתחילים מהתא הראשון.For a maximum, start from the first cell.
b = aמעתיק את ההפניה, לא את המערך: שניהם מצביעים לאותו עצם (שתי הפניות לאותו עצם).b = acopies the reference, not the array: both point to the same object (aliasing).clone()יוצר מערך חדש.clone()makes a new array.Arrays.sortממיין במקום.Arrays.sortsorts in place.
מונחיםTerms
- מערךarray: שורה של תאים צמודים מאותו טיפוס, בגודל קבוע:
int[] a = new int[5];אוint[] a = {1, 2, 3};. המערך הוא עצם ב-heap.A row of cells of the same type, with a fixed size:int[] a = new int[5];orint[] a = {1, 2, 3};. The array is an object on the heap. - אינדקסindex: המספר של מקום במחרוזת (או במערך). מתחילים מ-0: ב-
"java"התו באינדקס 0 הואj.The number of a position in a string (or an array). It starts at 0: in"java"the character at index 0 isj. - lengthlength: מספר התאים במערך:
a.length(בלי סוגריים, בשונה מ-length()של String). האינדקס האחרון הואa.length - 1.The number of cells in an array:a.length(no brackets, unlike String'slength()). The last index isa.length - 1. - שתי הפניות לאותו עצםaliasing:
int[] b = a;לא מעתיק את המערך:bמצביע על אותו מערך. שינוי דרךbנראה גם דרךa. להעתקה:a.clone().int[] b = a;doesn't copy the array:bpoints to the same array. A change throughbshows throughatoo. To copy:a.clone(). - מערך דו-ממדי2D array: מערך של מערכים:
int[][] grid = new int[3][4];הוא 3 שורות ו-4 עמודות.grid[r][c]הוא תא.An array of arrays:int[][] grid = new int[3][4];is 3 rows and 4 columns.grid[r][c]is one cell. - ArrayIndexOutOfBoundsExceptionArrayIndexOutOfBoundsException: החריגה כשפונים לתא שלא קיים:
Index 5 out of bounds for length 3.The exception for a cell that doesn't exist:Index 5 out of bounds for length 3. - מערךarray: שורה של תאים צמודים מאותו טיפוס, בגודל קבוע:
int[] a = new int[5];אוint[] a = {1, 2, 3};. המערך הוא עצם ב-heap.A row of cells of the same type, with a fixed size:int[] a = new int[5];orint[] a = {1, 2, 3};. The array is an object on the heap. - lengthlength: מספר התאים במערך:
a.length(בלי סוגריים, בשונה מ-length()של String). האינדקס האחרון הואa.length - 1.The number of cells in an array:a.length(no brackets, unlike String'slength()). The last index isa.length - 1. - שתי הפניות לאותו עצםaliasing:
int[] b = a;לא מעתיק את המערך:bמצביע על אותו מערך. שינוי דרךbנראה גם דרךa. להעתקה:a.clone().int[] b = a;doesn't copy the array:bpoints to the same array. A change throughbshows throughatoo. To copy:a.clone(). - מערך דו-ממדי2D array: מערך של מערכים:
int[][] grid = new int[3][4];הוא 3 שורות ו-4 עמודות.grid[r][c]הוא תא.An array of arrays:int[][] grid = new int[3][4];is 3 rows and 4 columns.grid[r][c]is one cell. - ArrayIndexOutOfBoundsExceptionArrayIndexOutOfBoundsException: החריגה כשפונים לתא שלא קיים:
Index 5 out of bounds for length 3.The exception for a cell that doesn't exist:Index 5 out of bounds for length 3.
Cheatsheet
יצירה, אינדקס ו-lengthCreating, indexing and length
int[] scores = {90, 75, 88};
System.out.println(scores[0]);
scores[1] = 80;
System.out.println(scores.length);
int[] empty = new int[3];
System.out.println(Arrays.toString(scores) + " " + Arrays.toString(empty));לולאות על מערך: סכום ומקסימוםLooping over an array: sum and max
int[] t = {21, 25, 19, 30};
int sum = 0;
for (int x : t) {
sum += x;
}
int max = t[0];
for (int i = 1; i < t.length; i++) {
if (t[i] > max) {
max = t[i];
}שני משתנים, מערך אחדTwo variables, one array
int[] a = {5, 6, 7};
int[] b = a;
b[0] = 50;
int[] c = a.clone();
c[1] = 60;
System.out.println(a[0] + " " + a[1]);מיון ומערך דו-ממדיSorting and a 2D array
int[] n = {4, 1, 3};
Arrays.sort(n);
System.out.println(Arrays.toString(n));
char[][] board = new char[2][3];
for (int r = 0; r < 2; r++) {
for (int c = 0; c < 3; c++) {
board[r][c] = (r + c) % 2 == 0 ? 'X' : 'O';
}
System.out.println(new String(board[r]));
}שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|---|---|
ArrayIndexOutOfBoundsException | צייד באגים: מעבר לקצהBug hunter: past the edge | Index 3 out of bounds for length 3: ב-i <= a.length הלולאה מגיעה ל-i = 3, אבל האינדקס האחרון הוא 2. מתקנים ל-i < a.length.Index 3 out of bounds for length 3: with i <= a.length the loop reaches i = 3, but the last index is 2. Fix it to i < a.length. |