How to copy one array into another and flip the values

• September 6th, 2013, 11:32 PM
How to copy one array into another and flip the values
----------------------------------------------

IE:

"go", "west", "young", "man"

would be this:

"man", "young", "west", "go"

----------------------------------------------

Code Java:

String[] s = { "from", "first", "to", "last" };
String w;
w = {s[3], s[2], s[1], s[0]};

I think there is such a function as a copy function, but I don't know how to flip it from there. Also, is there any way of simulating it?

As always, all help is appreciated. Thanks. :D
• September 6th, 2013, 11:59 PM
syedbhai
Re: How to copy one array into another and flip the values
Hello.
There are indeed methods present in the Java API to copy one array into another.
Coming to flipping the values of an array you can write your own logic which consists of hardly about four to five lines.
The idea is simple. Suppose there is a source array having 'n' elements. I need to copy these 'n' values in the target array in flipped manner.
Take the last element of source array and put it at the beginning in the target array. Place the other elements in the source array at the appropriate place in the target array.

Syed.
• September 7th, 2013, 12:17 AM
GregBrannon
Re: How to copy one array into another and flip the values
In pseudocode:

copyArray = new String[originalArray.length]
for ( i = copyArray.length - 1, j = 0 ; j < copyArray.length ; i--, j++ )
copyArray[j] = originalArray[i];
• September 7th, 2013, 08:31 PM
Re: How to copy one array into another and flip the values
So, using the variables "s" and "w" that would be this:

copyArray = new String[s.length]
for (i = copyArray.length - 2, j = 0; j < copy.length ; i--, j++ )
copyArray[j] = s[i];

Or is that a bad way of doing it? I just want to see if I get your code.
• September 7th, 2013, 09:27 PM
Ubiquitous
Re: How to copy one array into another and flip the values
The code they've provided has more than enough information to answer your question. The thing you may want to pose your instructor is if he wants you to convert a String[] array into a String. The difference is a String[] array contains multiple strings while a String singular.

• September 8th, 2013, 01:14 PM
Re: How to copy one array into another and flip the values
Well, when I try this:

Code Java:

w = {s[0], s[1], s[2], s[3]}

It returns an error. I get errors A LOT, due to different things I try, that say that w is already a string type. How do I take each individual value from s and store it in w. I know that I'm supposed to print w afterwards, but I don't know (I mean I've tried different stuff) how to put each value from an array into a string in the first place. Obviously, I want to flip s over into w. I've also tried this:

Code Java:

String w = new String[]w;
w = {s[3], s[2], s[1], s[0]};

I've tried other stuff too, but I always get an error. I don't know how to store an array value in a string that way.
• September 8th, 2013, 10:04 PM
jps
Re: How to copy one array into another and flip the values
Quote:

It returns an error.
I get errors A LOT,
but I always get an error.

Error messages tell what the problem is. Always include the full text of the error message with your code and quesiton
• September 9th, 2013, 12:45 PM
Re: How to copy one array into another and flip the values
Here's the error:

Code Java:

TC1.java:17: error: array dimension missing

String w = new String[]w;

• September 9th, 2013, 04:27 PM
GregBrannon
Re: How to copy one array into another and flip the values
That doesn't seem like A LOT to me, but I suppose taking them one at a time is a good way to attack them. A bit time consuming, however.

You should review the Arrays tutorial, how to declare and initialize them.