Arrays - Lists: Java Programming
Arrays - Lists: Java Programming
Java programming
▶ Model:
A list can be seen as a binder.
▶ You can browse it, look in, remove or add sheets.
▶ The binder can be empty, but the binder is still there.
▶ A model can be seen as a sequence of elements:
√
(♣, , ♠, ♯, ♣, ♭)
▶ The parenthesis is not part of the list, but rather represents the list itself.
▶ It shows the start and end of the list.
30 int age;
45 53 64 12 33 12 Integer array
0 1 2 3 Length - 2 Length - 1 Element index
▶ Here an array of length seven is created that can contain only decimal numbers.
▶ The different elements in the array can be accessed using the index:
rain[0] = 22.5;
▶ Notice that it is up to you as a programmer to make sure you are within the index
range when you read or set the values.
rain[0] = 22.5;
rain[1] = 3.4;
rain[2] = 10.5;
rain[3] = 0.3;
rain[4] = 3.14; Output:
rain[5] = 2.3;
rain[6] = 22.6;
0.3
System.out.println(rain[3]); The avarage rain fall is 9.248571428571427
▶ An array can be created for any data type, including classes (as we will look more
into later).
▶ If, at the time of creation, the elements are know, the array can be initiated directly.
▶ Put the initiated values in curly brackets at the initiation.
▶ Notice that the size is not needed when initiating with values, as it can be deduced
by the number of values.
int[] siffror = { 1, 2, 3, 4, 5, 6};
▶ The length of an array is given by the property length (as seen in the previous
example).
▶ One of the most common errors for arrays is ArrayIndexOutOfBoundsException
▶ One key insight is that the last index is always one less than the size:
max_index = array_name.length - 1;
▶ To fully understand how arrays work, we recommend that you try to do exercises
like calculating the sum, finding the largest, shift position and many more.
System.out.println();
▶ There is another, simpler, form of iteration for arrays (and other lists).
▶ It is called foreach iteration even if the keyword is for…
▶ Example:
for(double s: snow) {
sum += s;
}
for(double s: snow) {
sum += s;
}
for(String s: sentence1)
System.out.print(s + " ");
System.out.println();
for(String s: sentence1)
System.out.print(s + " ");
System.out.println();
for(String s: sentence2)
System.out.print(s + " ");
}
}
Department of Computer Science
Arrays – lists 18(26)
Ways to copy an array
▶ If you really want to do a value copy of an array, there are three ways to do that:
▶ Copy each element.
▶ Use copyOf in Arrays.
▶ the method clone.
▶ Next example will show how to copy each element manually.
▶ You should also have a closer look at copyOf.
▶ Another, not as recommended way, is to use arraycopy in System.
for(char c: greek){
System.out.print(c);
}
System.out.println();
for(int i = 0; i < greek.length; i++) {
Zeus
nordic[i] = greek[i];
}
Zeus
for(char c: nordic) {
System.out.print(c);
Oden
}
System.out.println();
Zeus
nordic[0] = 'O'; nordic[1] = 'd'; nordic[2] = 'e'; nordic[3] = 'n';
for(char c: nordic) {
System.out.print(c);
}
System.out.println();
for(char c: greek){
System.out.print(c);
}
}
}
▶ In this case, a new array is created with three rows with two columns – in each
“cell” there is an int.
▶ Notice that in Java it is allowed, but not recommended, to put the brackets with the
variable instead of with the data type.
array2D[0][0] = 8;
array2D[0][1] = 16;
array2D[1][0] = 32;
array2D[1][1] = 64; 8 16
array2D[2][0] = 128; 32 64
array2D[2][1] = 512;
128 512
for(int i = 0; i < array2D.length; i++) {
for (int j = 0; j < array2D[i].length; j++) {
System.out.print(array2D[i][j] + " ");
}
System.out.println();
}
}
}
System.out.println("Monday\tTuesday\tW-day\tT-day\tFriday\tS-day\tSunday");