Learn the definition and usage of Java arrays from scratch
Java array is a commonly used data structure used to store and manage large amounts of data of the same type. Knowing how to define and use Java arrays is an important step in learning and understanding this programming language. This article will introduce the basic concepts and usage of Java arrays from scratch, and provide specific code examples.
Example:
int[] nums = new int[5]; // 创建一个长度为5的整型数组 String[] names = new String[3]; // 创建一个长度为3的字符串数组 double[] points = new double[10]; // 创建一个长度为10的双精度数组
Example:
nums[0] = 10; // 修改数组第一个元素的值为10 int value = nums[1]; // 将数组第二个元素的值赋给变量value
Example:
int[] nums = {1, 2, 3, 4, 5}; // 直接将数组元素赋初值 String[] names = {"Alice", "Bob", "Charlie"}; // 直接将字符串数组元素赋初值
Example:
for (int i = 0; i < nums.length; i++) { System.out.println(nums[i]); } for (String name : names) { System.out.println(name); }
Example:
int[][] matrix = new int[3][3]; // 创建一个3x3的二维数组 matrix[1][2] = 10; // 修改二维数组第2行第3列的元素值为10 int[][][] cube = new int[2][3][4]; // 创建一个2x3x4的三维数组 cube[0][1][2] = 5; // 修改三维数组第1个面第2行第3列的元素值为5
The above is to learn the basic concepts and usage of Java arrays from scratch. By understanding the definition of arrays, access, assignment, initialization and traversal operations, as well as the use of multi-dimensional arrays, you can better master the Java programming language. I hope the specific code examples in this article can help you better learn and apply Java arrays.
The above is the detailed content of Learn the declaration and application of Java arrays from the basics. For more information, please follow other related articles on the PHP Chinese website!