Results 1 to 4 of 4
  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default reversing an array

    hi all,

    I need to reverse an array that does not reverse the offset value at the beginning or the end of the array. If someone can help me clean and fix my code it would be greatly appreciated.
    If this is not clear let me know.

    Java Code:
    import java.util.Arrays;
    
    
    public class Practice {
        
        public void reverse(int offset) {
            System.out.println("offset amount = " + offset);
            int temp = 0;
            int[] data = {-2,3,5,-14,1,2,3,4,9,7};
            System.out.println("data = " + Arrays.toString(data));
            if (data.length - 1 - offset == offset - 1);
            else if (offset + 1 == data.length - 1 - offset) {
                 temp = data[offset];
                 data[offset] = data[data.length - 1 - offset];
                 data[data.length - 1 - offset] = temp ;
            }
            else{  
                for (int i = 0; i < data.length - 1 - offset; i++) {
                    temp = data[data.length/2 + i];
                    System.out.println(temp);
                    data[data.length/2 + i] = data[data.length/2 - i - 1];
                    data[data.length/2 - i - 1] = temp;
                    System.out.println(temp);
                    if (temp == data[data.length - 1 -offset])
                        break;
                }
            }
            System.out.println("reversed array with null offsets" + Arrays.toString(data));
        }
    
        
        public static void main(String[] args) {
            Practice reversePractice = new Practice();
            reversePractice.reverse(2);
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: reversing an array

    What does this code do? What is the problem?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: reversing an array

    Quote Originally Posted by KevinWorkman View Post
    What does this code do? What is the problem?
    Thanks,

    What I need it to do is take the array data and reverse the values. Say the last number is 7 and the first number is -2, then the first number will be switched to 7 and the last to -2. The catch with my question is to offset the beginning and end of the array with a value (in my case 2 "public void reverse(int offset)" "reversePractice.reverse(2);"). that will reverse the array of whatever size with whatever values starting from 2 over from the left (data[2]) and 2 over from the right data[n - 1 - 2] (data[7] in the array declared in the code above). Note: the code above is not 100% correct.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: reversing an array

    What's not correct about it? What exactly does the code do? What is incomplete about it? What do you need help with?

    I suggest drawing a bunch of example arrays out on a piece of paper, then drawing out the results of calling this method with different offsets. You should notice a pattern that you can use.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Reversing an Array
    By georger55 in forum New To Java
    Replies: 1
    Last Post: 07-07-2012, 11:52 PM
  2. Reversing an array
    By severus1 in forum New To Java
    Replies: 12
    Last Post: 06-30-2011, 05:34 PM
  3. Array reverse algorithim - reversing elements
    By Adomini in forum New To Java
    Replies: 9
    Last Post: 08-30-2010, 04:13 AM
  4. Reversing
    By whosadork in forum New To Java
    Replies: 14
    Last Post: 11-06-2008, 04:29 AM
  5. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08: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
  •