Lately I have been learning Java so I have been looking for problems I can practice the basics on. I came across a problem that I thought was very interesting and fun to work on. The problem was to find the maximum number in a 2D array and also show the index of that number. I thought it would be good to show my process.
There is two steps to this problem. First we have to find the maximum number and then return the element’s index in the 2D array. I am going to create a method for each of the steps. I will call the first method maxNum. I will be creating variable that will represent the maximum number. I will use two for loops. This is what my first method looks like.
public class FindMaxAndIndex{
public int maxNum(int\[\]\[\] array) {
int max = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array\[i\].length; j++) {
if (array\[i\]\[j\] > max) {
max = array\[i\]\[j\];
}
}
}
return max;
}
}
The maxNum method will take a 2D array as an argument. Every time the value of the max variable is lower than one of the element’s the value will be replaced with that element. This is how we find the maximum value. Now we need to find the index of the maximum number. This is actually very similar to the maxNum method. The only difference is that we can iterate through the array and check if the element is equal to the maximum number. If so, we store the index values into an array and return it. This is what that method will look like.
public int\[\] arrIndex(int\[\]\[\] array) {
int\[\] indexArr = new int\[2\];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array\[i\].length; j++) {
if (array\[i\]\[j\] == maxNum(array)) {
indexArr\[0\] = i;
indexArr\[1\] = j;
}
}
}
return indexArr;
}
This is actually all we need! I am going to add a main method and output the value to the console. This is what the completed class looks like.
public class FindMaxAndIndex {
public int maxNum(int\[\]\[\] array) {
int max = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array\[i\].length; j++) {
if (array\[i\]\[j\] > max) {
max = array\[i\]\[j\];
}
}
}
return max;
}
public int\[\] arrIndex(int\[\]\[\] array) {
int\[\] indexArr = new int\[2\];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array\[i\].length; j++) {
if (array\[i\]\[j\] == maxNum(array)) {
indexArr\[0\] = i;
indexArr\[1\] = j;
}
}
}
return indexArr;
}
public static void main(String\[\] args) throws Exception {
int\[\]\[\] array = { {1, 4}, {8, 56}, {20, 7} };
FindMaxAndIndex app = new FindMaxAndIndex();
System.out.println("The maximum value in this 2D array is " + app.maxNum(array));
System.out.println("The position of the maximum value is \[" + app.arrIndex(array)\[0\] + ", " + app.arrIndex(array)\[1\] + "\]");
}
}
This problem is another great way to get used to iterating through arrays. I found it fun to solve and I learned from it. Happy coding! 😎
Top comments (2)
Hey there! I just read your post and I really liked it. However, I noticed that your code snippets seem a bit off. Here's a formatting guide that might help you with formatting them properly. Let me know if you have any questions or need further assistance. Keep up the great work!
Thanks so much for the formatting guide. I will be using this to make my posts better. I appreciate the help!