• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array question

 
Ryan Newcombe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
My professor wants me to "swap" the values of the first and the last array object and I am having a hard time figuring this out. I don't know if I should create another "for" loop to code this swap or if I can just code one line and make the first value equal the last value. Here is the sample.

public class Lab21C {
public static void main(String[] args) {
char[] table = {'a', 'b', 'c', 'd'};

// YOUR CODE GOES HERE

for (int i = 0; i < table.length; i++) {
System.out.println(table[i]);
}
}
}

I was thinking something like this:

for (int i = 0; i < table.length; i++) {
table[0] = table[3];
table[3] = table[0];
But that just makes the first and last values the same. This also has to be coded for an array of any size not just 4.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ryan Newcombe:
...table[0] = table[3];
table[3] = table[0];
But that just makes the first and last values the same...

Do you see why this is? You've got a certain value at table[3], and you assign that value to table[0]. Then you want to take what was at table[0] and assign it to table[3] -- but it's too late, because what was at table[0] has already been replaced.

Hint: One basic way to solve this is to declare a separate char variable to hold whatever is at table[0].

Note: Switching these values does not require any iteration. You know that the first element is at table[0]. And you know that the length of the array is table.length. So the last element is at...
 
Ryan Newcombe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your post.
I understand why the numbers are the same, it is exactly the way you explained it. I thought about trying to keep the value of that character but there is nothing in my lesson to tell me how to do that. I tried reassigning the variables but you probably know that didn't work.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this give you an idea...?

char tempChar = table[0];
 
Ryan Newcombe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That gives me an idea but I still don't understand how to get the tempChar back to what it is supposed to be before printing.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before trying to find out how the syntax in Java works exactly, find out what your program has to do exactly. What is the algorithm / procedure that it has to perform?

Maybe the following analogy helps:

Suppose you have two buckets, one filled with water and one filled with milk. How do you swap the contents of the buckets (without mixing milk and water)? You need a third bucket. First pour the water in the third bucket, then pour the milk in the bucket that originally had the water, then pour the water from the third bucket into the bucket that originally contained the milk.

Now suppose that you have a whole row of buckets, numbered from 0 to N. You also have a spare bucket. How are you going to swap the contents of buckets 0 and N, 1 and N-1, 2 and N-2, etc.?
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assign the last element to tempChar, assign the first element to the last element and assign the tempChar to the first element. Basic stuff!
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ryan Newcombe:
That gives me an idea but I still don't understand how to get the tempChar back to what it is supposed to be before printing.


Ah! Here's the bit of magic that you'll need to know:

When you say tempChar = table[0], you are taking the value that is stored in table[0] and storing a copy of that value in tempChar. So, when you change the value in table[0], the value in tempChar does not change.

Does that help?
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is another tricky solution, which involves a bit of maths.


[ October 31, 2005: Message edited by: Srinivasa Raghavan ]
 
Ryan Newcombe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you to everybody for all your help! I finally got it. That bucket of water, bucket of milk idea really helped. Thanks again!
Ryan Newcombe
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic