Home > Java > javaTutorial > Learn the declaration and application of Java arrays from the basics

Learn the declaration and application of Java arrays from the basics

PHPz
Release: 2024-02-24 08:30:08
Original
553 people have browsed it

Learn the declaration and application of Java arrays from the basics

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.

  1. Definition of array
    Java array is a fixed-length, continuous storage container that can access and modify data according to the index position. Declaring an array requires specifying the array type and length.

Example:

int[] nums = new int[5]; // 创建一个长度为5的整型数组
String[] names = new String[3]; // 创建一个长度为3的字符串数组
double[] points = new double[10]; // 创建一个长度为10的双精度数组
Copy after login
  1. Access and assignment of arrays
    Access and modify the value of array elements through index positions. The index of the array starts from 0, and the maximum index value is the length of the array minus 1.

Example:

nums[0] = 10; // 修改数组第一个元素的值为10
int value = nums[1]; // 将数组第二个元素的值赋给变量value
Copy after login
  1. Initialization of array
    While declaring the array, you can directly assign initial values ​​to the array elements.

Example:

int[] nums = {1, 2, 3, 4, 5}; // 直接将数组元素赋初值
String[] names = {"Alice", "Bob", "Charlie"}; // 直接将字符串数组元素赋初值
Copy after login
  1. Array traversal
    Traverse all elements of the array in a loop, you can use a for loop or a foreach loop.

Example:

for (int i = 0; i < nums.length; i++) {
    System.out.println(nums[i]);
}

for (String name : names) {
    System.out.println(name);
}
Copy after login
  1. Multidimensional arrays
    Java also supports multidimensional arrays, which can be defined as arrays containing other arrays. Multidimensional arrays can be thought of as tables or matrices, and elements can be accessed through row and column indexes.

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template