Gareth D.
—How do I declare and initialize an array in Java?
The easiest way to declare and initialize an array of a primitive type such as int
in Java is by using the following syntax.
int[] myArray = new int[]{1, 2, 3};
This does a few things at once.
int[] myArray
says that we want to declare a new variable called myArray
that has the type of an array of integers.new int[]
says we want to immediately allocate memory to store data in this array.{1,2,3}
is the data we want to store in the array. Java will count the elements in our initial array and automatically initialize the array with enough memory for three int
s.Here’s a runnable example:
class Main { public static void main(String[] args) { // declare the variable and allocate memory int[] myArray = new int[]{1, 2, 3}; for (int i = 0; i < 3; i++) { System.out.println(myArray[i]); } } }
Which will print out:
1 2 3
If you don’t want to hard code the data immediately when you initialize the array, you can instead create an empty array (with all values initialized to zero) as follows.
int[] myArray = new int[3];
Here, you have to explicitly give the size of the array. In our example, we’ve created an array that can store 3 int
s, all of which are zero.
Here’s a runnable example of dynamically setting the values in an array.
class Main { public static void main(String[] args) { // declare the variable and allocate memory int[] myArray = new int[3]; myArray[1] = 100; for (int i = 0; i < 3; i++) { System.out.println(myArray[i]); } } }
Which will print:
0 100 0
You can use the same syntax to create arrays to store other objects. In the example below, we use Integer
instead of int
, which is very similar to the above code but initializes the default values to null
instead of 0
.
class Main { public static void main(String[] args) { // declare the variable and allocate memory Integer[] myArray = new Integer[3]; myArray[1] = 100; for (int i = 0; i < 3; i++) { System.out.println(myArray[i]); } } }
The code prints:
null 100 null
You can create a 2D array in Java by adding a second set of square brackets, for example:
int[][] myArray = new int[4][2];
This will create an array with four rows and two columns. Here’s a runnable example:
class Main { public static void main(String[] args) { // declare the variable and allocate memory int[][] myArray = new int[4][2]; myArray[2][1] = 1; for (int i = 0; i < 4; i++) { for (int j = 0; j < 2; j++) { System.out.print(myArray[i][j] + " "); } System.out.println(); } } }
This prints the following (we set the value of the third row and second column to 1, and the rest of the values all have the default 0).
0 0 0 0 0 1 0 0
You can initialize more dimensions by adding more square brackets, for example:
int[][][] myArray = new int[4][2][3];
Arrays with three or more dimensions are harder to visualize, but here’s a runnable example that prints it out as four sets of 2-by-3 arrays.
class Main { public static void main(String[] args) { // declare the variable and allocate memory int[][][] myArray = new int[4][2][3]; myArray[2][1][2] = 1; for (int i = 0; i < 4; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 3; k++) { System.out.print(myArray[i][j][k] + " "); } System.out.println(); } System.out.println("____"); } } }
This will print:
0 0 0 0 0 0 ____ 0 0 0 0 0 0 ____ 0 0 0 0 0 1 ____ 0 0 0 0 0 0 ____
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.