# Practice With Arrays!

Page 1 of 1

## 9 Replies - 862 Views - Last Post: 15 October 2012 - 01:36 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=295725&amp;s=27d6d989093d78d1dc37c5344d328e47&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Javano0b

Reputation: 0
• Posts: 11
• Joined: 02-October 12

# Practice With Arrays!

Posted 15 October 2012 - 10:57 AM

Hey guys, below is the shell of an assignment I've been given followed by the code I have. I'm not including the driver class, I just want to see how close I am to getting the methods correct. Thanks in advance!

1. Write a method called moveDown that deletes the element at index spot and moves the elements down to fill the gap. If spot is out of bounds, the method does nothing.
Use the signature: public static void moveDown (char[] goodies, int spot)

Ok so here's what I have for that method:
```public static void moveDown (char[] goodies, int spot)
{
for (int i = 0; i < goodies.length; i++)
{
goodies [i]--;
spot--;
}
}

```

2. Write a method called moveUp that inserts one new element at index spot and moves the elements up to make room for the new element. When moving the elements up, the originally last element will cease to exist. If spot is out of bounds, the method does nothing.
Use the signature: public static void moveUp (char[] goodies, int spot, char newElement)

Here's what I have for this, and I know it is quite wrong, I can't come up with a way to do this one though...
```public static void moveUp (char[] goodies, int spot, char newElement)
{
for (int i = 0; i < goodies.length; i++);
{
goodies [i]++;
spot++;
}
}

```

Finally 3. Write a method called reverser that reverses the order of elements in an array.
Use the signature: public static void reverser (char[] goodies)

And here's what I have that I believe to be correct for this one:
```public static void reverser (char[] goodies)
{
{
char temp;
char[] start = {'g', 'o', 'o','d', 'y'};
char[] end = {'y', 'd', 'o','o','g'};

temp = goodies[start];
goodies[start] = goodies[end];
goodies[end] = temp;
reverser(goodies);
}

}

```

Thanks so much for your help!

Is This A Good Question/Topic? 0

## Replies To: Practice With Arrays!

### #2 rfs02

Reputation: 26
• Posts: 70
• Joined: 30-September 12

## Re: Practice With Arrays!

Posted 15 October 2012 - 11:03 AM

Let's focus on moveDown for a second.

• You need to check if spot is within the bounds of the array, which you are not
• goodies[i]-- decrements goodies[i], which is not what you want... You want to make goodies[i] equal to goodies[i + 1]
• Why exactly are you decrementing spot?

For moveUp... Same comments, but your array might become larger than the input array, so you need to expand it by one element.

EDIT: Nevermind, the originally last element of the array can disappear as pointed out by sepp2k in the comments below, so no need to create a new array.

And I have no idea what you are trying to do with reverser

This post has been edited by rfs02: 15 October 2012 - 11:45 AM

### #3 jon.kiparsky

• Pancakes!

Reputation: 9454
• Posts: 16,377
• Joined: 19-March 11

## Re: Practice With Arrays!

Posted 15 October 2012 - 11:05 AM

```public static void moveDown (char[] goodies, int spot)
{
for (int i = 0; i < goodies.length; i++)
{
goodies [i]--;
spot--;
}
}

```

so here you enter the loop with i==0, and then you try to reference goodies[-1] and you get an exception because there ain't no such thing.

You don't attempt to change anything about the array, so nothing can possibly happen here.
Instead, for each element starting from goodies[spot] you want to replace that element with the next one.

