1

For example I'm making a hangman program and for the words I want to make an array called words and the items in words are the letters what ever word

This is what I thought you could do:

String[] words =  new String [15];
words[1] = String[] MILK = {"M","I","L","K"};
words[2] = String[] CLOTH = {"C","L","O","T","H"};
3
  • Is it possible to place array inside array? Commented Jun 6, 2013 at 18:34
  • You might consider using char arrays instead, e.g. {'M', 'I', 'L', 'K'}. Commented Jun 6, 2013 at 18:36
  • I thought JavaScript :) I better remove that... Commented Jun 6, 2013 at 18:38

4 Answers 4

6

You can use array of arrays

String[][] arrays = new String[][] { array1, array2,  array53};
Sign up to request clarification or add additional context in comments.

Comments

3

I think you are looking for:

String[][] words =  new String [15][];
String[] milk = words[0] = new String[] {"M","I","L","K"};
String[] cloth = words[1] = new String[] {"C","L","O","T","H"};

Comments

3

What you want is called a multidimensional array. These are basically arrays inside of arrays.

Your code should be as follows:

String[][] words =  new String [15][];
words[1] = new String[] {"M","I","L","K"};
words[2] = new String[] {"C","L","O","T","H"};

For documentation on it, see this website

Comments

1

What you will be using is a two dimensional array.

Think of it as a matrix that will look something like this

[][][][]
[][][][]
[][][][]
[][][][]

Where each box contains a letter.

You initialize by using the following code

String[][] words =  new String [x][y];

Where x is the total number of words (rows) and y is the total number of letters (columns).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.