Results 1 to 9 of 9
  1. #1
    corrax is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default Help with arrays!

    Hi. I have a mini problem. If I call an integer array and name/initialize it like as follows, I get no errors.

    Java Code:
    int[] exampleArray = {5,5,5};
    But if I do this, I get errors.

    Java Code:
    int[] exampleArray;
    exampleArray = {5,5,5};
    Does anyone know how to fix this?
    Thanks.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you want to put values in the array you need to first initialize the array, then add the items with the indexes.
    Java Code:
     
    int[] intArray;
    int[] intArray = new int[3];
    intArray[0] = 5;
    ...

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Java Code:
    exampleArray = new int[] {5,5,5};
    Or
    Java Code:
    exampleArray = new int[3];
    for (int i = 0; i < exampleArray.length; i++) {
        exampleArray[i] = 5;
    }
    db

  4. #4
    corrax is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    If you want to put values in the array you need to first initialize the array, then add the items with the indexes.
    Java Code:
     
    int[] intArray;
    int[] intArray = new int[3];
    intArray[0] = 5;
    ...
    Thanks for the reply. But that would be kind of annoying because I am creating many arrays,with many fields. So is there a way to reset the array using the {} method? (And also if the fields of the array are different?)

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can use a loop to fill larger arrays. Also, check out what Darryl said

  6. #6
    corrax is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    Ok. Thank you!

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also, please mark your thread solved if you are satisfied with the answers you received. You can do this with the thread tools at the top of the page.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,437
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by corrax View Post
    Hi. I have a mini problem. If I call an integer array and name/initialize it like as follows, I get no errors.

    Java Code:
    int[] exampleArray = {5,5,5};
    But if I do this, I get errors.

    Java Code:
    int[] exampleArray;
    exampleArray = {5,5,5};
    Does anyone know how to fix this?
    Thanks.
    The compiler is too stupid to recognize { 5, 5, 5 } an array of ints in your second example; it could as well have been an array of longs or bytes etc. Arrays by themselves are kind of stupid ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    So is there a way to reset the array using the {} method?
    It's called an array initializer.
    Arrays
    An array initializer is written as a comma-separated list of expressions, enclosed by braces "{" and "}".

    db

Similar Threads

  1. How Useful Are Arrays ?
    By House in forum New To Java
    Replies: 11
    Last Post: 09-05-2010, 02:58 PM
  2. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. Need help regarding Arrays Help pls....
    By shaggyoo7 in forum New To Java
    Replies: 3
    Last Post: 01-14-2009, 04:36 AM
  5. arrays
    By hasysf in forum New To Java
    Replies: 12
    Last Post: 07-28-2008, 02:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •