Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Creating multiple arraylists with a loop?

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Creating multiple arraylists with a loop?

    What you need to do it create a 2D array list.

    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);
     }

    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

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Creating multiple arraylists with a loop?

    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

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

     
    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?

Similar Threads

  1. Loop not creating objects
    By xecure in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 10:48 PM
  2. Encapsulation question regarding ArrayLists
    By LDM91 in forum Collections and Generics
    Replies: 3
    Last Post: October 27th, 2010, 11:51 AM
  3. Transferring elements between arraylists
    By KipTheFury in forum Collections and Generics
    Replies: 6
    Last Post: August 23rd, 2010, 02:13 PM
  4. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM
  5. [SOLVED] How to dynamically create ArrayLists (or something similar)?
    By igniteflow in forum Collections and Generics
    Replies: 4
    Last Post: August 6th, 2009, 06:00 AM