This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initializing Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initializing Arrays" Watch "Initializing Arrays" New topic
Author

Initializing Arrays

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
How many ways can an array be initialized? I remember someone had a very odd way of initializing an array but I can't remember how it was done. I know there is...
String myStrings[] = {"blah", "blah"};
String myString[] = new String[2];
Are there others?
Thanks
Dale


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Rick Salsa
Ranch Hand

Joined: Jul 17, 2001
Posts: 173
You could also do:
int a[]=new int[] {1,2,3,4};
int b[][] = new int[3][];
int b[][] = new int[3][3];
That's all I can think of off the top if my head.
/rick
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
For One Dimensional arrays here is one more:
String myString[] = new String[] {"blah", "blah",};
Note the comma in the end - it is allowed in Java.
Brian
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Here is one more!
Class A {}
A ref[] = {new A(), new A(), new A()};


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
 
Consider Paul's rocket mass heater.
 
subject: Initializing Arrays