Creating multiple arraylists with a loop?
Hi, I am new to this forum and I am in desperate need of help. I am trying to simulate a boggle game with 16 dice. To import the dice, i created a text file of all the letters on all of the dice and created a scanner. I want to create 16 separate arraylists, or "dice", that contain the 6 letters, which are going to be chosen at random, or "rolled", at a later point.
So far I have got this, and i know it wont work.
Code java:
for(int d = 1;d<17;d++){
ArrayList<String> dice(d) = new ArrayList<String>();
for (int i = 0; i < 6; i++){
dice(d).add(in.next());
}
}
So inplace of the (d), i just want the value, or number, of d so in the end ill have dice1, dice2, dice3... etc.
What are my options?
please and thx
Re: Creating multiple arraylists with a loop?
What you need to do it create a 2D array list.
Code java:
ArrayList< ArrayList<String> > dice = new ArrayList< ArrayList<String> >();
for(int i = 0; i<16; i++){
ArrayList<String> die = new ArrayList<String>();
for (int j = 0; j < 6; j++){
die.add(in.next());
}
dice.add(die);
}
Code java:
dice.get(0).get(0); //return face 1 of dice 1
Although might I add that if you are using fixed sized things, i.e. you will always have 16 dice wth 6 faces, you'd be better of using a 2D array,
Chris
Re: Creating multiple arraylists with a loop?
Thx a bunch. this fixed most of my problems except for one.
When i try to do "dice.get(int1).get(int2)" Eclipse gives me an error that reads "The method get(int) is undefined for the type Object"
What do i do?
Re: Creating multiple arraylists with a loop?
Code java:
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList< ArrayList<Character> > dice = new ArrayList< ArrayList<Character> >();
for(int i = 0; i<16; i++){
ArrayList<Character> die = new ArrayList<Character>();
for (int j = 0; j < 6; j++){
die.add((char)(Math.random()*17 + 65));
}
dice.add(die);
}
for(int i = 0; i < dice.size(); i++){
System.out.print("Dice " + i + ": ");
for(int j = 0; j < dice.get(i).size(); j++){
System.out.print(dice.get(i).get(j) + " ");
}
System.out.println();
}
}
}
This works perfectly for me, perhaps you could provide more information about what it is you are doing?
Chris
Re: Creating multiple arraylists with a loop?
Ok so im going to show you all of my code... embarrassing as it may be. I know its not written very well, but im a novice so i guess whatever.
Code java:
import java.io.*;
import java.util.*;
public class Boggle {
public static void main(String[] args){
new Boggle();
}
public Boggle(){
ArrayList<ArrayList<Character>> dice = new ArrayList<ArrayList<Character>>();
// import the cubes
importCubes(dice);
/* THIS CHECKS IF EVERYTHING IS IMPORTED CORRECTLY
for(int d = 0; d<dice.size(); d++){
for(int l = 0; l<dice.get(d).size(); l++){
System.out.print(dice.get(d).get(l) + "\t");
}
System.out.println();
}
*/
// import dictionary
// roll the dice
Character[][] letterBoard = roll(dice);
for(int d = 0; d<letterBoard.length; d++){
for(int l = 0; l<letterBoard[d].length; l++){
System.out.print(letterBoard[d][l] + "\t");
}
System.out.println();
}
// find all the possible words
// cross check against dictionary
}
public void importCubes(ArrayList x){
Scanner in = null;
try {
in = new Scanner(new File("Dice.txt"));
}
catch (IOException i)
{
System.out.println("Error: " + i.getMessage());
}
for(int i = 0; i<16; i++){
ArrayList<Character> die = new ArrayList<Character>();
for (int j = 0; j < 6; j++){
die.add(in.next());
}
x.add(die);
}
}
// So the method roll with go through all the 16 spots on the 4x4 grid and pick a random
// 6 sided dice and put it in that spot, then "roll" it by picking at random one of it's sides and
// putting that into Character[][] board.
public Character[][] roll(ArrayList x){
Random rand = new Random();
int[] availability = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
Character[][] board = null;
for(int r = 0;r<4;r++){
for(int c = 0;c<4;c++){
for(int tries=0;tries<10000;tries++){
int d = rand.nextInt(16);
if(availability[d]!=0){
int l = rand.nextInt(6);
board[r][c] = x.get(d).get(l); // At this line, it underlined the second "get" in
availability[d] = 0; // red and it suggests to add cast to 'x.get(d)'
break;
}
}
}
}
return board;
}
}
I am trying to make a Boggle simulator that will be able to randomize(shake) the letters on the 4x4 grid and then find all the words that can be made. I am sure there are MANY things you could fix in this code to be more optimized, but i would like to figure it mostly for myself. Could you please help figure out what is broken?