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!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!
