Results 1 to 9 of 9
  1. #1
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default Help removing items from an Arraylist and placing into a 2D Array

    I need to remove 52 RANDOM elements from my arraylist and place them into a 2D array with 6 rows and 9 columns. This is my code so far.

    Java Code:
    private Shoe[][] getRandomShoesArray() {
    		ArrayList<Shoe> shoes = createTheShoes();
    		Shoe [][] randomShoes;
    		randomShoes = new Shoe[6][9];
    		for(int row=0; row < NUMBER_OF_ROWS; row++) {
    			for(int col=0; col < NUMBER_OF_COLS; col++) {
    				for(int i=0;i<shoes.size();i++) {
    					randomShoes[row][col] = shoes.get(i);
    				}	
    			}	
    		}		
    		return null;
    	}
    I don't think is correct as I only want 52 random shoes but I can't think of which way the loops should go. Also how would I RANDOMLY remove them, Math.random() is the only random method we have learnt. Also in the last 4 rows there would be four blank spaces that can be filled up with null but am not sure how to do this...

    Any help appreciated thanks!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Quote Originally Posted by david522 View Post
    I don't think is correct as I only want 52 random shoes
    Then I would probably use a loop that iterates 52 times.
    but I can't think of which way the loops should go.
    In that case step away from the computer, grab a pen and some paper and work out how you would do it by hand.
    Also how would I RANDOMLY remove them,
    The ArrayList class has methods to help you do this.
    Math.random() is the only random method we have learnt.
    You can use Math.random but since that returns a floating point number between 0 and 1 you need to scale it up to 0 - 52. Or you can learn how to use the Random class.
    Also in the last 4 rows there would be four blank spaces that can be filled up with null but am not sure how to do this...
    The default value in an object array is null. If you do not add anything to the array at a certain location then it will be null. You don't need to do anything.

  3. #3
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    I am just a novice as well, but i think that your on the correct track with the random idea. I would just be worried about having random 0-52 come up the same number twice. Youll need some way to store the values generated by random, compare new randoms to the stored values before placing the number into the new array so that there are no repeats.

    at least that is how i would approach it.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Quote Originally Posted by Junky View Post
    You can use Math.random but since that returns a floating point number between 0 and 1 you need to scale it up to 0 - 52.
    I just realised that should be 0 - size of the list. Which leads to the question what happens if there isn't 52 objects in the List?

  5. #5
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default

    The number is set as 52 by a final int constant.
    I've been thinking of how to store the number that has already been used but am having difficulty seeing how i could be compared to itself...
    I was thinking of trying this
    Java Code:
    for(int row=0; row < NUMBER_OF_ROWS; row++) {
    			for(int col=0; col < NUMBER_OF_COLS; col++) {
    				for(int i=((int)(Math.random() * TOTAL_NUMBER_OF_SHOES));i<=TOTAL_NUMBER_OF_SHOES;i++) {
    					int stored = i;
    					if(i = i){
    						.........
    					} 
    					randomShoes[row][col] = pack.get(i);
    				}	
    			}	
    		}
    I still do not understand how I would go about looping it through 52 times while looping the rows/columns as a true 2D array must have 2 loops?
    Last edited by david522; 08-17-2011 at 03:36 AM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    I still do not fully understand what you are doing. If all you want to do is take 52 Objects from the List and put them into a 2D array then you only need a single loop.
    Java Code:
    loop 52 times {
        generate random number n;
        get Shoe from List at index n;
        add Shoe object to 2D array at position [x][y];
        update values for x & y (if needed);
    }
    Working out what values x and y should be is the only ticky part.
    Last edited by Junky; 08-17-2011 at 04:27 AM.

  7. #7
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default

    Ok so I'm trying to follow your structure but I'm not too sure if this is correct. If you could give me a few further hints that would be great. Really appreciate all your help!

    Also I need someway of storing the values I have already used but am figuring that out now... EDIT : I think I just have to remove the elemant from the array list as I use it.

    Java Code:
    private Shoe[][] getRandomShoesArray() {
    		ArrayList<Shoe> shoe = createTheShoe();
    		Shoe [][] randomShoe;
    		randomShoes = new Shoe[6][9];
                    int x = 0;
                    int y = 0;
    		for(int i=0; i <= TOTAL_NUMBER_OF_SHOES; i++) {
    			int random = (int)(Math.random() * TOTAL_NUMBER_OF_SHOES);
    			Shoe placement = pack.get(random);
    			randomShoes[x][y] = placement;
                            if (x < 6) {
                               x++
                            }
                           if (y < 9) {
                              y++
                           }
    		}
    		return null;
    	}
    Last edited by DarrylBurke; 08-17-2011 at 10:18 AM. Reason: Removed unnecessary vertical whitespace

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    if you were to hard code accessing the elements of a 2D array then it would be:
    Java Code:
    arr[0][0]
    arr[0][1]
    arr[0][2]
    arr[0][3]
    arr[0][4]
    arr[0][5]
    arr[0][6]
    arr[0][7]
    arr[0][8]
    arr[1][0]
    arr[1][1]
    arr[1][2]
    Can you see a pattern?

  9. #9
    david522 is offline Member
    Join Date
    May 2011
    Posts
    21
    Rep Power
    0

    Default

    Ah cheers, thanks for pointing out the basics to me! Have been playing around with it for hours but I'm fairly certain I'm on the right track now.
    Java Code:
    private Shoe[][] getRandomShoesArray() {
    		ArrayList<Shoe> shoe = createTheShoe();
    		Shoe [][] randomShoes;
    		int x = 0;
            int y = 0;
    		randomShoes = new Shoe[6][9];
    		for (int i = 0; i < TOTAL_NUMBER_OF_SHOES; i++) {
    				int random = ((int)Math.random() * shoe.size());
    				Shoe placement = shoe.get(random);
    				shoe.remove(random);
    				randomShoes[x][y] = placement;
    				if (y == 8 && x < 5) {
    					x++;
    					y = 0;
    				}
    				if (y < 8 && x < 5) {
    					y++;				
    				}		
    		}		
    				
    		return randomShoes;
    	}
    Last edited by david522; 08-18-2011 at 01:54 AM.

Similar Threads

  1. [HELP] Removing objects from an ArrayList!
    By bpx95 in forum New To Java
    Replies: 4
    Last Post: 04-30-2011, 05:54 AM
  2. Removing Items In a GUI
    By 67726e in forum New To Java
    Replies: 3
    Last Post: 08-01-2010, 05:46 PM
  3. Removing from an ArrayList while looping thru
    By pahiker in forum New To Java
    Replies: 7
    Last Post: 06-22-2010, 07:38 AM
  4. Adding \ removing items from Jlist
    By Desperado in forum AWT / Swing
    Replies: 10
    Last Post: 12-17-2009, 12:48 PM
  5. Replies: 12
    Last Post: 11-05-2009, 07:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •