1.3) How Java Stores 2D Arrays
String[][] adungeon;
String[][] adungeon = new String[3][4];
String[][] adungeon = {
{"trap", "spider", "treasure"},
{"rat", "gems", "spiderweb"},
{"treasure", "treasure", "gargoyle"},
{"goblin", "pit", "oil lamp"}
}
System.out.println( adungeon[2][1] );
adungeon[2][1] = "peppo"; // row 2, column 1
System.out.println( adungeon[2][1] );
treasure peppo
for (int i=0; i < adungeon.length; i++){
for (int j=0; j < adungeon[i].length; j++){
System.out.print(adungeon[i][j] + " ");
}
System.out.println();
}
trap spider treasure rat gems spiderweb treasure peppo gargoyle goblin pit oil lamp
2.1) Nested Loops for 2D Arrays
2.2) Getting the Number of Rows and Columns
2.3) Looping Through a 2D Array
System.out.printf(format, parameters, ...)
%s
is a placeholder for strings %-10s
justifies string left with 10 characters width%d
is a placeholder for integers%f
is a placeholder for floating point numbers%-10.2f
justifies left with 10 characters width and 2 decimalsSystem.out.println("Item Quantity Price");
System.out.printf("%-10s%-10d%-10.2f\n", "Cigars", 5, 13.40);
System.out.println();
Item Quantity Price Cigars 5 13.40
for (int i=0; i < adungeon.length; i++){
String[] row = adungeon[i];
for (int j=0; j < row.length; j++){
System.out.printf("%-10s", row[j]);
}
System.out.println();
}
trap spider treasure rat gems spiderweb treasure peppo gargoyle goblin pit oil lamp
for (String[] row : adungeon){
for (String cell : row){
System.out.printf("%-10s", cell);
}
System.out.println();
}
trap spider treasure rat gems spiderweb treasure peppo gargoyle goblin pit oil lamp
String[][] adungeon = {
{"trap", "spider", "treasure"},
{"rat", "gems", "spiderweb"},
{"treasure", "treasure", "gargoyle"},
{"goblin", "pit", "oil lamp"}
}
adungeon
contain some treasure?adungeon
contains?List<List<String>> lsdungeon = List.of(List.of("trap", "spider", "treasure"),
List.of("rat", "gems", "spiderweb"),
List.of("treasure","treasure", "gargoyle"),
List.of("goblin", "pit", "oil lamp"));
// var : type inference (since Java 10)
var lsdungeon = List.of(List.of("trap", "spider", "treasure"),
List.of("rat", "gems", "spiderweb"),
List.of("treasure","treasure", "gargoyle"),
List.of("goblin", "pit", "oil lamp"));
System.out.println(lsdungeon.get(0));
[trap, spider, treasure]
System.out.println(lsdungeon.get(1));
[rat, gems, spiderweb]
System.out.println(lsdungeon.get(1).get(2));
spiderweb
System.out.println(lsdungeon);
[[trap, spider, treasure], [rat, gems, spiderweb], [treasure, treasure, gargoyle], [goblin, pit, oil lamp]]
System.out.println(lsdungeon.get(2));
[treasure, treasure, gargoyle]
for (List<String> row : lsdungeon){
for (String cell : row){
System.out.printf("%-10s", cell);
}
System.out.println();
}
trap spider treasure rat gems spiderweb treasure treasure gargoyle goblin pit oil lamp
Does lsdungeon
contains some treasure?
var lsdungeon = List.of(List.of("trap", "spider", "treasure"),
List.of("rat", "gems", "spiderweb"),
List.of("treasure","treasure", "gargoyle"),
List.of("goblin", "pit", "oil lamp"));
int i = 0;
boolean found = false;
while (i < lsdungeon.size() && !found){
found = lsdungeon.get(i).contains("treasure");
i++;
}
System.out.println("found:" + found);
found:true
How many treasures does lsdungon
contains?
var lsdungeon = List.of(List.of("trap", "spider", "treasure"),
List.of("rat", "gems", "spiderweb"),
List.of("treasure","treasure", "gargoyle"),
List.of("goblin", "pit", "oil lamp"));
int count = 0;
for (List<String> row : lsdungeon){
count += Collections.frequency(row, "treasure");
}
System.out.println(count);
3
At which coordinates (row, column) is the first treasure in each row?
var lsdungeon = List.of(List.of("trap", "spider", "treasure"),
List.of("rat", "gems", "spiderweb"),
List.of("treasure","treasure", "gargoyle"),
List.of("goblin", "pit", "oil lamp"));
int count = 0;
for (int i = 0; i < lsdungeon.size(); i++){
int j = lsdungeon.get(i).indexOf("treasure");
if (j != -1){
System.out.println("Found a treasure at row " + i + " and column " + j);
}
}
Found a treasure at row 0 and column 2 Found a treasure at row 2 and column 0
Using Monster
defined as in classes slides
Note for us here a healty cerberus is a monster with the name
"Cerberus" and exactly 7 lifepoints.
Monster cerberus1 = new Monster();
cerberus1.name = "Cerberus";
cerberus1.lifepoints = 7;
Monster cerberus2 = new Monster();
cerberus2.name = "Cerberus";
cerberus2.lifepoints = 7;
Monster zombo = new Monster();
zombo.name = "Zombo";
zombo.lifepoints = 4;
List<List<Monster>> mdungeon = List.of(List.of(cerberus1, cerberus1, zombo),
List.of(zombo, cerberus2, cerberus1),
List.of(zombo, cerberus1, zombo),
List.of(cerberus1, zombo, cerberus2));
Does mdungeon
contain some healthy cerberus?
var mdungeon = List.of(List.of(cerberus1, cerberus1, zombo),
List.of(zombo, cerberus2, cerberus1),
List.of(zombo, cerberus1, zombo),
List.of(cerberus1, zombo, cerberus2));
int i = 0;
boolean found = false;
while (i < mdungeon.size() && !found){
found = mdungeon.get(i).contains(cerberus1);
i++;
}
System.out.println("found:" + found);
found:true
How many healthy cerberus does mdungeon
contain?
var mdungeon = List.of(List.of(cerberus1, cerberus1, zombo),
List.of(zombo, cerberus2, cerberus1),
List.of(zombo, cerberus1, zombo),
List.of(cerberus1, zombo, cerberus2));
int count = 0;
for (var row : mdungeon){
count += Collections.frequency(row, cerberus1);
}
System.out.println(count);
7
At which coordinates (row, column) is the first healthy cerberus in each row?
var mdungeon = List.of(List.of(cerberus1, cerberus1, zombo),
List.of(zombo, cerberus2, cerberus1),
List.of(zombo, cerberus1, zombo),
List.of(cerberus1, zombo, cerberus2));
int count = 0;
for (int i = 0; i < mdungeon.size(); i++){
int j = mdungeon.get(i).indexOf(cerberus1);
if (j != -1){
System.out.println("Found a healthy cerberus at row " + i + " and column " + j);
}
}
Found a healthy cerberus at row 0 and column 0 Found a healthy cerberus at row 1 and column 1 Found a healthy cerberus at row 2 and column 1 Found a healthy cerberus at row 3 and column 0
All classes inherits from class Object
, so we can have a mixed list of generic objects:
List<List<Object>> mixdungeon = List.of(List.of(cerberus1, "oil lamp", zombo),
List.of(zombo, cerberus2, cerberus1),
List.of("spiderweb",cerberus1, zombo),
List.of(cerberus1, zombo, cerberus2));
We can also omit Object
in the type:
List<List> mixdungeon = List.of(List.of(cerberus1, "treasure", zombo),
List.of(zombo, cerberus2, cerberus1),
List.of("treasure", cerberus1, zombo),
List.of(cerberus1, "treasure", "treasure"));
var mixdungeon = List.of(List.of(cerberus1, "treasure", zombo),
List.of(zombo, cerberus2, cerberus1),
List.of("treasure", cerberus1, "oil lamp"),
List.of(cerberus1, "treasure", "treasure"));
Object obj = cerberus1;
//Object obj = cerberus2;
//Object obj = "treasure";
mixdungeon
contain some obj
?obj
does mixdungeon
contain?obj
in each row?