Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java Exercises: Print the contents of a two-dimensional Boolean array in specific format

Java Basic: Exercise-154 with Solution

Write a Java program to print the contents of a two-dimensional Boolean array where t will represent true and f will represent false.

Sample array:
array = {{true, false, true},
{false, true, false}};

Pictorial Presentation:

Java Basic Exercises: Print the contents of a two-dimensional Boolean array in specific format.

Sample Solution:

Java Code:

import java.util.Scanner;
public class Solution {
	public static void main(String[] args) {
		boolean[][] array = {{true, false, true},
							 {false, true, false}};
		int rows_length = array.length;
		int columns_length = array[0].length;
		
		for (int i = 0; i < rows_length; i++) {
			for (int j = 0; j < columns_length; j++) {
				
				if (array[i][j]) {
					System.out.print(" t ");
				} else {
					System.out.print(" f ");
				}
				
			}
			System.out.println();
		}	
    }
}

Sample Output:

 t  f  t 
 f  t  f 

Flowchart:

Flowchart: Java exercises: Print the contents of a two-dimensional Boolean array in specific format.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program that accepts two double variables and test if both strictly between 0 and 1 and false otherwise.
Next: Write a Java program to print an array after changing the rows and columns of a given two-dimensional array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe