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

Page 1 of 1

## 5 Replies - 2462 Views - Last Post: 05 December 2011 - 11:44 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=258496&amp;s=1a2320dc8fce7f71361a5497cb8056ea&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 swim_5318

Reputation: 1
• 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
*/
{
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)
{
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

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• 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]?

### #3 swim_5318

Reputation: 1
• 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
*/
{
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

### #4 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• 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

### #5 swim_5318

Reputation: 1
• 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.

### #6 DimitriV

• vexing conundrum

Reputation: 587
• Posts: 2,746
• 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.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }