Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Java基础梳理:数组

创建数组

下面这几种方式都可以创建一个数组1 int[] a;2 int[] b = new int[5];3 String c[] = new String[] { "Hello", "World" };4 double [] d = new double[6]; 我们比较习惯于第二种方式,第一种只是声明了数组并未初始化,使用的时候往往会因为忘记将变量初始化而报错。而第三种、第四种只是写法习惯上的问题。 对于数组初始化,数字类型的值默认为0,字符串类型默认为null,布尔类型默认为false。java还提供了一种简单的方式,对于这种创建方式,数组的大小就是初始化值的个数。所以使用的时候要注意,因为数组一旦创建就无法改变大小,但是数组元素的值是可以改变的。int[] numbers={2005,1007,1994,2015}; 当用做返回值时,我们还可以通过不创建变量的情况下直接创建一个匿名数组1 public String[] getSomethings(String input) {2 //TODO: do something ...3 return new String[]{input};4 }

数组元素的访问

数组的中的元素,我们是通过元素的索引去访问的int[] numbers = { 2005, 1007, 1994, 2015 };System.out.println(numbers[1]); 一旦访问的下标超过数组的索引,就会报出数组索引超过边界的异常,所以在使用数组的时候这个也是要注意的一点。 通常我们用for去便利一个数组中的元素,这种方式便于控制数组中的每个元素int[] numbers = { 2005, 1007, 1994, 2015 };for (int i = 0; i < numbers.length; i++) {System.out.println(numbers[i]);} 也可以通过foreach的方式

数组常用的一些基本函数

对于数组的操作,一般都在java.util.Arrays类中 1.将数组作为字符串输出1 int[] numbers = { 2005, 1007, 1994, 2015 };2 String input = Arrays.toString(numbers);3 System.out.println(input); 2.数组的拷贝int[] numbers = { 2005, 1007, 1994, 2015 };int[] tempNumbers=numbers;//拷贝的是数组的引用System.out.println(Arrays.toString(tempNumbers));numbers[0]=1999;System.out.println(tempNumbers[0]); 如果是想直接拷贝数组中的所有元素的值,可以通过Arrays.copyOf方法int[] numbers = { 2005, 1007, 1994, 2015 };int[] tempNumbers=Arrays.copyOf(numbers, 6);//对于多余的元素将被赋予0值System.out.println(Arrays.toString(tempNumbers)); 还可以通过System.arraycopy方法int[] numbers = { 2005, 1007, 1994, 2015 };int[] tempNumbers=new int[5];System.arraycopy(numbers, 0, tempNumbers, 1, numbers.length);System.out.println(Arrays.toString(tempNumbers));numbers[0]=2000;System.out.println(tempNumbers[0]); 3.数组排序int[] numbers = { 2005, 1007, 1994, 2015 };Arrays.sort(numbers);System.out.println(Arrays.toString(numbers));

多维数组

对于二维数组和不规则数组,其实就是把数组的元素再看成一个数组来处理就可以。String input="";int[][] table={{11,15,9},{16,7,10},{15,12,13}};for (int i = 0; i < table.length; i++) {int[] temp=table[i];for (int j = 0; j < temp.length; j++) {input+=String.valueOf(temp[j])+" ";}}System.out.println(input);

简单应用

假设一个ATM机器里有六个钱箱,每个钱箱存放不同的面值,那么我们该如何初始化这些钱箱呢?一般情况下我们会这么做 1 public class CashUnit { 2 private int value; 34 public int getValue() { 5 return value; 6 } 78 public void setValue(int value) { 9 this.value = value;10 }11 12 public int getCount() {13 return count;14 }15 16 public void setCount(int count) {17 this.count = count;18 }19 20 private int count;21 } View Code 1 CashUnit[] cashUnit=new CashUnit[6];2 cashUnit[0].setValue(100);3 cashUnit[1].setValue(50);4 cashUnit[2].setValue(20);5 cashUnit[3].setValue(10);6 cashUnit[4].setValue(5);7 cashUnit[5].setValue(1); 我么还可以通过索引对应值的方式去实现1 public static int[] cashValues = { 100, 50, 20, 10, 5, 1 };2 3 public static CashUnit[] createCashUnits() {4 CashUnit[] cashUnit = new CashUnit[6];5 for (int i = 0; i < cashUnit.length; i++) {6 cashUnit[i].setValue(cashValues[i]);7 }8 return cashUnit;9 }本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-01/127049.htm