Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default can someone check my code plz

    Hello,
    I have to make a method that rotates the array to the right
    ie if i have an array of int which has the following content: {3, 8, 19, 7}
    if i invoke this method it should return {7, 3, 8, 19}

    heres my code

    Java Code:
    import java.util.*;
    
    public class TestRotateRight
    {
        public static void main (String[] args)
        {
            int [] list = {3, 8, 19, 7};
            
            rotateRight(list);
            
            System.out.println(Arrays.toString(list));   // [7, 3, 8, 19]
            
            rotateRight(list);
            rotateRight(list);
            
            System.out.println(Arrays.toString(list));   // [8, 19, 7, 3]
            
            rotateRight(list);
            
            System.out.println(Arrays.toString(list));   // [3, 8, 18, 7]
            
            rotateRight(list);
            rotateRight(list);
            rotateRight(list);
            
            System.out.println(Arrays.toString(list));   // [8, 19, 7, 3]
            
            rotateRight(list);
            rotateRight(list);
            rotateRight(list);
            rotateRight(list);
            
            System.out.println(Arrays.toString(list));   // [8, 19, 7, 3]
        }
        
        public static void rotateRight (int[] list)
        {
            int length = list.length;
            
            int [] temp = new int [length];
            for ( int i = 0; i < list.length; i++)
            {
                temp[i] = list[i];
            }
            
            list = new int [length];
            
            list[0] = temp[length - 1];
            
            int index = 1;
            
            for ( int i = 0; i < length - 1; i++)
            {
                list[index] = temp[i];
    
                index++;
            }
        }
    }
    i used the debugger on the method that rotates, and i got the correct formation of the new array, but in the main all its printing out is the same list we created it for all the printlns

    can someone tell me where my code is wrong at?
    thanks in advance

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    public static void rotateRight (int[] list)
        {
            int length = list.length;
            
            int [] temp = new int [length];
            for ( int i = 0; i < list.length; i++)
            {
                temp[i] = list[i];
            }
            
            list = new int [length];
    
            // code continues...

    The rotateRight() method is passed a reference (or pointer) to an array. That's OK because you can use this reference to read and write array elements.

    But then in the last line of the part I quoted you make the variable list point to a new and completely different array. You do things to that array, but what you do will have no effect on the array that list pointed to at the start of the method (and that's the one that counts, because that's the one the calling method can see.)

  3. #3
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    but i want it to do the changes to the same array since its being printed in the main, and the question doesnt allow me to print out a new array, in the question the main method was given as above and i had to do the method

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    but i want it to do the changes to the same array

    Yes I know that - so why create a new and completely different array and proceed to set its values?

    (You do see that you are creating a new array, don't you? Say if not because its key to what's going on here.)

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aizen92 View Post
    but i want it to do the changes to the same array since its being printed in the main, and the question doesnt allow me to print out a new array, in the question the main method was given as above and i had to do the method
    Maybe you want to have a look at the System.arraycopy( ... ) method; it can copy (move) elements in the same array. All you have to do is save the last element of the arry and stick it back in as the first element after the arraycopy( ... ) method has finished.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Okay
    ill read the arraycopy method and do it, and ill post back my result

    EDIT: but where in the java doc can i find it?
    Last edited by aizen92; 02-20-2011 at 10:38 AM.

  7. #7
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    Quote Originally Posted by pbrockway2 View Post
    Yes I know that - so why create a new and completely different array and proceed to set its values?

    (You do see that you are creating a new array, don't you? Say if not because its key to what's going on here.)
    im creating a new array to hold the content of the original array since i will be changing the original array, but i dont want to lose its content, so i put them in a temporary array by which they will hold the value of the list array and then i arrange the original array as i want while still keeping my original values

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aizen92 View Post
    im creating a new array to hold the content of the original array since i will be changing the original array, but i dont want to lose its content, so i put them in a temporary array by which they will hold the value of the list array and then i arrange the original array as i want while still keeping my original values
    Have you given up on the arraycopy( ... ) method already?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    no, but i just noticed pbrockway2's post, and i didnt reply to him
    and for the arraycopy method where do i find it, in the java doc?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aizen92 View Post
    no, but i just noticed pbrockway2's post, and i didnt reply to him
    and for the arraycopy method where do i find it, in the java doc?
    Yep, it's in the System class (it's a public static method, ready for use).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    okay
    ill read it and try and see how it goes
    thnx Jos for the help

  12. #12
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    okay, i used the arraycopy method, and tried the method alone, and gave me the correct results but still when i run the main method, its not working, it only gives me the orginal array
    heres my modified rotate method with the arraycopy method

    Java Code:
    public static void rotateRight (int[] list)
        {
            int [] temp = new int [list.length];
            
            System.arraycopy(list, 0, temp, 0, list.length);
            
            list = new int [temp.length];
            
            list[0] = temp[temp.length - 1];
            
            System.arraycopy(temp, 0, list, 1, temp.length - 1);
        }
    EDIT: I removed the "list = new int [temp.length];" and it worked
    im glad it worked but how come it didnt when it was there?

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aizen92 View Post
    okay, i used the arraycopy method, and tried the method alone, and gave me the correct results but still when i run the main method, its not working, it only gives me the orginal array
    heres my modified rotate method with the arraycopy method

    Java Code:
    public static void rotateRight (int[] list)
        {
            int [] temp = new int [list.length];
            
            System.arraycopy(list, 0, temp, 0, list.length);
            
            list = new int [temp.length];
            
            list[0] = temp[temp.length - 1];
            
            System.arraycopy(temp, 0, list, 1, temp.length - 1);
        }
    EDIT: I removed the "list = new int [temp.length];" and it worked
    im glad it worked but how come it didnt when it was there?
    For the same reason as before: you are changing the 'list' variable which is an entirely local and new array. This is how I'd do it:

    Java Code:
    public static void rotateRight(int[] a) {
       int n= a.length-1;
       if (n > 0) { // something to do?
          int last= a[n]; // keep the previous last element
          System.arraycopy(a, 0, a, 1, n); // shift everything to the right;
          a[0]= last; // stick back in the previous last element
       }
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    ohh i see
    well thanks very much Jos for the help

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aizen92 View Post
    ohh i see
    well thanks very much Jos for the help
    You're welcome of course; here's a sketch for another approach: suppose you can reverse part of an array a, from position l up to (but not including) position h. Reversing a sequence of things can be implemented with a sequence of elementary swap operations. You right rotate operation can be implemented with a couple of reverse operations like this:

    Java Code:
    public static void rotateRight(int[] a) {
       reverse(a, 0, a.length);
       reverse(a, 1, a.length);
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    This approach seems simpler and more straightforward to me:
    Java Code:
            public void rotateRight(int[] a) {
                if (a.length == 0) return; // safety first!
                int tmp = a[a.length - 1];
                for (int i = a.length - 1; i > 0; i--) {
                    // this part should be simple -- don't want to spoon-feed everything
                }
                a[0] = tmp;
            }
    -Gary-

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gcalvin View Post
    Java Code:
                if (a.length == 0) return; // safety first!
    Allow me to change that to:

    Java Code:
    if (a == null || a.length <= 1) return; // safety and laziness first!
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by aizen92 View Post
    im creating a new array to hold the content of the original array since i will be changing the original array, but i dont want to lose its content, so i put them in a temporary array by which they will hold the value of the list array and then i arrange the original array as i want while still keeping my original values

    To return to your original code for a moment...

    Java Code:
    public static void rotateRight (int[] list)
    {
        int length = list.length;
            
        int [] temp = new int [length];
        for ( int i = 0; i < list.length; i++)
        {
            temp[i] = list[i];
        }
            
        list = new int [length];  // --- why? ---
            
        list[0] = temp[length - 1];
            
        int index = 1;
            
        for ( int i = 0; i < length - 1; i++)
        {
            list[index] = temp[i];
    
            index++;
        }
    }

    I wasn't talking about the temp array, but the new array that you assign to list. Giving list a new value causes the oddity you reported: the debugger shows list with the correct values in the method, but the caller prints the old values.

    To answer my own question: there is no reason for doing that. What would happen if you removed that line? As you say, you have a copy of its values and you are about to assign new values to each position of the array.

  19. #19
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default

    well i though id empty the original array to fill it from the start with what i want it to be filled with, thats y i re-declare it (sorry if wrong term, i forgot the exact term for it), so that the original array would be emptyed, and fill it from the start

  20. #20
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    list = new int [length];

    I hope you can see now that the effect of this line is *not* to empty the array. Rather it makes list point to an entirely new array. The old one (which remains the one seen by the caller) will not be affected by anything you subsequently do.

    And I hope you can see for yourself the effect of removing this line from your original code.

Page 1 of 2 12 LastLast

Similar Threads

  1. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 04:21 AM
  2. can somebody check my code
    By raven in forum New To Java
    Replies: 2
    Last Post: 08-29-2010, 08:55 PM
  3. Could you please check my code?
    By Cylab in forum New To Java
    Replies: 2
    Last Post: 08-11-2010, 12:13 PM
  4. can anyone check my code?
    By Harmesh Goyal in forum New To Java
    Replies: 8
    Last Post: 06-29-2010, 05:43 AM
  5. Plz Some one check my code
    By TamTam in forum AWT / Swing
    Replies: 1
    Last Post: 02-07-2009, 11:24 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
  •