In Java, there are two main types of array structures:
// Create an array of integers with 5 elements int[] myArray = new int[5]; // Initialize the array with values myArray[0] = 1; myArray[1] = 2; myArray[2] = 3; myArray[3] = 4; myArray[4] = 5;
You can also use the shorthand syntax to create and initialize a single-dimensional array in Java:
int[] myArray = {1, 2, 3, 4, 5};
// Create a 2D array with 3 rows and 4 columns int[][] myMatrix = new int[3][4]; // Initialize the array with values myMatrix[0][0] = 1; myMatrix[0][1] = 2; myMatrix[0][2] = 3; myMatrix[0][3] = 4; myMatrix[1][0] = 5; myMatrix[1][1] = 6; myMatrix[1][2] = 7; myMatrix[1][3] = 8; myMatrix[2][0] = 9; myMatrix[2][1] = 10; myMatrix[2][2] = 11; myMatrix[2][3] = 12;
You can also use the shorthand syntax to create and initialize a multidimensional array in Java:
int[][] myMatrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};