Sunday 7 June 2015

Manipulating Arrays Demo

public class ManipulatingArraysDemo {

        /* This example uses copyOfRange method in the java.util.Arrays class to
         * copy a range of values from copyFrom to copyTo.
         * 
         * This allows us to cut out a whole line of code. Which doesn't seem like
         * much in this small demo. But it would make quite a difference to a larger
         * project.
         * 
         * copyOfRange requires 3 parameters. The first is the source array.
         * The second is the index starting point in the source array. The third
         * is the last index. However you'll notice the 'a' is not copied.
         * 
         * This is because that last index is exclusive. In other words we copy
         * from index 2 upto but not including index 9.
         */

        public static void main(String[] args) {
                char[] copyFrom = {'d','e','c','a','f','f','e',
                                'i','n','a','t','e','d'};
                char[] copyTo = java.util.Arrays.copyOfRange(copyFrom, 2, 9);

                System.out.println(new String(copyFrom) + " to " + new String(copyTo));
        }
}

No comments:

Post a Comment

Translate