Java and Back Again

Robert M Ricci
Apr 15 · 3 min read

A beginners tale…part 5

Photo by Nathan Dumlao on Unsplash

Welcome to my fifth and final post about my trip into learning java. In part one, I talked a little about the history and some basic principles. With part two, I went into state and instances, with some time explaining a little about object-oriented programming. Then in part three, we talked about arrays and ArrayLists. In part four, we got into encapsulation, and the different ways methods can be accessed. Now with part five, we are going to talk about 2D arrays.

Just to clarify what I mean by a 2D array, is basically just an array of arrays. Which are declared similarly to standard arrays. You will notice in the example below that a standard array is declared with a single bracket after the array type. Whereas a 2D is declared with two brackets after.

 int[] numbers;    //Standard array int[][] moreNumbers;    //2D array

Where things can get a little tricky is when you have to access an element within the array. In a standard array, you would enter an element index within a bracket after the name of the array. With a 2D array, you will need to enter a number into both brackets. The first one to pick the sub-array, the second for the element you want within that array.

int[][] numbers = {{1,2,3,4}, {5,6,7,8}};   int number = numbers[1][1];  //This would return 6. 

We have different ways of initializing and declaring 2D arrays. The first is that you can declare and initialize at the same time. The second is to declare and initialize on separate lines. You will need to use the new keyword if that is the route you take.

int[][] numbers = {{1,2,3,4}, {5,6,7,8}};  //Single line int[][] numbers;numbers = new Integer[][]{{1,2,3,4}, {5,6,7,8}};

Modifying a 2D array is just like modifying a standard array. You just pick what index you want to change and go from there.

int[][] numbers = {{1,2,3,4}, {5,6,7,8}};  numbers[0][1] = 10int[][] numbers = {{1,10,3,4}, {5,6,7,8}};  //This is what the array     
would look like after

The last thing I want to talk about involves traversing an array to find the data that you want. There are ways I want to discuss one is Row-Major and the other is Column-Major. Row-Major would start at the top left, and move left to right, then top to bottom. you can use a nested loop like the example below to iterate through them. The first loop goes through the rows, while the second goes through the columns.

for(int i = 0; i < numbers.length; i++) {
for(int j = 0; j < numbers[i].length; j++) {
System.out.println(numbers[i][j]);
}
}

The image below gives a nice visual of both Row major and column-major.

As you can see Column-major is slightly different. It starts in the same place, but it runs top to bottom, left to right. So, you would need your nested loop to be flipped.

for(int i = 0; i < numbers[0].length; i++) {
for(int j = 0; j < numbers.length; j++) {
System.out.println(numbers[j][i]);
}
}

CONCLUSION

I hope you enjoyed my journey through getting started with Java. I find it relatively easy to work with. It has a lot of similarities with other languages that I familiar with, which I found it easier to grasp some concepts.

Geek Culture

Proud to geek out. Follow to join our +1.5M monthly readers.