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

Thread: Changing whole array

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Changing whole array

    How can I change a complete array when it's already initialized?

    Can I change this array:
    String[] stringArray = {"hello","world","test","whatever"};

    to this:
    {"banana","fish","tree","apple"}

    without changing every single one, like this:
    stringArray[0] = "banana";
    stringArray[1] = "fish";
    . . .
    ?

    Something like:
    stringArray = {"banana","fish","tree","apple"};


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Changing whole array

    Did you try writing the code, compiling and executing it to see what happens? This method is useful for printing out arrays when debugging:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Changing whole array

    Eclipse gives an error: "Array constants can only be used in initializers." in this line:
    stringArray = {"banana","fish","tree","apple"};

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Changing whole array

    Add "new String[]" before the {
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Changing whole array

    Maybe I'll rephrase.

    So this is my code:
    String[] stringArray = {"hello","world","test","whatever"};
    System.out.println("stringArray: "+ java.util.Arrays.toString(stringArray));
    stringArray = {"banana","fish","tree","apple"}; // Error: "Array constants can only be used in initializers."
    System.out.println("stringArray: "+ java.util.Arrays.toString(stringArray));

    I wanna change a whole array after it has already been initialized without having to initialize every single one individually.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Changing whole array

    Did you see post#4? Try adding what I suggested there to the code.
    That was referring to the code in post#3.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Gugubo (December 22nd, 2013)

  8. #7
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Changing whole array

    I tried your suggestion in post 4, but apparently I typed it in a bit wrong.

    Tried it again and it works. Thanks!

Similar Threads

  1. Changing strings in a switch statement n stroing it in an array
    By ajw1993 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: February 20th, 2013, 05:25 PM
  2. Iterate over array, changing current object?
    By slimchance in forum Loops & Control Statements
    Replies: 7
    Last Post: May 14th, 2012, 04:52 PM
  3. Replies: 2
    Last Post: September 8th, 2011, 09:50 PM
  4. changing GUI
    By timmin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 3rd, 2011, 08:16 AM
  5. Changing Array variables from different classes
    By smellyhole85 in forum Collections and Generics
    Replies: 6
    Last Post: December 9th, 2010, 03:18 PM