Results 1 to 10 of 10
  1. #1
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default Array reverse algorithim - reversing elements

    Hi everyone. Thanks in advance for everyone who created this forum!

    I am a student in a java learners class, and I wanted some feedback on some work that I am doing in school. (not the answer, but some pointers)

    The homework is I have to construct a method that will reverse the elements in an array of integers. For efficiency purposes, this method will not create another array in the process of the reversing.

    This is my code so far, but my issue is that while I am using a " for " loop for counting int [] a backwards, I am not initializing the elements.....

    Code so far is :
    Java Code:
    
    	public static void reverse(int[] a) 
    	{
    		
    
    		if (a == null)
    			throw new IllegalArgumentException("null array");
    
    		
    		
    		for( int i = a.length; i > 0 ; i --)
    			 
            {   
    			// this is the tough part of the code, 
    			// as I cannot create another array to put 
    			//the reverse order in. 
    			
    			
             }
    		
        }

  2. #2
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    I have tried to put in the for loop a[i], but an error comes up.

    Since I cannot return anything, (which is why method is void), I am confused immensely.

    It has to have a[i] in the for loop somewhere, right?

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Imagine what you are doing is swapping the elements in the array; instead of doing this:
    Java Code:
    Array A:	Array B:
    1 2 3 4 5 6	6
    1 2 3 4 5 6	6 5
    1 2 3 4 5 6	6 5 4
    1 2 3 4 5 6	6 5 4 3
    1 2 3 4 5 6	6 5 4 3 2
    1 2 3 4 5 6	6 5 4 3 2 1
    You are instead doing this:
    Java Code:
    Array A:
    1 2 3 4 5 6
    6 2 3 4 5 1
    6 5 3 4 2 1
    6 5 4 3 2 1
    So to start, you swap 1 and 6, then 2 and 5, then 3 and 4, then terminate the loop.

    Hopefully this helps a bit; you will need to access a[i], yes, and another element of a as well.

    I'm not sure why you're not allowed to return anything, but that's another issue altogether. For now we'll stick to working on the loop.

  4. #4
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Thanks Zach. By another element in the loop, I am assuming then that I should use another counter, like "j" instead of "i" , right? That is the way how I can identify two elements at once (such as '6' and '1' , then '2' and '5', etc....)

    I tried the answer like a[i] = (a[a.length] - 1) -i), and it ran, but my professor said that while it may have worked in this instance, it is not correct. (sadness!)

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    To solve array manipulations, it useful to take a piece of paper and a pencil and work thru the logic before writing the code.

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by Adomini View Post
    Thanks Zach. By another element in the loop, I am assuming then that I should use another counter, like "j" instead of "i" , right? That is the way how I can identify two elements at once (such as '6' and '1' , then '2' and '5', etc....)
    I don't see why you wold use two different variables... think about it; as you get further from one end, you get the same distance from the other end also. As Norm suggested, you can work this out on paper and see that, to achieve the desired result, you only need the one variable.
    Java Code:
    [color=red]1[/color] 2 3 4 5 [color=red]6[/color]
    6 [color=red]2[/color] 3 4 [color=red]5[/color] 1
    6 5 [color=red]3[/color] [color=red]4[/color] 2 1
    6 5 4 3 2 1


    Quote Originally Posted by Adomini View Post
    I tried the answer like a[i] = (a[a.length] - 1) -i), and it ran, but my professor said that while it may have worked in this instance, it is not correct. (sadness!)
    Your code is very close to being correct. Not sure why your professor didn't say that you were on the right track. But think about what your code does:
    Java Code:
    ([color=red]a[a.length][/color] - 1) -i)
    This will produce an exception. I'm assuming you know that arrays are zero based: The "0" element of {6,5,4,3,2,1} is 6. The "5" element of {6,5,4,3,2,1} is 1. Since the length is 6, we're looking for the "6" element... which doesn't exist.

    Instead, you need to think about which element you should be accessing--when choosing that element, its value should be inside the [] entirely. So, in some situation where you had:
    Java Code:
    int x = 5;
    int y = 2;
    int[] z = {1,2,3,4,5,6};
    And you wanted to pick the x-y'th element of the array:
    Java Code:
    int newInt = z[x] - y; // WRONG!
    int newInt = z[x - y]; // RIGHT!

    Hope that puts you on the right track.

  7. #7
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Got it! I also went to a friend who gave me the same explanation and broke it down for me. You guys are awesome. Thanks, really. (So mad at my professor........)

    I will check back in for more help when I get problems that I cannot solve in 24 hours or so. I hope I do not wear you guys out with my constant haggling if it comes to that.

    Thanks so much!

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by Adomini View Post
    I hope I do not wear you guys out with my constant haggling if it comes to that.
    It's a forum; we're here to help people who have questions.

  9. #9
    kjv007 is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    Moderator edit -- hijack post deleted. Poster please see my note below.
    Last edited by Fubarable; 08-30-2010 at 04:14 AM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    kjv, welcome to the forum, but I ask if you have your own question that you post it in a new thread, one of your very own, rather than hijacking someone else's thread. I have no choice but to delete your question.

Similar Threads

  1. Swapping elements of an array help please
    By ikillu in forum New To Java
    Replies: 11
    Last Post: 01-15-2012, 08:49 PM
  2. sum of elements in array
    By myst in forum New To Java
    Replies: 7
    Last Post: 07-17-2010, 08:36 AM
  3. Math.random in array and reverse order
    By eugenechia in forum New To Java
    Replies: 4
    Last Post: 02-17-2010, 03:33 AM
  4. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 11:56 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 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
  •