









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Each data value stored in an array is called an element. • Each element is accessed using an integer index or subscript. • As with strings, the ...
Typology: Schemes and Mind Maps
1 / 16
This page cannot be seen from the preview
Don't miss anything!










int[] counts = new int[23];!* counts[0] = 12;! counts[1] = 22;! counts[2] = 17;! counts[3] = 16;! counts[4] = 4;! counts[5] = 11;
no length length can be an int expression reference to the array
int sum = 0;! for (int i = 0; i < counts.length; i++) {! sum = sum + counts[i];! }! if (counts.length > 0){! System.out.print(“The average number of “ + !!! “bacteria colonies is “);! System.out.println(________________________);! }
Constant : number of elements in the array Not a method: There is no () !!
index > 5 is out of bounds
index < 0 is out of bounds
int min = _________ ;! for (int i = __; i < counts.length; i++) {! if (________________) {! min = counts[i];! }! }!
counts[0] 1!
! int[] sequence = new int[10];! ! fillAllSums(sequence);! ! …! }!!!!!!!! !! !!!!!!!! !! public static void fillAllSums(int[] seq) {! ! seq[0] = 0;! ! for (int i = 1; i < seq.length; i++) {! !! seq[i] = seq[i-1] + i;! ! }! }! sequence!
seq! 0 1 3 6 10 15 21 28 36 45 ae2f ae2f ae2f
! int[] sequence = new int[10];! ! fillAllSums(sequence);! ! …! }! ! public static void fillAllSums(int[] seq) {! ! seq[0] = 0;! ! for (int i = 1; i < seq.length; i++) {! !! seq[i] = seq[i-1] + i;! ! }! }!
0 1 3 6 10 15 21 28 36 45 Changes to an array in a method are visible outside the method! sequence! ae2f ae2f
public static int[] copy(int[] data){! if (data == null) return null;! int[] data2 = new int[data.length];! for (int i = 0; i < data.length; i++){! data2[i] = data[i];! }! return data2;! }! counts! null! Causes nullPointerException if data is null!