aspose file tools*
The moose likes Beginning Java and the fly likes Array question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array question" Watch "Array question" New topic
Author

Array question

Ryan Newcombe
Greenhorn

Joined: Oct 02, 2002
Posts: 24
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

Joined: Aug 31, 2004
Posts: 11343

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...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ryan Newcombe
Greenhorn

Joined: Oct 02, 2002
Posts: 24
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

Joined: Aug 31, 2004
Posts: 11343

Does this give you an idea...?

char tempChar = table[0];
Ryan Newcombe
Greenhorn

Joined: Oct 02, 2002
Posts: 24
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

Joined: Aug 16, 2005
Posts: 14347
    
  22

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.?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
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

Joined: Aug 20, 2001
Posts: 1824

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?


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Here is another tricky solution, which involves a bit of maths.


[ October 31, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Ryan Newcombe
Greenhorn

Joined: Oct 02, 2002
Posts: 24
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.
 
subject: Array question