Say I have the 2D array
X X X O O O
O O O O O O
O O O O O O
X X X X O O
O O O O O O
O O O O O O
I want to count the number of X's recurring within the 2D array. In this case it should be 7. I want to return a true/false boolean value. How would I go about doing this?
CodePudding user response:
You could probably use Lambda expression for this. But, for you might be easier to understand iterating over your 2D array and collect the count in a map.
public class CountDemo {
public static void main(String[] args) {
char[][] matrix = {
{ 'X', 'X', 'X', 'O', 'O', 'O' },
{ 'O', 'O', 'O', 'O', 'O', 'O' },
{ 'O', 'O', 'O', 'O', 'O', 'O' },
{ 'X', 'X', 'X', 'X', 'O', 'O' },
{ 'O', 'O', 'O', 'O', 'O', 'O' },
{ 'O', 'O', 'O', 'O', 'O', 'O' }};
Map<Character, Integer> charCount = new HashMap<>();
for (char[] characters : matrix) {
for (char character : characters) {
Integer count = charCount.get(character);
if (count == null) {
charCount.put(character, 1);
} else {
charCount.put(character, count);
}
}
}
System.out.println("Character count");
System.out.println("X's: " charCount.get('X'));
System.out.println("O's: " charCount.get('O'));
}
}
The output of this program is
Character count
X's: 7
O's: 29
The explanation of the code:
The outer for-loop iterates over each array of characters while the inner loop iterates over the characters in the current array.
Once you have a character, you will "get" the current (count) value for the given key (character). If no matched character is found, the get() function will return null. In that case, you found the first instance of that character; so you just enter the value of "1" for the count and save it in the map. Otherwise, you increment the current count and you put back the updated count in the map.
Once you finish iterating over the matrix, you are ready to display the stats. Since I knew this example was just 'X' and 'O', I just called the get() directly. When you don't know the contents of the map, the correct solution is to iterate over the map's entries and get whatever is there. So, the System.out statements in the code above should be replaced with the following code snippet:
Iterator<Map.Entry<Character, Integer>> iter = charCount.entrySet().iterator();
System.out.println("Character count");
while(iter.hasNext()) {
Entry<Character, Integer> entry = iter.next();
System.out.println(entry.getKey() "'s: " entry.getValue());
}
If you were to change your character matrix as follows
char[][] matrix = {
{ 'X', 'X', 'X', 'O', 'E', 'O' },
{ 'O', 'O', 'O', 'O', 'O', 'O' },
{ 'E', 'O', 'O', 'O', 'O', 'O' },
{ 'X', 'X', 'X', 'X', 'E', 'O' },
{ 'O', 'O', 'O', 'O', 'O', 'O' },
{ 'O', 'O', 'O', 'O', 'E', 'O' }};
The output of the program using the EntrySet solution will be as follows
Character count
E's: 4
X's: 7
O's: 25
CodePudding user response:
The most basic solution would be, to have a Methode, that needs a 2D-Array and the value that you are expecting. Then you iterate through the array using two for-loops. Every time you find an x, you add plus one onto an int variable. After you iterated through the 2D-Array, you compare that counter with the expected value and return a true if the numbers are the same and a false if this is not the case. Something like this:
public boolean countX (char[][] array, int expectation){
int counter = 0;
for (int i = 0; i < array.length; i ) {
for (int j = 0; j < array[i].length; j ) {
if(array[i][j] == 'x'){
counter ;
}
}
}
return counter == expectation;
}
