Reputation: 542
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"};
Upvotes: 1
Views: 108
Reputation: 55
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).
Upvotes: 1
Reputation: 5654
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"};
Upvotes: 3
Reputation: 422
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
Upvotes: 3
Reputation: 40318
You can use array of arrays
String[][] arrays = new String[][] { array1, array2, array53};
Upvotes: 6