FAQ: 2D Arrays: Java - Declaration, Initialization, and Assignment

This community-built FAQ covers the “Declaration, Initialization, and Assignment” exercise from the lesson “2D Arrays: Java”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Declaration, Initialization, and Assignment

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1.

Declare a 2D array of float values called floatTwoD.

Wow, wow! Codecademy should’ve first introduced me to the float datatype, I don’t know what that is!

1 Like

public class Exercise2 {
public static void main(String args) {

	// Declare a 2d array of float values called floatTwoD
float[][] floatTwoD; // Since this is 2D. We can simply create the array with float which is the name of the datatype and 2 brackets infront followed by the name which is floatTwoD.


	// Initialize the 2d array from the last step to an empty 2d array consisting of 4 arrays with 10 elements each
 floatTwoD = new float[4][10]; // We have already initialize floatTwoD so there is no need to create it again, we can simply instantiate this with a new float datatype that has 4 rows and 10 columns.
	

	// Declare and initialize an empty 2d array of integers consisting of 15 rows and 8 columns called dataChart
int[][] dataChart = new int[15][8]; Since this has a different datatype which is an integer. Lets rewrite the whole 2D array again with the given name and the new datatype and assign the value of row which is 15 and columns which has 8 values.

	
	// Create a 2D char array called ticTacToe representing the provided tic-tac-toe board. Use the characters 'X', 'O', and ' '.
char[][] ticTacToe = new char[][] {{'X', 'O', 'O'},{'O', 'X', '  '}, {'X', '  ', 'X'}}; //First note this is a char datatype. So create your instance and  followed by your characters as it shown on the board; 



	
	// When no one is looking, you want to modify the game to where you, 'O', wins the game. Replace the game board so that all X’s are O’s and all O’s are X’s. Do this in one line with initializer lists.


ticTacToe = new char[][]{{'O', 'X', 'X'},{'X', 'O', ' '}, {'O', ' ', 'O'}}; //Simply change your X values to O and O to X. So far as your using new here this will create a different Tictactoe with O will be the wins.


}

}

2 Likes

Hello,

I’m stuck to the last exercise : " When no one is looking, you want to modify the game so 'O' wins. Replace the game board so that all X’s are O’s and all O’s are X’s. Do this in one line with initializer lists. Do not declare ticTacToe again."

Create a 2D char array called ticTacToe representing the provided tic-tac-toe board. Use the characters ‘X’, ‘O’, and ’ '.
char ticTacToe = new char { {‘X’, ‘O’, ‘O’}, {‘O’, ‘X’, ’ '}, {‘X’, ’ ', ‘X’} };

When no one is looking, you want to modify the game to where you, ‘O’, wins the game. Replace the game board so that all X’s are O’s and all O’s are X’s. Do this in one line with initializer lists.

ticTacToe = new char[][] { {'X', 'O', 'O'}, {'O', 'X', ' '}, {'X', ' ', 'X'} };

I keep having this error message " Did you overwrite the value of ticTacToe with a new 2D char array on a new line?"

What should I do ?

Thanks

To preserve code formatting in forum posts, see: How do I format code in my posts?

I don’t see where you have replaced the X’s with O’s and vice versa. You created the ticTacToe array in Step 3. In Step 4, you are supposed to exchange the X’s and O’s.
For Example {'O', 'X', ' '} from Step 3 will become {'X', 'O', ' '} in Step 4.

1 Like

I am not able to find solution
Also the Solution Panel is not working
How to declare this 2D array?

I think you have to use capital letters (uppercase) 'X' and 'O'.

what does a column represent in 2D array