Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to copy one array into another and flip the values

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool How to copy one array into another and flip the values

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

    IE:

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

    would be this:

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

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

     
    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.


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

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

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,308
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

    Default 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];

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

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

    So what exactly is your question about the code they provided?

  6. #6
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to copy one array into another and flip the values

    Well, when I try this:

     
    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:

     
    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.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to copy one array into another and flip the values

    Quote Originally Posted by ghostheadx View Post
    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

  8. #8
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to copy one array into another and flip the values

    Here's the error:

     
    TC1.java:17: error: array dimension missing 
     
        String w = new String[]w;

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,308
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

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

Similar Threads

  1. array copy!!!
    By Username in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 13th, 2013, 02:53 PM
  2. how to flip image vertically
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 7th, 2012, 07:11 PM
  3. [SOLVED] Please help me with the Coin flip rogram.
    By nikhilrudrangi in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2012, 10:10 PM
  4. Array in constructor and deep copy.
    By Ejii in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 08:34 PM
  5. How to create a coin flip program
    By mrsaucy in forum Java Theory & Questions
    Replies: 1
    Last Post: December 20th, 2010, 04:31 AM

Tags for this Thread