Cody Lee
Cody Lee

Reputation: 29

The use of char arrays in java

I am currently working on a Tic Tac Toe assignment using java. They asked me to use char array in it, but I am not sure about how to use char arrays, and what kind of values does it store inside?

Upvotes: 0

Views: 183

Answers (2)

Vince
Vince

Reputation: 15146

char arrays store multiple char values.

A char is a single character (letter/number/symbol) wrapped with single quote marks: 'L'.

To create an array variable, specify the type of the array, followed by [], followed by an identifier (name).

char[] myArray;

Initializing an array is somewhat similar to initializing an object: use the new keyword, specify the type of the array, then specify how many array indexes (how many valuws the array can store) wrapped in [ ]

 char[] myArray = new char[10]; //10 indexes; 0 to 9

To store a value in the array, access an index by specifying the array name followed by the index wrapped in [ ]:

 myArray[0] = 'c';

Accessing the value is similar:

char letter = myArray[0];

You can also specify values when initializing the array:

char[] myArray = { 'c', 'a' };

Upvotes: 2

Rakesh
Rakesh

Reputation: 91

You can declare array of characters as char[] anArrayOfChars; Refer this [link] (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) for more details. Also, here's [a link] (http://www.coderslexicon.com/a-beginner-tic-tac-toe-class-for-java/) for sample code for tic-tac-toe implemented in Java.

Upvotes: 0

Related Questions