java基础-数组

时间:2021-09-02 15:15:58

浏览以下内容前,请点击并阅读 声明

定义:数组是一个能容纳固定数量,类型单一的若干个值的容器。注意,数组是一个对象。

  数组一旦创建,则其长度固定不变,数组中的所有值叫元素(Element),获取元素要靠索引号(index),索引号从0开始,对应第一个元素。

aaarticlea/gif;base64,R0lGODlhTwF8APcAAP///+rq6gAAAN/v3+Hw4d3u3ePx4+Xy5efz5+Lw4mVvZejz6JmZmeTx5Oby5ur06tzt3Oz17ODv4Nvt297u3un06T8/P+727uv169rs2ru7u8Lhwtns2e327fD38O/376qqqhEREYiIiPL48t7v3uDw4Nfr1yIiItzu3N3d3cPhw8zMzNjs2Mfjx2ZmZu7u7tjr2DMzM8jHx0RERPT59NXq1dbr1vH48Xd3d9rt2tbq1sTixLLTssXixVVVVZGPj8LgwsvLy/P588rRysbjxhAQEKjIqPH38cPiw/X69YyMjIeHh8HgwcjQyMjkyC8vL8zSzD4+PtTq1Nra2vT09MnRyYmHh1dUVaysrB8fH319ffr6+pycnMjjyPP488bixn9/f6nJqdHo0b+/v8vSy9Tp1F5eXqnIqW1tbUNOQ3J8ck5OTsnUyYqhioGDgYeQh9Pp04mRicvRy7HTsc7TzlFUUYuii56lnrnEuafIp2BsYGNuY/b69k1STYmgibnduc3SzVNVU7PTs87mzsThxIKEgsHIwbHWscnQyYyijHqAerHSsaGmoaOno1RVVE9TTwwMDMfQx36CfsbPxvX59b/fv1plWp+fn9/f383Tza+vr7rduu/v72JtYjtCO19sX8fKx9Dn0MrlymFtYU5TTrfDt8zmzFJUUnV1ddfs18/PzyUoJc7Wzs/Tz3yCfJKSkoWPhUhISGNvYysrK83azdDX0L6+vr7Gvtvb28jLyMTJxHiAeMzVzLzFvOPj429vb3Vyc9LY0qrKqlBTUNHp0U5STtbV1cvly1hcWM/Uz3uBe19pX4iKiFJaUsXKxaK5omNpYxwcHFppWo6kji83L8DHwGRvZI2NjTo6Ojg+OE5aToafhkZTRqbHpnd/d2p5aldXV83NzWJuYqGhoVp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2A700XiGQAEiToAAcQJHiBQzBJw4a3nsCYJzM4eIkaLvCRcCYmh94SKE5c1vvfo4Ybag/1cADKTOMB36Ld8ZLgS6eNzUTGJOT8eYeVtkscAXVr2aFmA2BQMBM0DEcJHCqwYcoAHEwBE6bgwfKVyAxt7dRwwA4TWskBr97OYVXlcQxL5CgIYUaCsohRTsCoEvBkJwSpXmfinMKE6eeOutdiyw8EIMM9RwQw479PDDDRVKEAD7tsoLO6ZASCE/uNwi8D/z/GMquhVQzO0r2wAQ4au68rJLIOzcEmGFF7QyyC67sCsIRCabdPJJCyCZUIA1kDqsiLeyeA4AzSaEL8mvAEiNtLk4G9E87KYEQckbt+ILt7r4+rFNyaobzDyBDgRSwaY4mRA5py6xQIAxFiuQAcCwO9kUyK/c2s9NMWcIrqAWBSAwBRGuC1PJGE7kMS4k+WIULwAC5JNS4ISL1CkwqCw0LVVeTYssgkLAAUwA3JovtbPcghFPW1O47AU2lSQLMKlMhHTZUeFSD74j3/wrsDuP0uTBLdNCjaD1uOorTNcs84HP1468aoUYVNMKt28FuuwEzcS0yltvBcJNt+x6o3QwsnLM9l+AFZqhvIpAMFKygBNW+KQiBXiUovVW0ICzhSu22COvCK7ohXFtvfhjkEMWeWSSSzb5ZJRTVnllllt2+WWYY5Z5ZpprriggADs=" alt="" />

声明数组变量

  数组的定义由两部分组成,数组的类型和数组的名称即变量的名称,数组的类型写法为 type[], type是数组中包含的元素的类型,方括号表明这个变量类型为数组,数组的大小不是类型的一部分,因此方括号内为空。例如:

byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;

方括号也可加在数组名称的后面 如

byte anArrayOfBytes[] ;

不过不提倡此形式。

数组的创建,初始化和取值

数组的创建可以使用new关键词:

// 创建一个整数数组
anArray = new int[10];

数组的初始化:

//方法1 使用索引号对数组中的元素逐个赋值
anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // and so forth //方法2比较简单,这里声明,赋值一步是完成的,数组的大小根据花挂号内的元素个数决定
//注意以下初始化方式声明,初始化必须在一个语句内
int[] anArray = {
100, 200, 300,
400, 500, 600,
700, 800, 900, 1000
};
//或者
int[] anArray = new int[]{
100, 200, 300,
400, 500, 600,
700, 800, 900, 1000
};

注意,第二种方法只能用于声明语句中,如果使用,数组长度由括号内元素个数决定。

数组取用可以使用数组的索引号,使用实例:

System.out.println("元素 1 索引号为 0: " + anArray[0]);

多维数组

Java中也可以定义元素为数组的数组,即多维数组,其定义和使用见如下代码:

class MultiDimArrayDemo {
public static void main(String[] args) {
//此处定义和赋值必须一步完成
String[][] names = {
{"Mr. ", "Mrs. ", "Ms. "},
{"Smith", "Jones"}
};
// 输出为 Mr. Smith
System.out.println(names[0][0] + names[1][0]);
// 输出为 Ms. Jones
System.out.println(names[0][2] + names[1][1]);
}
}

一维的数组和多维数组都可以用 arrayName.length来获得数组的大小,即元素的个数,如

//输出数组变量anArray的大小

System.out.println(anArray.length);

数组的复制

  System类中提供了一个arraycopy的方法,可以实现将一个数组复制到另一个数组,用法为:

  public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) 

  其中src为复制的源数组,dest位目的数组,srcPos为源数组的起始位置,length为需要复制的数量。如:

class ArrayCopyDemo {
public static void main(String[] args) {
char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
'i', 'n', 'a', 't', 'e', 'd' };
char[] copyTo = new char[7];
System.arraycopy(copyFrom, 2, copyTo, 0, 7); //输出为caffein
System.out.println(new String(copyTo));
}
}

如果创建一个简单的数组而且仅使用一次,可使用如下快捷方法如:

//随机创建数组并输出其中一个元素 结果为 cdd
System.out.println(new String[]{"abc","cdd"}[1]);

此外,java的java.util.Arrays提供了比较多的对数组进行操作的方法,详见其API文档。