当前位置:网站首页>The use of two-dimensional array (including the definition of two-dimensional array, the declaration and initialization of two-dimensional array (dynamic initialization, static initialization), common
The use of two-dimensional array (including the definition of two-dimensional array, the declaration and initialization of two-dimensional array (dynamic initialization, static initialization), common
2022-07-24 13:09:00 【Cording..】
The use of two-dimensional arrays ( Including the definition of two-dimensional array , Two dimensional array declaration and initialization ( dynamic initialization , initiate static ), Common assignment methods for binary arrays ( dynamic initialization , Assignment of static initialization ), Wrong definition, assignment method, etc )
The details are in the code comments , Very detailed , Read carefully and you will get something
package com.atcording.java;
/*
* The use of two-dimensional arrays
*
* 1. understand :
* Understanding of two-dimensional arrays , We can think of it as a one-dimensional array array1 As another one-dimensional array array2 Of the elements of .
* Actually , From the operating mechanism of the underlying array , There's no multidimensional array .
*
* 2. The use of two-dimensional arrays :
* ① Two dimensional array declaration and initialization
* ② How to call the element in the specified position of the array
* ③ How to get the length of an array
* ④ How to traverse arrays
* ⑤ The default initialization value of an array element : see ArrayTest3.java
* ⑥ Memory parsing of arrays : see ArrayTest3.java
*
*
*/
public class ArrayTest2 {
public static void main(String[] args) {
//1. Two dimensional array declaration and initialization
int[] arr = new int[]{1,2,3};// Assignment of one-dimensional array
// initiate static ( Assign values directly after definition )
int[][] arr1 = new int[][]{
{1,2,3},{4,5},{6,7,8}};
/* A two-dimensional array can be simply understood as a one-dimensional array
Nesting of , This two bit array is equivalent to defining a one-dimensional array with three large elements , And the three big elements contain
Different small elements , A pair of braces in braces is equivalent to a large element , The first big element is {1,2,3}, The small element is
1,2,3 Three ,3 In the third small element of the first large element arr[0][2]==3,arr[1][1]==5*/
// dynamic initialization 1( Dynamic initialization refers to defining an empty array first , After the definition is completed, it can be assigned at any time )
String[][] arr2 = new String[3][2];// When defining, both inner and outer elements define
/* dynamic initialization 1 assignment
for example :arr2[0][0]=2;arr2[0][1]=3;arr2[1][0]=6; etc. */
// dynamic initialization 2
String[][] arr3 = new String[3][];// Only outer elements are defined , Undefined inner layer
/* dynamic initialization 2 assignment :
arr3[1] = new String[4];( Equivalent to after assignment arr3[1] There are four empty elements inside, and then you can assign values one by one )
arr3[1][0]="ni";arr3[1][1]="nih"; etc.
*/
// Wrong situation
// String[][] arr4 = new String[][4];
// String[4][3] arr5 = new String[][];
// int[][] arr6 = new int[4][3]{
{1,2,3},{4,5},{6,7,8}};
// It's also the right way to write :
int[] arr4[] = new int[][]{
{1,2,3},{4,5,9,10},{6,7,8}};
int[] arr5[] = {
{1,2,3},{4,5},{6,7,8}};
//2. How to call the element in the specified position of the array
System.out.println(arr1[0][1]);// Output results :2
System.out.println(arr2[1][1]);
/* Output results :null( Because array arr2 Initialize for dynamic , Not assigned, so it is empty )*/
arr3[1] = new String[4];
System.out.println(arr3[1][0]);
//3. Gets the length of the array
System.out.println(arr4.length);// Output results :3
/*int[] arr4[] = new int[][]{
{1,2,3},{4,5,9,10},{6,7,8}};arr4 There are three parentheses in the array braces
Number , There are three major elements */
System.out.println(arr4[0].length);// Output results :3( It is equivalent to that the first big element has 3 A small element )
System.out.println(arr4[1].length);// Output results :4( It is equivalent to that the second big element has 4 A small element )
//4. How to traverse a two-dimensional array
for(int i = 0;i < arr4.length;i++){
for(int j = 0;j < arr4[i].length;j++){
System.out.print(arr4[i][j] + " ");
}
System.out.println();
}
}
}
Code 2:
/*
* The use of two-dimensional arrays :
* Regulations : The two-dimensional array is divided into the elements of the outer array , The elements of the inner array
* int[][] arr = new int[4][3];
* Outer elements :arr[0],arr[1] etc.
* Inner elements :arr[0][0],arr[1][2] etc.
*
* ⑤ The default initialization value of an array element
* For initialization mode 1 : such as :int[][] arr = new int[4][3];
* The initialization value of the outer element is : Address values
* The initialization value of the inner element is : Same as one-dimensional array initialization
*
* For initialization mode 2 : such as :int[][] arr = new int[4][];
* The initialization value of the outer element is :null
* The initialization value of the inner element is : Cannot call , Otherwise, the report will be wrong .
*
* ⑥ Memory parsing of arrays
*
*/
public class ArrayTest3 {
public static void main(String[] args) {
// Define an integer array
int[][] arr = new int[4][3];
System.out.println(arr[0]);// Output results :[[email protected]
System.out.println(arr[0][0]);// Output results :0
// System.out.println(arr);// Output results :[[[email protected]
// Define floating point arrays
System.out.println("*****************");
float[][] arr1 = new float[4][3];
System.out.println(arr1[0]);// Output results : Address values
System.out.println(arr1[0][0]);// Output results :0.0
System.out.println("*****************");
// Define a string array
String[][] arr2 = new String[4][2];
System.out.println(arr2[1]);// Output results : Address values
System.out.println(arr2[1][1]);// Output results :null( The array is empty because it is not assigned a value )
System.out.println("*****************");
double[][] arr3 = new double[4][];
System.out.println(arr3[1]);// Output results :null( The array is empty because it is not assigned a value )
// System.out.println(arr3[1][0]);// Report errors ( Only the outer element is defined, and the inner element is not defined, so an error is reported )
}
}
边栏推荐
- 自己实现is_default_constructible
- 2022.07.21
- The basis of point graph in the map of life information and knowledge
- Modification of EAS login interface
- The core capability of accelerating enterprise data application innovation flexibility
- Generator and async solve asynchronous programming
- 基于matlab的语音处理
- Data + AI summit 2022 PPT download
- Efficientformer: lightweight vit backbone
- SSM在线考试系统含文档
猜你喜欢

Analysis of ISP one click download principle in stm32

Solutions to problems in IE6 browser

如何画 贝赛尔曲线 以及 样条曲线?

EAS environment structure directory

基于boost库的搜索引擎

EAS approval process related table

Node takes effect after using NVM to install under Windows system, but NPM does not take effect

36. Delete the penultimate node of the linked list

SSM医院住院管理系统

Nearly 65billion pieces of personal information were illegally handled in seven years, and the investigation of didi network security review case was announced
随机推荐
It is difficult for Chinese consumers and industrial chains to leave apple, and iPhone has too much influence
树莓派自建 NAS 云盘之——数据自动备份
中国消费者和产业链都很难离开苹果,iPhone的影响力太大了
Redis(13)----浅谈Redis的主从复制
编写浏览器插件
How can flinksql run in perjob mode on yarn? I submit tasks on SqlClient
Digital intelligence integration accelerates enterprise business innovation
SSM online examination system including documents
基于boost库的搜索引擎
2022.07.15 暑假集训 个人排位赛(十)
The EAS BOS development environment client cannot be started, but the server does show that it is ready
Promise
Research on data governance quality assurance
ESP32ADC
使用Jenkins搭建CI服务器
Arduino框架下ESP32 EEPROM库函数实现对各数据类型保存示例
27. Longest increasing subsequence
C code specification
The basis of point graph in the map of life information and knowledge
[C language] dynamic memory management