Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-09-2007, 06:08 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
Rep Power: 0
bugger is on a distinguished road
Default Initializing Arrays
I used java.util.Arrays to initialize an integer array. Please review the code below:

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:
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-09-2007, 07:31 PM
Member
 
Join Date: Nov 2007
Posts: 8
Rep Power: 0
Lang is on a distinguished road
Default
I think what you're trying to do this:
Code:
String[] array = new String[10];

array[0] = "Zero";
array[1] = "One";
//etc
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-09-2007, 07:37 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
Rep Power: 0
bugger is on a distinguished road
Default
Thanks. But I want some function to initialize a String array without putting values manually.

Thanks.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-09-2007, 07:49 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
Rep Power: 0
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Default
Just do what Lang said inside a loop.

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

}
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-09-2007, 09:19 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,389
Rep Power: 3
hardwired is on a distinguished road
Default
Can I also initialize a String array uisng java.util.Arrays? If yes, please tell me the method.
Yes.
Code:
String[] s = new String[6];
Arrays.fill(s, "");
System.out.printf("s = %s%n", Arrays.toString(s));
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-09-2007, 09:43 PM
Senior Member
 
Join Date: Nov 2007
Posts: 111
Rep Power: 0
bugger is on a distinguished road
Default
Great.
thanks.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
new to arrays jimJohnson New To Java 1 04-08-2008 02:45 PM
2D-Arrays kbyrne New To Java 1 02-07-2008 10:08 PM
Initializing variables using constructors Java Tip Java Tips 0 01-13-2008 08:28 PM
Initializing property object from a file Java Tip Java Tips 0 12-29-2007 04:55 PM
Initializing SAXParser Java Tip Java Tips 0 12-14-2007 06:33 PM


All times are GMT +2. The time now is 01:49 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org