5 Replies - 1754 Views - Last Post: 05 December 2011 - 11:44 PM Rate Topic: -----

#1 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Complete a method that swaps adjacent values in an array of integers.

Posted 05 December 2011 - 01:54 AM

I am very new to programming, so please bare with me.
The task is to complete a method that swaps adjacent values in an array of integers. For example, if the array contains the values
1 4 9 16 25 36
then after calling the method, it should contain the values
4 1 16 9 36 25
If the array contains an odd number of elements, leave the last element in place. For example,
1 4 9 16 25 36 45
becomes
4 1 16 9 36 25 45

Here is what I have tried and I have gotten a java.lang.ArrayIndexOutOfBoundsException error.....
public class Swapper
{
   /**
      This method swaps adjacent elements of the given array
      @param values an array
   */
   public void swapAdjacentElements(int[] values)
   {
      for(int i=0; i < values.length; i++) 
         {
            values[i]=values [i++]; 
            values[i+1]=values [i]; 
            values[i+2]=values [i+3]; 
            values[i+3]=values [i+2]; 
            values[i+4]=values [i+5];
            values[i+5]=values [i+4];  
            
          }
   }
   
   
   // This method is used to check your work
   public int[] check(int[] values)
   {
      swapAdjacentElements(values);
      return values;
   }
}


This post has been edited by macosxnerd101: 05 December 2011 - 07:32 AM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Complete a method that swaps adjacent values in an array of integers.

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Complete a method that swaps adjacent values in an array of integers.

Posted 05 December 2011 - 06:44 AM

You gave me chuckle: "bare with me" means lets get naked together. I'm not that kind of guy.

As for your swap method, time to rethink. You want to swap the 1st and 2nd, 3rd and 4th, 5th and 6th, etc. elements. That means swap the elements at the 0/1, 2/3, 4/5, etc. indices. So you could use a for loop that counts by two rather than one, something like:
for ( i = 0 ; i < array.length ; i += 2 )
{
   int tempInt = array[i];
   array[i] = array[i + 1];
   array[i + 1] = tempInt;
}

BUT, you still have to be concerned that [i + 1] could be an index out of bounds for an odd number of items in array[]. So how do you restrict the for loop so that (for example) an array of 7 elements doesn't try to swap the last element, array[6], with a non-existent array[7]?
Was This Post Helpful? 0
  • +
  • -

#3 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: Complete a method that swaps adjacent values in an array of integers.

Posted 05 December 2011 - 11:38 PM

I tried a if statement to keep it from going out of bounds, but it didn't work....

{
   /**
      This method swaps adjacent elements of the given array
      @param values an array
   */
   public void swapAdjacentElements(int[] values)
   {
       for(int i=0; i<values.length; i+=2)
          {
               int tempInt= values[i];
               values[i]= values[i+1]; 
               if(values.length%2==0)
                    values[i+1]=tempInt; 
           }
   }
   

This post has been edited by smohd: 06 December 2011 - 12:25 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Complete a method that swaps adjacent values in an array of integers.

Posted 05 December 2011 - 11:39 PM

Just to avoid people with dirty minds (cough, cough GregBrannon), it is spelt Bear With Me
Was This Post Helpful? 0
  • +
  • -

#5 swim_5318  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 84
  • Joined: 05-December 11

Re: Complete a method that swaps adjacent values in an array of integers.

Posted 05 December 2011 - 11:42 PM

Thanks, sorry about that. I am so exhausted lately.

Thanks, sorry about that. I am so exhausted lately.
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Complete a method that swaps adjacent values in an array of integers.

Posted 05 December 2011 - 11:44 PM

That is fine... long year.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1