UF 06: JAVA¶

Matrices¶

Alta Formazione Professionale - ITT Marconi

March 2021

8.1 Two-dimensional (2D) Arrays¶

1.1) 2D Arrays

row-major.png

1.2) Array Storage¶

1.3) How Java Stores 2D Arrays

arrarr.png

1.4) Declaring 2D Arrays¶

In [1]:
String[][] adungeon;
In [2]:
String[][] adungeon = new String[3][4];
In [3]:
String[][] adungeon = {
                            {"trap", "spider", "treasure"},
                            {"rat",  "gems",   "spiderweb"},
                            {"treasure",   "treasure",  "gargoyle"},
                            {"goblin", "pit",  "oil lamp"}
                        }

Arrays get / set value¶

In [4]:
System.out.println(  adungeon[2][1] );

adungeon[2][1] = "peppo";   // row 2, column 1

System.out.println(  adungeon[2][1] );
treasure
peppo

2 Traversing 2D Arrays (nested loops)¶

In [5]:
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

Some formatting example...¶

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 decimals
In [6]:
System.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     

Better 2D array printing¶

In [7]:
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  

2.4) Enhanced For-Each Loop for 2D Arrays¶

In [8]:
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  

2.5) 2D Array Algorithms¶

In [9]:
String[][] adungeon = {
                           {"trap", "spider", "treasure"},
                           {"rat",  "gems",   "spiderweb"},
                           {"treasure",   "treasure",  "gargoyle"},
                           {"goblin", "pit",  "oil lamp"}
                      }
  • Does adungeoncontain some treasure?
  • How many treasures does adungeon contains?
  • At which coordinates (row, column) is the first treasure in each row?

List collections¶

In [10]:
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"));
In [11]:
// 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"));
In [12]:
System.out.println(lsdungeon.get(0));
[trap, spider, treasure]
In [13]:
System.out.println(lsdungeon.get(1));
[rat, gems, spiderweb]
In [14]:
System.out.println(lsdungeon.get(1).get(2));
spiderweb

Printing list dungeon¶

In [15]:
System.out.println(lsdungeon);
[[trap, spider, treasure], [rat, gems, spiderweb], [treasure, treasure, gargoyle], [goblin, pit, oil lamp]]
In [16]:
System.out.println(lsdungeon.get(2));
[treasure, treasure, gargoyle]
In [17]:
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  

List array algorithms¶

Does lsdungeoncontains some treasure?

In [18]:
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"));
In [19]:
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?

In [20]:
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"));
In [21]:
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?

In [22]:
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"));
In [23]:
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

Monsters dungeon¶

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.

In [25]:
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 mdungeoncontain some healthy cerberus?

In [26]:
var mdungeon = List.of(List.of(cerberus1,  cerberus1,  zombo),
                       List.of(zombo,      cerberus2,  cerberus1),
                       List.of(zombo,      cerberus1,  zombo),
                       List.of(cerberus1,  zombo,      cerberus2));
In [27]:
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?

In [28]:
var mdungeon = List.of(List.of(cerberus1,  cerberus1,  zombo),
                       List.of(zombo,      cerberus2,  cerberus1),
                       List.of(zombo,      cerberus1,  zombo),
                       List.of(cerberus1,  zombo,      cerberus2));
In [29]:
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?

In [30]:
var mdungeon = List.of(List.of(cerberus1,  cerberus1,  zombo),
                       List.of(zombo,      cerberus2,  cerberus1),
                       List.of(zombo,      cerberus1,  zombo),
                       List.of(cerberus1,  zombo,      cerberus2));
In [31]:
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

Mixed dungeon¶

All classes inherits from class Object, so we can have a mixed list of generic objects:

In [32]:
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:

In [33]:
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"));

Mixed dungeon algorithms¶

In [34]:
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"));
In [35]:
Object obj = cerberus1;
//Object obj = cerberus2;
//Object obj = "treasure";
  • Does mixdungeoncontain some obj?
  • How many obj does mixdungeon contain?
  • At which coordinates (row, column) is the first obj in each row?