Results 1 to 6 of 6
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default Initializing Arrays

    I used java.util.Arrays to initialize an integer array. Please review the code below:

    Java Code:
    int[] array = new int[10];
    Arrays.fill(array, 0);
    for(int i=0;i<array.length;i++)
    System.out.println("Value at " + i + " is " + array[i]);
    Output:
    Java Code:
    Value at 0 is 0
    Value at 1 is 0
    Value at 2 is 0
    Value at 3 is 0
    Value at 4 is 0
    Value at 5 is 0
    Value at 6 is 0
    Value at 7 is 0
    Value at 8 is 0
    Value at 9 is 0
    Can I also initialize a String array uisng java.util.Arrays? If yes, please tell me the method.

    Thanks.

  2. #2
    Lang is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    I think what you're trying to do this:
    Java Code:
    String[] array = new String[10];
    
    array[0] = "Zero";
    array[1] = "One";
    //etc

  3. #3
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks. But I want some function to initialize a String array without putting values manually.

    Thanks.

  4. #4
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Just do what Lang said inside a loop.

    Java Code:
    for(int i = 0; i < array.length; i++){
      array[i] = "This is the string at index" + i;
    
    }

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Can I also initialize a String array uisng java.util.Arrays? If yes, please tell me the method.
    Yes.
    Java Code:
    String[] s = new String[6];
    Arrays.fill(s, "");
    System.out.printf("s = %s%n", Arrays.toString(s));

  6. #6
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

Similar Threads

  1. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 03:45 PM
  2. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 11:08 PM
  3. Initializing variables using constructors
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 09:28 PM
  4. Initializing property object from a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-29-2007, 05:55 PM
  5. Initializing SAXParser
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-14-2007, 07:33 PM

Posting Permissions

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