EDIT: Reading too fast, total fail on my part. (In my mind, the -- was inside the brackets)
This decrements each member of the array (and alters a local variable which doesn't matter). Probably not what you wanted.

For reverser, there's a pretty big conceptual error to start with: an array index has to be an int - an array won't do!

This post has been edited by jon.kiparsky: 15 October 2012 - 11:16 AM

### #4 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,611
• Joined: 25-December 08

## Re: Practice With Arrays!

Posted 15 October 2012 - 11:07 AM

For number one, when you do [il]goodies [i]--;[il] all you are do is telling the element in the array at that location, to decrement its value by one. since it is a char[] if the value was 'b', it would become 'a'.

you need to take your start index and delete it or replace it with the next index. The only thing you have to worry about is to make sure that the index you are accessing doesn't become greater or equal to the array.length:
```int delIndex = 2;
if( delIndex >= array.legth )
return;
for( int i = 0; i < array.length; i++ )
{
//TODO: add a check to make sure IndexOutOfBounds isnt thrown
array[ delIndex + i ] = array[ delIndex + i + 1];
}
```

For problem number 2, take a look at my suggestions for number 1. The problem is technically the same with just a reverse task.

For problem number 3, there is two ways to complete this taks. you could either create a swap() function and keep swapping elements until you got to the middle. Or you create a second array of the same size, loop through the first one backwards and fill up the second forwards, then return the new array (or set the new one to the old one)

### #5 sepp2k

• D.I.C Lover

Reputation: 2298
• Posts: 3,557
• Joined: 21-June 11

## Re: Practice With Arrays!

Posted 15 October 2012 - 11:15 AM

In your first method you're decreases the value of each element of the array by one. So if you put in an array containing the characters b,t, and y, you'll get out an array containing the character a,s, and x. You're also subtracting goodies.length from spot, but since that's a local variable, that has no effect whatsoever. You're not removing any element of the array or changing any element's position.

In the second method you're doing the same thing except that you're increasing instead of decreasing. Again you're not moving any elements and you're definitely not inserting the new element - you're not even using the newElement parameter.

In your last method you're using char arrays as indices. That does not make any sense and should cause a type error. You're also recursing on an array of the same size, leading to infinite recursion.

rfs02, on 15 October 2012 - 08:03 PM, said:

For moveUp... Same comments, but your array might become larger than the input array, so you need to expand it by one element.

You can't expand arrays in Java. Note that the assignment says that the previously last element will disappear.

jon.kiparsky, on 15 October 2012 - 08:05 PM, said:

```public static void moveDown (char[] goodies, int spot)
{
for (int i = 0; i < goodies.length; i++)
{
goodies [i]--;
spot--;
}
}

```

so here you enter the loop with i==0, and then you try to reference goodies[-1] and you get an exception because there ain't no such thing.

No, he doesn't. i starts at 0, as you say. And then it's only ever increased, so it never becomes negative. spot does become negative, but since he never actually uses it (to index into the array or for anything else), that's of little consequence.

### #6 jon.kiparsky

• Pancakes!

Reputation: 9454
• Posts: 16,377
• Joined: 19-March 11

## Re: Practice With Arrays!

Posted 15 October 2012 - 11:17 AM

sepp2k, on 15 October 2012 - 01:15 PM, said:

No, he doesn't. i starts at 0, as you say. And then it's only ever increased, so it never becomes negative. spot does become negative, but since he never actually uses it (to index into the array or for anything else), that's of little consequence.

Yes, I just noticed that (see edit)

### #7 rfs02

Reputation: 26
• Posts: 70
• Joined: 30-September 12

## Re: Practice With Arrays!

Posted 15 October 2012 - 11:42 AM

sepp2k, on 15 October 2012 - 11:15 AM, said:

You can't expand arrays in Java. Note that the assignment says that the previously last element will disappear.

I was thinking that you would have to create a new array and move all the data elements to the new array. I hadn't noticed the part of the assignment that said the last element would disappear. That's why I never did well on my assignments

### #8 Javano0b

Reputation: 0
• Posts: 11
• Joined: 02-October 12

## Re: Practice With Arrays!

Posted 15 October 2012 - 12:11 PM

Wow thanks for all the quick responses! I see what I did wrong with my moveDown and moveUp methods, and know how to fix them! For the reverser method though I thought I was somewhat on the right track but now I'm starting to see the problems. How could I go about successfully coding this reverser method? We just started arrays last week so any and all help is completely appreciated!

### #9 rfs02

Reputation: 26
• Posts: 70
• Joined: 30-September 12

## Re: Practice With Arrays!

Posted 15 October 2012 - 01:07 PM

I would start at both ends of the array and swap the element at position 0 with the element at position goodies.length, then I would do the same for the elements at positions 1 and goodies.length - 1... etc....

Make sure you stop when you reach to the middle of the array, otherwise, you'll end up reversing it again.

### #10 jon.kiparsky

• Pancakes!

Reputation: 9454
• Posts: 16,377
• Joined: 19-March 11

## Re: Practice With Arrays!

Posted 15 October 2012 - 01:36 PM

In your original design, you used a recursive call. This is not necessary, but it's a cool way to do it. What you'd need, though, would be to arrange for it to terminate.

You could do this by including some state information in the method call, and using it to know what you're changing and when you're done.

If you have char[] array = {'a','b','c','d','e'} then the steps might look like:

first pass, swap 'a' and 'e'
second pass, swap 'b' and 'd'
third pass, swap 'c' and 'c' (!)
fourth pass, recognize that there's nothing to do, so just return.

The question is, what information do you need to include in the method call to make this work?

You could also get exactly the same effect without the recursive call, using a for loop. What would this look like?