Results 1 to 2 of 2
  1. #1
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Exclamation What does this Constructor Do ?

    Hi Guys

    I have a bit of Java code and I'm trying to digest it and understand it.

    Here is the code:

    Java Code:
    public class SimpleRandomCount extends RandomCount {
        /**
         * Constructor
         */
        public SimpleRandomCount(int size) {
            super(size);
        }
        
        /**
         * Randomise the array
         */
        protected void randomise() {
            
            Integer[] copy = new Integer[array().length];
            // used to indicate if elements have been used
            boolean[] used = new boolean[array().length];
            
            Arrays.fill(used,false);
            
            for (int index = 0; index < array().length; index++) {
                
                int randomIndex;
                
                do {
                   randomIndex = randomIndex();
                } while (used[randomIndex]);
                copy[index] = array()[randomIndex];
                used[randomIndex] = true;
            }
            
            for (int index = 0; index < array().length; index++) {
                array()[index] = copy[index];
            }
            
        }
    
    } // End of class SimpleRandomCount
    The first bit of code that I'm trying to get my head around is this bit of the randomise() method:

    Java Code:
    Integer[] copy = new Integer[array().length];
            // used to indicate if elements have been used
            boolean[] used = new boolean[array().length];
            
            Arrays.fill(used,false);
    What does the bottom line do and which array does it do it to ? cause there are two array (copy and used) initiated above that line !

    I would greatly appreciate any help.

    Thank You.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Ciwan View Post
    Java Code:
    Integer[] copy = new Integer[array().length];
            // used to indicate if elements have been used
            boolean[] used = new boolean[array().length];
            
            Arrays.fill(used,false);
    What does the bottom line do and which array does it do it to ? cause there are two array (copy and used) initiated above that line !
    Have you read the API documentation for the Arrays.fill( ... ) method? You should have; it fills an array (the first parameter) with the value mentioned as the second parameter, so it fills the entire array 'used' with the value 'false'. It is quite useless because you have just created the array so it is already filled with values 'false'.

    I don't know what that constructor does besides calling its superclass constructor with the same parameter value ...

    kind regards,

    Jos

Similar Threads

  1. Constructor
    By arefeh in forum New To Java
    Replies: 9
    Last Post: 01-11-2010, 11:38 AM
  2. Constructor
    By Sarinam in forum AWT / Swing
    Replies: 1
    Last Post: 06-19-2008, 08:03 AM
  3. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:01 AM
  5. Constructor Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 05:44 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
  •