Dave's Brain

Browse - Programming Tips - How do I initialize an array?

Date: 2011mar30
Language: Java
Keywords: init

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} };

Add a comment

Sign in to add a comment
Copyright © 2008-2018, 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.