File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initializing Arrays" Watch "Initializing Arrays" New topic

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?

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.
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.
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="" 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
I agree. Here's the link:
subject: Initializing Arrays
It's not a secret anymore!