You are on page 1of 3

NAMA : Arkan Niko Sarajiva (22520241013)

KELAS: F2

1. we have a list of items :


device 1 = "smartphone"
device 2 = "television"
device 2 = "computer"

a. please create an array containing device types !


b. please write the output of each index ! (e.g.: devices[0]=smartphone, etc)

Answer :
a. Certainly! In Java, an array can be created using square brackets [] after the data type, like
this:
public class Main {
public static void main(String[] args) {
String[] devices = {"smartphone", "television", "computer"};

System.out.println(devices[0]);
System.out.println(devices[1]);
System.out.println(devices[2]);
}
}
This will create a String array named devices containing the three device types. You can access
the elements of the array using the index, like devices[0] for "smartphone", devices[1] for
"television", and so on.
b. System.out.println(devices[0]);
System.out.println(devices[1]);
System.out.println(devices[2]);
2. we have data for 4 days can be presented as a two dimensional array as below using java :
day 1 - 22 12 5 2
day 2 - 15 6 10 20
day 3 - 10 8 12 5
day 4 - 12 15 8 6

a. the above data can be presented as two dimensional array as ...


b. please write the output of each indexl (e.g.: days[0][0]=22, etc)
c the output of days [1][2]= ...
d. the output of days [0]= ...
Answer
a. int[][] data = {{22, 12, 5, 2}, {15, 6, 10, 20}, {10, 8, 12, 5}, {12, 15, 8, 6}};
This creates a 4 by 4 integer array named data, where each row represents a day, and each
column represents a data point for that day. For example, data[0][1] would give you the value 12,
which is the second data point for the first day.
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
System.out.print(data[i][j] + " ");
}
System.out.println();
}
b.
days[0][0] Output: 22
days[0][1] Output: 12
days[0][2] Output: 5
days[0][3] Output: 2
days[1][0] Output: 15
days[1][1] Output: 6
days[1][2] Output: 10
days[1][3] Output: 20
days[2][0] Output: 10
days[2][1]) Output: 8
days[2][2] Output: 12
days[2][3] Output: 5
days[3][0] Output: 12
days[3][1] Output: 15
days[3][2]) Output: 8
days[3][3] Output: 6

c.
days[1][2] Output: 10

d.
days[0]) Output: [22, 12, 5, 2]

You might also like