java中数组的定义及使用(转)

本文详细介绍了Java中数组的概念、声明及初始化方式,包括一维数组、二维数组及其操作方法,并提供了多个示例帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载:http://blog.sina.com.cn/s/blog_7b83134b01016fmf.html

数组:是一组相关变量的集合

数组是一组相关数据的集合,一个数组实际上就是一连串的变量,数组按照使用可以分为一维数组、二维数组、多维数组
数据的有点
不使用数组定义100个整形变量:int i1;int i2;int i3
使用数组定义 int i[100];
数组定义:int i[100];只是一个伪代码,只是表示含义的
一维数组

一维数组可以存放上千万个数据,并且这些数据的类型是完全相同的,
使用java数组,必须经过两个步骤,声明数组和分配内存给该数组,
声明形式一
声明一维数组:数据类型 数组名[]=null;
非配内存给数组:数组名=new 数据类型[长度];
声明形式二
声明一维数组:数据类型 [] 数组名=null;

java数据类型分为两大类
基本数据类型
int、long操作的时候本身就是具体的内容
引用数据类型:数组、类、接口

引用传递的就是一个内存的使用权,一块内存空间,可能有多个人同时使用

事例声明数组

package com.qn.array;

public class Test {
<wbr><wbr><wbr>public static void main(String[] args) {<br><wbr><wbr>int score[]=null;//声明数组<br><wbr><wbr>score=new int[3];//开辟空间,大小为3<br><wbr>}<br> }<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


数组的声明格式里,数据类型是数组元素的数据类型,常见的有整形、浮点型、与字符型等
数组名是用来统一这组相同数据类型元素的名称,其命名规则和变量的相同
数组声明后实际上是在栈内存中保存了此数组的名称,结下了是要在堆内存中配置数组所需要的内存,齐产固定是告诉编译器,所声明的数组要存放多少个元素,而new 则是命令编译器根据括号里的长度

<wbr></wbr>

基本数据类型偶读有其默认值:int 0;只要是引用数据类型默认值就是null

事例

package com.qn.array;

public class Test {
<wbr><wbr><wbr>public static void main(String[] args) {<br><wbr><wbr>int score[]=null;//声明数组<br><wbr><wbr>score=new int[3];//开辟空间,大小为3<br><wbr><wbr>System.out.print("score[0]="+score[0]);<br><wbr><wbr>System.out.print("\tscore[1]="+score[1]);<br><wbr><wbr>System.out.print("\tscore[2]="+score[2]);<br><wbr>}<br> }<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


堆栈内存的解释
数组操作中,在栈内存中保存的永远是数组的名称,只开辟了栈内的空间,数组是永远无法使用的,必须有指向的对内存才可以使用,要想开辟新对内存空间必须使用new关键字,之后就是将对内存的使用权交给对应的栈内存,而且一个堆内存空间可以同时被多个栈内存空间指向,比如一个人可以有多个名字,人就相当于对内存,名字就相当于栈内存

声明数组的同时分配内存空间
声明数组的同时非配内存
数据类型 数组名[]=new 数据类型[个数]
int score[]=new int[10];
声明一个元素个数为10的整形数组score,同时开辟依靠内存空间工期使用
java中,由于整形数据类型占用的空间为4个byte,而整个数组score可保存的元素有10个。所以上例中占用的内存共有4*10=40个字节


<wbr>数组的访问</wbr>

数组中元素的表示方法
想要访问数组里的元素可以利用索引来完成,java的数组索引标号由10开始,以一个score[10]的整形数组为例,score[0]代表第一个元素
一直向下,最后一个为score[9]



取得数组的长度
在java中取得数组的长度(也就是数组元素的长度)可以利用数组名称.length完成,
数组名称.length--返回一个int类型的数据

package com.qn.array;

public class Test {
<wbr><wbr><wbr>public static void main(String[] args) {<br><wbr><wbr>int score[]=null;//声明数组<br><wbr><wbr>score=new int[3];//开辟空间,大小为3<br><wbr><wbr>System.out.println(score.length);<br><wbr>}<br> }<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

结果


数组的静态初始化
之前的数组,所采用的都是动态初始化,所有的内容在数组声明的时候并不具体的指定,而是以默认值的形式出现
静态初始化是指在数组声明后直接为数组指定具体的内容
如果想要直接在声明的时候给数组赋初始值,可以采用大括号完成,只要在数组的生命格式后面加上初值的赋值即可,
数据类型 数组名 []={初始值0,初始值1,初始值3,....初始值n};

package com.qn.array;

public class Test {
<wbr><wbr><wbr>public static void main(String[] args) {<br><wbr><wbr>int score[]={1,2,3,4,5,6};//使用静态初始化声明数组<br><wbr><wbr>System.out.println(score.length);<br><wbr>}<br> }<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

结果

6

范例求出数组中的最大值和最小值

package com.qn.array;

public class Test {
<wbr><wbr><wbr>public static void main(String[] args) {<br><wbr><wbr>int score[]={45,12,8,23,89,56};//使用静态初始化声明数组<br><wbr><wbr>int max=0;<br><wbr><wbr>int min=0;<br><wbr><wbr>max=min=score[0];<br><wbr><wbr>for (int i = 0; i &lt; score.length; i++) {<br><wbr><wbr><wbr>if(score[i]&gt;max){<br><wbr><wbr><wbr><wbr>max=score[i];<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>if(score[i]<br><wbr><wbr><wbr><wbr>min=score[i];<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr><br><wbr><wbr>}<br><wbr><wbr><br><wbr><wbr>System.out.println("最大值:"+max);<br><wbr><wbr>System.out.println("最小值:"+min);<br><wbr>}<br> }<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

结果

<wbr></wbr>

范例排序,在操作中排序是比较常用的

从大到小

package com.qn.array;

public class Test {
<wbr><wbr><wbr>public static void main(String[] args) {<br><wbr><wbr>int score[]={45,12,8,23,89,56};//使用静态初始化声明数组<br><wbr><wbr>int temp=0;<br><wbr><wbr>for (int i = 0; i &lt; score.length; i++) {<br><wbr><wbr>for (int j = 0; j &lt; score.length-1; j++) {<br><wbr><wbr><wbr>if(score[i]&gt;score[j]){<br><wbr><wbr><wbr><wbr>temp=score[i];<br><wbr><wbr><wbr><wbr>score[i]=score[j];<br><wbr><wbr><wbr><wbr>score[j]=temp;<br><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr>}<br><wbr><wbr><wbr><br><wbr><wbr>}<br><wbr>}<br><wbr><wbr>for (int i = 0; i &lt; score.length; i++) {<br><wbr><wbr><wbr>System.out.print(score[i]+"\t");<br><wbr><wbr>}<br><wbr><wbr>}<br> }<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

结果



这个时候不要被i值所迷惑<wbr><wbr><wbr>if(score[i]&gt;score[j]){</wbr></wbr></wbr>

这一步主要知识为了比较,实际上完成之后输出的时候是根据j的值排序的

<wbr></wbr>

二维数组
如果可以把一维数组当做几何中的一条线图形,那么二维数组就相当于一个表格
<wbr><wbr><wbr>A<wbr><wbr><wbr>B<br> 1<wbr>姓名 年龄<br> 2<wbr>齐宁<wbr>21<br> 3<wbr>齐燕<wbr>23<br> 4<wbr>齐威<wbr>26</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

二维数组声明的方式和以为数组的类似,内存分配也一样是用new这个关键字
其实声明与分配内存的格式如下
动态初始化
数据类型 数组名[][];
数组名=new 数据类型[行的个数][列的个数];

声明并初始化数组
数据类型 数组名[][]=new 数据类型[行的个数][列的个数];
静态初始化

二维数组的存储
声明二维数组score 同时开辟一段内存空间
int score[][]=new int[4][3];
整体数据score可保存的元素是4*3=12个,在java中,int数据类型所占用的空间为4个字节,因此该整形数组占用的内存共为4*12=48个字节
事例

package com.qn.array;

public class test1 {
<wbr>public static void main(String[] args) {<br><wbr><wbr>int score[][] = new int[4][3];<br><wbr><wbr>score[0][1] = 30;<br><wbr><wbr>score[1][0] = 31;<br><wbr><wbr>score[2][1] = 32;<br><wbr><wbr>score[2][2] = 33;<br><wbr><wbr>score[3][1] = 34;<br><wbr><wbr>score[1][1] = 35;<br><wbr><wbr>for (int i = 0; i &lt; score.length; i++) {<br><wbr><wbr><wbr>for (int j = 0; j &lt; score[i].length; j++) {<br><wbr><wbr><wbr><wbr>System.out.print(score[i][j]+"\t");<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>System.out.println("");<br><wbr><wbr>}<br><wbr>}<br> }<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

结果


不规则二维数组

long[][] numfive=new long[5][];<wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)">//定义一个long类型的不规则数组</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)">numfive[0]=new long[5];</span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)">//为第1行分配5列</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)">numfive[1]=new long[6];</span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)">//为第2行分配6列</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)">numfive[2]=new long[7];</span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)">//为第3行分配7列</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)">numfive[3]=new long[8];</span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)">//为第4行分配8列</span><br style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)">numfive[4]=new long[9];</span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)"></span><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><wbr style="color:rgb(73,73,73); font-family:simsun; font-size:14px"><span style="color:rgb(73,73,73); font-family:simsun; font-size:14px; background-color:rgb(234,222,189)">//为第5行分配9列</span><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


二维数组静态初始化

<wbr></wbr>

用到的时候才会开辟空间,不用的(红色部分)则不开辟空间

多维数组

一般只是用到二维数组

三维数组简单了解

package com.qn.array;

public class test1 {
<wbr>public static void main(String[] args) {<br><wbr><wbr>int score[][][]={<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>{5,1},{6,7}<br><wbr><wbr><wbr><wbr>},<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr>{9,4},{8,3}<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr>};<br><wbr><wbr>System.out.print(score[0][0][0]+"\t");<br><wbr><wbr>System.out.print(score[0][0][1]+"\t");<br><wbr><wbr>System.out.print(score[0][0][0]+"\t");<br><wbr><wbr>System.out.print(score[0][0][1]+"\t");<br><wbr><wbr><br><wbr><wbr>System.out.print(score[1][0][0]+"\t");<br><wbr><wbr>System.out.print(score[1][0][1]+"\t");<br><wbr><wbr>System.out.print(score[1][1][0]+"\t");<br><wbr><wbr>System.out.print(score[1][1][1]+"\t");<br><wbr>}<br> }<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

<wbr></wbr>

<wbr></wbr>

http://hi.baidu.com/vip_qining/item/7638e85d1d8a4a0adb16355a

参考:java 中数组的十大用法


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值