Similarity with 1d arrays • Each element in the 2D array must by the same type, • either a primitive type or object type. • Subscripted variables can be use just like a variable: rating[0][3] = 10



Yüklə 12,61 Kb.
tarix08.05.2023
ölçüsü12,61 Kb.
#109558
firvads eng zori2


Similarity with 1D Arrays • Each element in the 2D array must by the same type, • either a primitive type or object type. • Subscripted variables can be use just like a variable: rating[0][3] = 10; • Array indices must be of type int and can be a literal, variable, or expression. rating[3][j] = j; • If an array element does not exists, the Java runtime system will give you an ArrayIndexOutOfBoundsException 4 Summer 2010 15-110 (Reid-Miller) Declaring 2D Arrays • Declare a local variable rating that references a 2D array of int: • Declare a field family that reference a 2D array of GiftCards: • Create a 2D array with 3 rows and 4 columns and assign the reference to the new array to rating: • Shortcut to declare and create a 2D array: int[][] rating; private GiftCard[][] family; rating = new int[3][4]; int[][] rating = new int[3][4]; 5 Summer 2010 15-110 (Reid-Miller) Example 1 • Find the average rating by the reviewer in row 2. int sum = 0; for (int col = 0; col <= 3; col++) { sum += rating[2][col]; } double average = (double) sum / 4; 2 6 9 3 7 1 7 Similarity with 1D Arrays • Each element in the 2D array must by the same type, • either a primitive type or object type. • Subscripted variables can be use just like a variable: rating[0][3] = 10; • Array indices must be of type int and can be a literal, variable, or expression. rating[3][j] = j; • If an array element does not exists, the Java runtime system will give you an ArrayIndexOutOfBoundsException 4 Summer 2010 15-110 (Reid-Miller) Declaring 2D Arrays • Declare a local variable rating that references a 2D array of int: • Declare a field family that reference a 2D array of GiftCards: • Create a 2D array with 3 rows and 4 columns and assign the reference to the new array to rating: • Shortcut to declare and create a 2D array: int[][] rating; private GiftCard[][] family; rating = new int[3][4]; int[][] rating = new int[3][4]; 5 Summer 2010 15-110 (Reid-Miller) Example 1 • Find the average rating by the reviewer in row 2. int sum = 0; for (int col = 0; col <= 3; col++) { sum += rating[2][col]; } double average = (double) sum / 4; 2 6 9 3 7 1 7 Similarity with 1D Arrays • Each element in the 2D array must by the same type, • either a primitive type or object type. • Subscripted variables can be use just like a variable: rating[0][3] = 10; • Array indices must be of type int and can be a literal, variable, or expression. rating[3][j] = j; • If an array element does not exists, the Java runtime system will give you an ArrayIndexOutOfBoundsException 4 Summer 2010 15-110 (Reid-Miller) Declaring 2D Arrays • Declare a local variable rating that references a 2D array of int: • Declare a field family that reference a 2D array of GiftCards: • Create a 2D array with 3 rows and 4 columns and assign the reference to the new array to rating: • Shortcut to declare and create a 2D array: int[][] rating; private GiftCard[][] family; rating = new int[3][4]; int[][] rating = new int[3][4]; 5 Summer 2010 15-110 (Reid-Miller) Example 1 • Find the average rating by the reviewer in row 2. int sum = 0; for (int col = 0; col <= 3; col++) { sum += rating[2][col]; } double average = (double) sum / 4; 2 6 9 3 7 1 7 Similarity with 1D Arrays • Each element in the 2D array must by the same type, • either a primitive type or object type. • Subscripted variables can be use just like a variable: rating[0][3] = 10; • Array indices must be of type int and can be a literal, variable, or expression. rating[3][j] = j; • If an array element does not exists, the Java runtime system will give you an ArrayIndexOutOfBoundsException 4 Summer 2010 15-110 (Reid-Miller) Declaring 2D Arrays • Declare a local variable rating that references a 2D array of int: • Declare a field family that reference a 2D array of GiftCards: • Create a 2D array with 3 rows and 4 columns and assign the reference to the new array to rating: • Shortcut to declare and create a 2D array: int[][] rating; private GiftCard[][] family; rating = new int[3][4]; int[][] rating = new int[3][4]; 5 Summer 2010 15-110 (Reid-Miller) Example 1 • Find the average rating by the reviewer in row 2. int sum = 0; for (int col = 0; col <= 3; col++) { sum += rating[2][col]; } double average = (double) sum / 4; 2 6 9 3 7 1 7 Similarity with 1D Arrays • Each element in the 2D array must by the same type, • either a primitive type or object type. • Subscripted variables can be use just like a variable: rating[0][3] = 10; • Array indices must be of type int and can be a literal, variable, or expression. rating[3][j] = j; • If an array element does not exists, the Java runtime system will give you an ArrayIndexOutOfBoundsException 4 Summer 2010 15-110 (Reid-Miller) Declaring 2D Arrays • Declare a local variable rating that references a 2D array of int: • Declare a field family that reference a 2D array of GiftCards: • Create a 2D array with 3 rows and 4 columns and assign the reference to the new array to rating: • Shortcut to declare and create a 2D array: int[][] rating; private GiftCard[][] family; rating = new int[3][4]; int[][] rating = new int[3][4]; 5 Summer 2010 15-110 (Reid-Miller) Example 1 • Find the average rating by the reviewer in row 2. int sum = 0; for (int col = 0; col <= 3; col++) { sum += rating[2][col]; } double average = (double) sum / 4; 2 6 9 3 7 1 7 Similarity with 1D Arrays • Each element in the 2D array must by the same type, • either a primitive type or object type. • Subscripted variables can be use just like a variable: rating[0][3] = 10; • Array indices must be of type int and can be a literal, variable, or expression. rating[3][j] = j; • If an array element does not exists, the Java runtime system will give you an ArrayIndexOutOfBoundsException 4 Summer 2010 15-110 (Reid-Miller) Declaring 2D Arrays • Declare a local variable rating that references a 2D array of int: • Declare a field family that reference a 2D array of GiftCards: • Create a 2D array with 3 rows and 4 columns and assign the reference to the new array to rating: • Shortcut to declare and create a 2D array: int[][] rating; private GiftCard[][] family; rating = new int[3][4]; int[][] rating = new int[3][4]; 5 Summer 2010 15-110 (Reid-Miller) Example 1 • Find the average rating by the reviewer in row 2. int sum = 0; for (int col = 0; col <= 3; col++) { sum += rating[2][col]; } double average = (double) sum / 4; 2 6 9 3 7 1 7 Similarity with 1D Arrays • Each element in the 2D array must by the same type, • either a primitive type or object type. • Subscripted variables can be use just like a variable: rating[0][3] = 10; • Array indices must be of type int and can be a literal, variable, or expression. rating[3][j] = j; • If an array element does not exists, the Java runtime system will give you an ArrayIndexOutOfBoundsException 4 Summer 2010 15-110 (Reid-Miller) Declaring 2D Arrays • Declare a local variable rating that references a 2D array of int: • Declare a field family that reference a 2D array of GiftCards: • Create a 2D array with 3 rows and 4 columns and assign the reference to the new array to rating: • Shortcut to declare and create a 2D array: int[][] rating; private GiftCard[][] family; rating = new int[3][4]; int[][] rating = new int[3][4]; 5 Summer 2010 15-110 (Reid-Miller) Example 1 • Find the average rating by the reviewer in row 2. int sum = 0; for (int col = 0; col <= 3; col++) { sum += rating[2][col]; } double average = (double) sum / 4; 2 6 9 3 7 1 7
Yüklə 12,61 Kb.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin