External Exam Download Resources Web Applications Games Recycle Bin

two dimensional array


for any problem that requires a list within a list:
String[][] myGrid = new String[2][2];
    myGrid[0][0] = "A1";
    myGrid[0][1] = "Battleship";
    myGrid[1][0] = "B1";
    myGrid[1][1] = "B2";
which is the same as:
String[][] myGrid = new String[][]{{"A1", "Battleship"},{"B1", "B2"}};
so this will print out "Battleship":
System.out.println(myGrid[0][1]);

timetable as a two dimensional array
public class timeTable {
    public static void main(String[] args) { 
      int totalDays = 2;
      int totalLessons = 3;
      int englishLessonCount = 0;
      
      String[][] timetable = new String[totalDays][totalLessons];
      timetable[0][0] = "Maths";
      timetable[0][1] = "English";
      timetable[0][2] = "Science";
      timetable[1][0] = "History";
      timetable[1][1] = "English";
      timetable[1][2] = "Religion";
      
      for(int today = 0; today < totalDays; today++)
      {
         for(int thisLesson = 0; thisLesson < totalLessons; thisLesson++)
         {
           if(timetable[today][thisLesson].equals("English"))
           {
             englishLessonCount++;
           }
         }
      }
      System.out.println("english lesson count: " + englishLessonCount);
    }
}