Detailed introduction and example analysis of Java two-dimensional arrays
What is an array
An array (Array) is an ordered sequence of elements. If a limited collection of variables of the same type is named, then the name is the array name. The individual variables that make up an array are called components of the array, also called elements of the array, sometimes also called subscript variables/12713827). The numeric number used to distinguish the individual elements of an array is called a subscript. In programming, an array is a form of organizing several elements of the same type in an orderly manner for the convenience of processing. These ordered collections of similar data elements are called arrays. Arrays are collections used to store multiple data of the same type.
Example (equipment column)
Array, elements and subscripts:
For example, when playing King of Glory, everyone must produce equipment , each one has its own equipment slot. Then this equipment column is an array, the equipment in it is the element, and the position where the equipment is placed is the subscript. That is to say, each subscript corresponds to an equipment, and the subscript starts from 0, so the subscript corresponding to the first equipment is 0
Declaration array
int type
Fix the array length when declaring the array, and the length of the array remains unchanged. There are two declaration methods, the first is direct assignment when declaring. The second type does not assign a value when declared, but has a fixed length. Although there is no assignment, all elements will be assigned a value of 0 by default.
public class Test { public static void main(String[] args) { //声明int类型数组并初始化赋值 int[] a={1,2,3,4,5,}; //声明数组设值数组长度,并初始化全为0 int[] b=new int[10]; } }
String type
There is no difference from the above and there are two declaration methods.
public class Test { public static void main(String[] args) { //声明int类型数组并初始化赋值 int[] a={1,2,3,4,5,}; //声明数组设值数组长度,并初始化全为0 int[] b=new int[10]; //声明String类型数组并初始化赋值 String[] d={"aa","bb","cc"}; //声明数组固定长度,默认初始化全为0 String[] c=new String[10]; } }
Array operation
Traverse the array
Traverse the array: two methods, for loop and for in loop
for loop, here I am in the array Three elements are placed, namely equipment. Loop output, starting from the subscript 0. zb.length is the size of this array
public class Test { public static void main(String[] args) { String[] zb={"冷静之靴","泣血之刃","名刀司命"}; for (int i = 0; i < zb.length; i++) { System.out.println(zb[i]); } } }
Result:
for in loop, forgot For how to use this cycle, you can refer to the previous article. The Golden Elixir article has a detailed introduction.
public class Test { public static void main(String[] args) { String[] zb={"冷静之靴","泣血之刃","名刀司命"}; for (String s : zb) { System.out.println(s); } } }
Two-dimensional array
A two-dimensional array is essentially an array with an array as an array element, that is, an "array of arrays", type specifier array name [constant expression] [constant expression Mode]. A two-dimensional array is also called a matrix, and a matrix with equal numbers of rows and rows is called a square matrix. Symmetric matrix a[i][j] = a[j][i], diagonal matrix: There are zero elements outside the main diagonal of an n-order square matrix.
A two-dimensional array is an ordinary one-dimensional array. Each element is a one-dimensional array, and the combination is a two-dimensional array.
Continue using the previous example. At the beginning of each game, one side's data panel has a default sorting (the panel showing equipment and economy). Each person has an equipment slot, which is equivalent to an array. Then there are five equipment columns (one team) on the information panel, and they are arranged in the default order, which is also equivalent to an array. An equipment slot counts as one element, and the position of the equipment slot is the subscript. But each element in this array is also an array, so the data panel is equivalent to a two-dimensional array.
Declaring a two-dimensional array
There is no difference between declaring a two-dimensional array and declaring an array. There are two situations.
public class Test { public static void main(String[] args) { //声明二维数组并赋值 int[][] a={{123},{456},{789}}; //声明二维数组固定大小 int[][] ns = new int[3][5]; } }
Some readers may still be confused by using the King of Glory to introduce the two-dimensional array. I'm a little confused, now type out the above example in code.
I can’t remember the name of the equipment here. I copied the equipment of the next three people directly
public class Test { public static void main(String[] args) { //五个人,每个人装备栏有三个装备。 String[][] wzry=new String[5][3]; //给第一个人买装备,就是给第一个数组赋值 wzry[0][0]="宝石"; wzry[0][1]="血刀"; wzry[0][2]="金身"; //给第二个人买装备,就是给第二个数组赋值 wzry[1][0]="铁剑"; wzry[1][1]="草鞋"; wzry[1][2]="护甲"; //给第三个人买装备,就是给第三个数组赋值 wzry[2][0]="宝石"; wzry[2][1]="血刀"; wzry[2][2]="金身"; //给第四个人买装备,就是给第四个数组赋值 wzry[3][0]="宝石"; wzry[3][1]="血刀"; wzry[3][2]="金身"; //给第五个人买装备,就是给第五个数组赋值 wzry[4][0]="宝石"; wzry[4][1]="血刀"; wzry[4][2]="金身"; } }
Now let’s run it and see what everyone’s equipment has
//第三个人的第二个装备 System.out.println("第三个人的第二个装备"); System.out.println(wzry[2][1]); //第一个人的第三个装备 System.out.println("第一个人的第三个装备"); System.out.println(wzry[0][2]); //第五个人的全部装备 System.out.println("第五个人的全部装备"); for (int i = 0; i < 3; i++) { System.out.println(wzry[4][i]); }
result:
The above is the detailed content of Detailed introduction and example analysis of Java two-dimensional arrays. For more information, please follow other related articles on the PHP Chinese website!

JVM handles operating system API differences through JavaNativeInterface (JNI) and Java standard library: 1. JNI allows Java code to call local code and directly interact with the operating system API. 2. The Java standard library provides a unified API, which is internally mapped to different operating system APIs to ensure that the code runs across platforms.

modularitydoesnotdirectlyaffectJava'splatformindependence.Java'splatformindependenceismaintainedbytheJVM,butmodularityinfluencesapplicationstructureandmanagement,indirectlyimpactingplatformindependence.1)Deploymentanddistributionbecomemoreefficientwi

BytecodeinJavaistheintermediaterepresentationthatenablesplatformindependence.1)Javacodeiscompiledintobytecodestoredin.classfiles.2)TheJVMinterpretsorcompilesthisbytecodeintomachinecodeatruntime,allowingthesamebytecodetorunonanydevicewithaJVM,thusfulf

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),whichexecutesbytecodeonanydevicewithaJVM.1)Javacodeiscompiledintobytecode.2)TheJVMinterpretsandexecutesthisbytecodeintomachine-specificinstructions,allowingthesamecodetorunondifferentp

Platform independence in JavaGUI development faces challenges, but can be dealt with by using Swing, JavaFX, unifying appearance, performance optimization, third-party libraries and cross-platform testing. JavaGUI development relies on AWT and Swing, which aims to provide cross-platform consistency, but the actual effect varies from operating system to operating system. Solutions include: 1) using Swing and JavaFX as GUI toolkits; 2) Unify the appearance through UIManager.setLookAndFeel(); 3) Optimize performance to suit different platforms; 4) using third-party libraries such as ApachePivot or SWT; 5) conduct cross-platform testing to ensure consistency.

Javadevelopmentisnotentirelyplatform-independentduetoseveralfactors.1)JVMvariationsaffectperformanceandbehavioracrossdifferentOS.2)NativelibrariesviaJNIintroduceplatform-specificissues.3)Filepathsandsystempropertiesdifferbetweenplatforms.4)GUIapplica

Java code will have performance differences when running on different platforms. 1) The implementation and optimization strategies of JVM are different, such as OracleJDK and OpenJDK. 2) The characteristics of the operating system, such as memory management and thread scheduling, will also affect performance. 3) Performance can be improved by selecting the appropriate JVM, adjusting JVM parameters and code optimization.

Java'splatformindependencehaslimitationsincludingperformanceoverhead,versioncompatibilityissues,challengeswithnativelibraryintegration,platform-specificfeatures,andJVMinstallation/maintenance.Thesefactorscomplicatethe"writeonce,runanywhere"


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
