Thread: OverWriting a int array.

Page 1 of 4 123 ... LastLast
Results 1 to 10 of 33
  1. #1 OverWriting a int array. 
    Contributor

    clem585's Avatar
    Join Date
    Sep 2013
    Posts
    3,788
    Thanks given
    706
    Thanks received
    702
    Rep Power
    570
    How can I make it so it doesn't overwrite the original array (NORMAL_ROOMS1)? Right now modifying the rooms in POSSIBILITIES modifies the one in NORMAL_ROOMS1 even though they're final.
    Code:
    public class DungeonConstants {	
    
        public static final NormalRoom[] NORMAL_ROOMS1 = {
    	// FROZEN_FLOORS
    			new NormalRoom(8, 240, SOUTH_DOOR),
    			new NormalRoom(10, 240, SOUTH_DOOR),
    			new NormalRoom(12, 240, SOUTH_DOOR)
    
    			,
    			new NormalRoom(8, 242, SOUTH_DOOR, WEST_DOOR),
    			new NormalRoom(10, 242, SOUTH_DOOR, WEST_DOOR),
    			new NormalRoom(12, 242, SOUTH_DOOR, WEST_DOOR)
    
    			,
    			new NormalRoom(8, 244, NORTH_DOOR, SOUTH_DOOR),
    			new NormalRoom(10, 244, NORTH_DOOR, SOUTH_DOOR),
    			new NormalRoom(12, 244, NORTH_DOOR, SOUTH_DOOR)
    
    			,
    			new NormalRoom(8, 246, NORTH_DOOR, SOUTH_DOOR, WEST_DOOR),
    			new NormalRoom(10, 246, NORTH_DOOR, SOUTH_DOOR, WEST_DOOR),
    			new NormalRoom(12, 246, NORTH_DOOR, SOUTH_DOOR, WEST_DOOR)
    
    			,
    			new NormalRoom(8, 248, NORTH_DOOR, SOUTH_DOOR, WEST_DOOR,
    					EAST_DOOR),
    			new NormalRoom(10, 248, NORTH_DOOR, SOUTH_DOOR, WEST_DOOR,
    					EAST_DOOR),
    			new NormalRoom(12, 248, NORTH_DOOR, SOUTH_DOOR, WEST_DOOR,
    					EAST_DOOR)};
    
    	public static final int EAST_DOOR = 0, WEST_DOOR = 1, NORTH_DOOR = 2,
    			SOUTH_DOOR = 3;
    		
    	public static NormalRoom generateNormalRoom() {
    		List<NormalRoom> POSSIBILITIES = new ArrayList<NormalRoom>();
    		int i_1;
    		int i_2;
    		for (i_1 = 0 ; i_1 < 4 ; i_1++) {
    			POSSIBILITIES.clear();
    			for (i_2 = 0 ; i_2 < START_ROOMS1.length ; i_2++) {
    				POSSIBILITIES.add(START_ROOMS1[i_2]);
    				POSSIBILITIES.get(i_2).modifyRot(i_1);
    			}
                   }
        }
    }
    Project thread
    Reply With Quote  
     

  2. #2  
    Contributor

    clem585's Avatar
    Join Date
    Sep 2013
    Posts
    3,788
    Thanks given
    706
    Thanks received
    702
    Rep Power
    570
    bump
    Project thread
    Reply With Quote  
     

  3. #3  
    Contributor

    clem585's Avatar
    Join Date
    Sep 2013
    Posts
    3,788
    Thanks given
    706
    Thanks received
    702
    Rep Power
    570
    Bumping within 24 hours since a certain person keeps making 5 help threads a day, making half the actual help threads fall into oblivion.
    Project thread
    Reply With Quote  
     

  4. #4  
    Extreme Donator


    Join Date
    Jul 2009
    Age
    27
    Posts
    4,351
    Thanks given
    826
    Thanks received
    1,239
    Rep Power
    1781
    Code:
    public static List<NormalRoom> randomise(NormalRoom[] rooms) {
    	/* Adds all pre-existing rooms to the array. */
    	List<NormalRoom> randomised = Arrays.asList(rooms);
    
    	/* Copy a room, but with a different rotations. */
    	int modCount = 5, modified = 0; // XXX: Change me?
    	
    	while (modified != modCount) {
    		int id = new SecureRandom().nextInt(rooms.length); // XXX: Change me?
    		NormalRoom room = randomised.get(id);
    		
    		if (room == null) {
    			/* Shouldn't happen. */
    			continue;
    		}
    		
    		NormalRoom random = new NormalRoom(...); // Add new parameters.
    		randomised.add(random;
    		
    		modified++;
    	}
    	
    	return randomised;
    }
    something like this?

    You can find my GitHub here, for what I'm currently working on.
    Reply With Quote  
     

  5. Thankful user:


  6. #5  
    Contributor

    clem585's Avatar
    Join Date
    Sep 2013
    Posts
    3,788
    Thanks given
    706
    Thanks received
    702
    Rep Power
    570
    Quote Originally Posted by Sir Tom View Post
    Code:
    public static List<NormalRoom> randomise(NormalRoom[] rooms) {
    	/* Adds all pre-existing rooms to the array. */
    	List<NormalRoom> randomised = Arrays.asList(rooms);
    
    	/* Copy a room, but with a different rotations. */
    	int modCount = 5, modified = 0; // XXX: Change me?
    	
    	while (modified != modCount) {
    		int id = new SecureRandom().nextInt(rooms.length); // XXX: Change me?
    		NormalRoom room = randomised.get(id);
    		
    		if (room == null) {
    			/* Shouldn't happen. */
    			continue;
    		}
    		
    		NormalRoom random = new NormalRoom(...); // Add new parameters.
    		randomised.add(random;
    		
    		modified++;
    	}
    	
    	return randomised;
    }
    something like this?
    It needs to add every element of the array. Doesn't that return only 5 random elements?
    Project thread
    Reply With Quote  
     

  7. #6  
    Registered Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks given
    23
    Thanks received
    46
    Rep Power
    6
    Didn't mean to double post.. just noticed.. zzz
    Reply With Quote  
     

  8. #7  
    Registered Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks given
    23
    Thanks received
    46
    Rep Power
    6
    You're going to need to create a new copy of the NormalRoom if you intend on the values inside of the array to stay the same.
    For a quick solution, make NormalRoom implement Cloneable and inside of your 'generateNormalRoom' method change:

    Code:
    POSSIBILITIES.add(START_ROOMS1[i_2]);
    to:
    Code:
    POSSIBILITIES.add(START_ROOMS1[i_2].clone());
    or instead of using clone you can make a static method inside of NormalRoom such as:
    Code:
    public static NormalRoom copyOf(NormalRoom copy) {
        return new NormalRoom(copy.param1, copy.param2, copy.param3.... etc);
    }
    Note: This is just a quick way to make your design work.

    Also, the final keyword indicates that the array's set value cannot be modified, but not the values inside of the array.
    Reply With Quote  
     

  9. Thankful user:


  10. #8  
    Contributor

    clem585's Avatar
    Join Date
    Sep 2013
    Posts
    3,788
    Thanks given
    706
    Thanks received
    702
    Rep Power
    570
    Quote Originally Posted by Archon View Post
    Quote Originally Posted by Archon View Post
    You're going to need to create a new copy of the NormalRoom if you intend on the values inside of the array to stay the same.
    For a quick solution, make NormalRoom implement Cloneable and inside of your 'generateNormalRoom' method change:

    Code:
    POSSIBILITIES.add(START_ROOMS1[i_2]);
    to:
    Code:
    POSSIBILITIES.add(START_ROOMS1[i_2].clone());
    or instead of using clone you can make a static method inside of NormalRoom such as:
    Code:
    public static NormalRoom copyOf(NormalRoom copy) {
        return new NormalRoom(copy.param1, copy.param2, copy.param3.... etc);
    }
    Note: This is just a quick way to make your design work.

    Also, the final keyword indicates that the array's set value cannot be modified, but not the values inside of the array.
    Used this instead but it still modified it.. wtf... I used a command to confirmed it wasn't the ints SOUTH_DOOR, WEST_DOOR etc... and it's not.

    Code:
    public static NormalRoom generateNormalRoom() {
        List<NormalRoom> POSSIBILITIES = new ArrayList<NormalRoom>();
        NormalRoom[] copy;
        int i_1;
        int i_2;
        for (i_1 = 0 ; i_1 < 4 ; i_1++) {
            POSSIBILITIES.clear();
            copy = NORMAL_ROOMS1.clone();
    	for (i_2 = 0 ; i_2 < copy.length ; i_2++) {
                POSSIBILITIES.add(copy[i_2]);
    	    POSSIBILITIES.get(i_2).modifyRot(i_1);
            }
        }
    }
    Btw the cmd I used to debug is this:

    Code:
    		if (cmd[0].equals("dgconst")) {
    			NormalRoom roomy = DungeonConstants.NORMAL_ROOMS1[81];
    			for (int i = 0 ; i < roomy.getDirections().length ; i++)
    				player.getPackets().sendGameMessage("ROT: "+roomy.getDirections()[i]);
    			if (DungeonConstants.WEST_DOOR != 0 || DungeonConstants.NORTH_DOOR != 1 || DungeonConstants.EAST_DOOR != 2 || DungeonConstants.SOUTH_DOOR != 3)
    				System.out.println("DOOR ROTATIONS VALUES HAVE BEEN MODIFIED.");
    			else 
    				System.out.println("Looks good.");
    		return true;
    		}
    Not optimised at all but it gets the job done. The numbers for ROT: weren't the same before/after doing the void method but the int constants were the same.
    Project thread
    Reply With Quote  
     

  11. #9  
    Registered Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks given
    23
    Thanks received
    46
    Rep Power
    6
    Quote Originally Posted by clem585 View Post
    Used this instead but it still modified it.. wtf... I used a command to confirmed it wasn't the ints SOUTH_DOOR, WEST_DOOR etc... and it's not.

    Code:
    public static NormalRoom generateNormalRoom() {
        List<NormalRoom> POSSIBILITIES = new ArrayList<NormalRoom>();
        NormalRoom[] copy;
        int i_1;
        int i_2;
        for (i_1 = 0 ; i_1 < 4 ; i_1++) {
            POSSIBILITIES.clear();
            copy = NORMAL_ROOMS1.clone();
    	for (i_2 = 0 ; i_2 < copy.length ; i_2++) {
                POSSIBILITIES.add(copy[i_2]);
    	    POSSIBILITIES.get(i_2).modifyRot(i_1);
            }
        }
    }
    Btw the cmd I used to debug is this:

    Code:
    		if (cmd[0].equals("dgconst")) {
    			NormalRoom roomy = DungeonConstants.NORMAL_ROOMS1[81];
    			for (int i = 0 ; i < roomy.getDirections().length ; i++)
    				player.getPackets().sendGameMessage("ROT: "+roomy.getDirections()[i]);
    			if (DungeonConstants.WEST_DOOR != 0 || DungeonConstants.NORTH_DOOR != 1 || DungeonConstants.EAST_DOOR != 2 || DungeonConstants.SOUTH_DOOR != 3)
    				System.out.println("DOOR ROTATIONS VALUES HAVE BEEN MODIFIED.");
    			else 
    				System.out.println("Looks good.");
    		return true;
    		}
    Not optimised at all but it gets the job done. The numbers for ROT: weren't the same before/after doing the void method but the int constants were the same.
    Do not clone the array. Clone the values inside of the array.
    Reply With Quote  
     

  12. #10  
    Extreme Donator


    Join Date
    Jul 2009
    Age
    27
    Posts
    4,351
    Thanks given
    826
    Thanks received
    1,239
    Rep Power
    1781
    Quote Originally Posted by clem585 View Post
    It needs to add every element of the array. Doesn't that return only 5 random elements?
    Look at the first line, the List is constructed with the pre-existed array, and then adds 5 different modified rooms.

    You can find my GitHub here, for what I'm currently working on.
    Reply With Quote  
     

Page 1 of 4 123 ... LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 5
    Last Post: 09-15-2014, 08:13 PM
  2. "For", "int[] Arrays", and more to come! - Short guide!
    By stalkerstrucker in forum Tutorials
    Replies: 3
    Last Post: 09-22-2013, 07:41 PM
  3. Storing int arrays in db
    By thesharp in forum Help
    Replies: 1
    Last Post: 03-17-2011, 01:17 AM
  4. array required, but int found
    By Cjay00091 in forum Help
    Replies: 5
    Last Post: 12-16-2009, 07:57 AM
  5. String array to int Aray how to do.
    By Serenity in forum Help
    Replies: 3
    Last Post: 03-24-2009, 04:32 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •