Results 1 to 3 of 3
  1. #1
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default List duplicate values.

    I'm writing my DES Implementation, in Java, and I've hit a bit of a problem. I'm not a very experienced Java coder, i've only been actively using it for the past month or so, and I like to undertake big projects to help me come to terms with a language more efficiently, so here's my big project. Basically, what I'm doing is generating an int array, then I'm storing it in a list, and that gets looped 16 times. I'm reusing variables to make it more efficient, but the problem is when I store the variable in the list, It replaces all previous values in the list. I think this might be to do with the pointers in the ArrayList or something, but I hoped anyone had any solution? Here is the function that the problem occurs in:

    Java Code:
    	/**
    	 * keyExpansion
    	 * 
    	 * @param initialKey key used to begin expansion.
    	 */
    	private void keyExpansion(int[] initialKey)
    	{
    		int[] leftKey = new int[28];
    		int[] rightKey = new int[28];
    		// For bisecting the key.
    		int[] nearlyKey = new int[56];
    		// The key before permutations.
    		int[] finalKey = new int[48];
    		// The final key value.
    		ArrayList<int[]> testList = new ArrayList<int[]>();
    		for(int roundNum = 0; roundNum < 16; roundNum++)
    		{
    			
    			if(roundNum > 0)
    			{
    				initialKey = nearlyKey;
    				nearlyKey = new int[56];
    				// If not first round, work on previous key block.
    			}
    			
    			for(int p = 0; p < 28; p++)
    			{
    				leftKey[p] = initialKey[p];
    				// Populate the left half of the key.
    				rightKey[p] = initialKey[28 + p];
    				// Populate the right half of the key.
    			}	
    		
    			rotate(leftKey,rotationTable[roundNum]);
    			rotate(rightKey,rotationTable[roundNum]);
    			// Rotate the two keys.
    			
    			
    			for(int p = 0; p < 28; p++)
    			{
    				nearlyKey[p] = leftKey[p];
    				nearlyKey[28 + p] = rightKey[p];
    				// Combine the two Keys.
    			}
    			
    			
    			for(int p = 0; p < 48; p++)
    			{
    				finalKey[p] = nearlyKey[permutedChoice2(p)];
    				// Perform permuted choice transformation.
    			}
    			
    			keySchedule.add(finalKey);
    			// Store the key.
    		}
    		
    	}
    Thanks guys!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: List duplicate values.

    I'm reusing variables to make it more efficient
    Whatever gave you the idea it's more efficient?

    when I store the variable in the list, It replaces all previous values in the list.
    You said it. You're storing the same reference variable in all positions in the List. That variable refers to one unique object, whose state you update.

    What other result could you expect?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: List duplicate values.

    Cracked the problem. It was to do with the points. I just re-initialised finalKey as an array[48] and it must have jumped to a different location in memory.

Similar Threads

  1. HashTable and Hash Map are not Showing duplicate values
    By omerkhalid34 in forum New To Java
    Replies: 1
    Last Post: 01-14-2012, 02:43 PM
  2. Can 2-3 Trees contain duplicate values?
    By 6thDAY in forum Advanced Java
    Replies: 1
    Last Post: 05-14-2011, 03:47 AM
  3. Discard duplicate or repeating values in the xml.
    By reach2sudhakar in forum XML
    Replies: 10
    Last Post: 09-23-2010, 12:59 PM
  4. How to detect duplicate values in an Array?
    By maz09 in forum New To Java
    Replies: 1
    Last Post: 04-08-2010, 07:58 AM
  5. How to make a hashmap to allow duplicate values?
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 12:35 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
  •