Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

How to create a 2D array and fill it with given 3 different characters symmetrically in Java

2 Answers

0 votes
public class Program {
    public static void main(String[] args) {
        int size = 6;
        char arr[][] = new char[size][size];

        char ch1 = '#';
        char ch2 = '$';
        char ch3 = '*';

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                // Fill the diagonals 
                if (i == j || (i + j) == (size - 1)) {
                    arr[i][j] = ch3;
                }
                else {
                    arr[i][j] = ch2;
                }
            }
        }

        for (int i = 0; i < size / 2; i++) {
            for (int j = i + 1; j < size - 1 - i; j++) {
                // Fill the upper positions
                arr[i][j] = ch1;

                // Fill the lower positions
                arr[size - 1 - i][j] = ch1;
            }
        }

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}




/*
run:

* # # # # * 
$ * # # * $ 
$ $ * * $ $ 
$ $ * * $ $ 
$ * # # * $ 
* # # # # * 

*/

 



answered Feb 16, 2024 by avibootz
0 votes
public class Program {
    
    public static void fill_array(char arr[][], int size, char ch1, char ch2, char ch3) {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                // Fill the diagonals 
                if (i == j || (i + j) == (size - 1)) {
                    arr[i][j] = ch3;
                }
                else {
                    arr[i][j] = ch2;
                }
            }
        }

        for (int i = 0; i < size / 2; i++) {
            for (int j = i + 1; j < size - 1 - i; j++) {
                // Fill the upper positions
                arr[i][j] = ch1;

                // Fill the lower positions
                arr[size - 1 - i][j] = ch1;
            }
        }
    }
    
    public static void main(String[] args) {
        int size = 6;
        char arr[][] = new char[size][size];

        fill_array(arr, size, '#', '$', '*');

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}




/*
run:
 
* # # # # * 
$ * # # * $ 
$ $ * * $ $ 
$ $ * * $ $ 
$ * # # * $ 
* # # # # * 
 
*/

 



answered Feb 16, 2024 by avibootz
...