Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:17:45+00:00 2026-06-15T17:17:45+00:00

I am trying to initialize a 2D array, in which the type of each

  • 0

I am trying to initialize a 2D array, in which the type of each element is char.
So far, I can only initialize this array in the follow way.

public class ticTacToe 
{
private char[][] table;

public ticTacToe()
{
    table[0][0] = '1';
    table[0][1] = '2';
    table[0][2] = '3';
    table[1][0] = '4';
    table[1][1] = '5';
    table[1][2] = '6';
    table[2][0] = '7';
    table[2][1] = '8';
    table[2][2] = '9';
}
}

I think if the array is 10*10, it is the trivial way.
Is there any efficient way to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:17:46+00:00Added an answer on June 15, 2026 at 5:17 pm

    How about something like this:

    for (int row = 0; row < 3; row ++)
        for (int col = 0; col < 3; col++)
            table[row][col] = (char) ('1' + row * 3 + col);
    

    The following complete Java program:

    class Test {
        public static void main(String[] args) {
            char[][] table = new char[3][3];
            for (int row = 0; row < 3; row ++)
                for (int col = 0; col < 3; col++)
                    table[row][col] = (char) ('1' + row * 3 + col);
    
            for (int row = 0; row < 3; row ++)
                for (int col = 0; col < 3; col++)
                    System.out.println (table[row][col]);
        }
    }
    

    outputs:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    

    This works because the digits in Unicode are consecutive starting at \u0030 (which is what you get from '0').

    The expression '1' + row * 3 + col (where you vary row and col between 0 and 2 inclusive) simply gives you a character from 1 to 9.

    Obviously, this won’t give you the character 10 (since that’s two characters) if you go further but it works just fine for the 3×3 case. You would have to change the method of generating the array contents at that point such as with something like:

    String[][] table = new String[5][5];
    for (int row = 0; row < 5; row ++)
        for (int col = 0; col < 5; col++)
            table[row][col] = String.format("%d", row * 5 + col + 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to initialize an array in C and for each element GCC
Can I declare an int array, then initialize it with chars? I'm trying to
according to this post and from OpenCV documentation, I can initialize and access each
I am trying to store an array of classes which I can initialise later
I am trying to initialize an empty array which itself contains 5 empty arrays.
How do I initialize a 2D array in Perl? I am trying the following
I am trying to initialize a vector which has integers 1 to n in
I'm trying to initialize string with iterators and something like this works: ifstream fin(tmp.txt);
do you know how I can initialize the variable ret below ? type ReferenceDataResponse
I am declaring an array of void pointers. Each of which points to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.