Dave's Brain

Browse - programming tips - java initialize array

Date: 2011mar30
Language: Java


Q.  How do I initialize an array?

A.  I do it this way:

	int[] myArray = { 1, 2, 3 }

which is nice and compact.
Some books tell you to do:

	int[] myArray = new int[] { 1, 2, 3 };

which works but is more verbose.

Initialize a 2D array in this way:

	int[][] twoDimArray = { {1,2,3}, {4,5,6}, {7,8,9} };
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: