Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initializing Arrays

 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rick Salsa
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is one more!
Class A {}
A ref[] = {new A(), new A(), new A()};
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic