How to define an array and give it values in Java?
In Java, an array can be defined and assigned values as follows:
// 定义一个整型数组并赋值
int[] numbers = {1, 2, 3, 4, 5};
// 定义一个字符串数组并赋值
String[] names = {"Alice", "Bob", "Charlie", "David"};
// 定义一个二维数组并赋值
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
In the examples above, an integer array named numbers, a string array named names, and a two-dimensional integer array named matrix are defined and assigned values through the curly braces {}. When defining an array, you need to specify the array type and size. If you want to simultaneously define and assign values, you can directly fill in the corresponding values in the curly braces.