数组

一维数组


一、创建

声明变量并分配内存
int []arr = new int[5];
或者:
int []arr;
arr = new int[5];
long[] old1={(long)40,(long)42};
long[] old2={40L,45L};

说明:

  1. 使用new关键字为数组分配内存时,整型数组中的各个元素的初始化值都为0 or null。

二、初始化

int arr1[] = new int[]{1,2,3,4}; 
或者:
int arr2[] = {1,2,3,4};     
或者:
a[0] = 1;

二维数组


一、创建

int [][]myarr = new int[2][4]; 
或者:
int [][]a = new int[2][];  
a[0] = new int[2];  
a[1] = new int[3]; 
int myarr[][] = {{12,0},{45,10}}; 
或者:
int myarr[][] = new int[][]{{12,0},{45,10}}; 
或者:
myarr[1][1] = 20;  

foreach


array是三维数组,它里面的元素都是二维数组,所以第一个foreach的变量是int[][];
而二维数组中的元素又是一维数组,所以第二个foreach的变量是int[]。
public class Test
{
	@SuppressWarnings("unused")
    public static void main(String[] args)
    {
		 int array[][][] = new int[][][]{      
		    { { 1, 2, 3 }, { 4, 5, 6 } },  
		    { { 7, 8, 9 }, { 10, 11, 12 } },  
		    { { 13, 14, 15 }, { 16, 17, 18 } }  
		};  
		 for (int[][] is : array) {        
			 for (int[] is2 : is) {  
				 for (int i : is2) {  
					 System.out.print(i + "\t");  
				 }  
				 System.out.println();     
			 }  
		 }	
    }
}

注意:int[] 不能强转为Object[],其余类似原因
 

几个类型转换举例


例1:

数组可以赋值给一个Object Reference。数组还可以赋值给一个Object[] Reference。

import java.util.Arrays;
public class COM { 
	public static void main(String[] args) throws Exception {	
        String[] s1 = {"h","i"};
        Object[] o1 = s1;
        String[] s2 = (String[])o1;
        Object o2 = s1;
        System.out.println(s1);
        System.out.println(o1);
        System.out.println(s1);
        System.out.println(o2);
        System.out.println("--------------");
        System.out.println(Arrays.toString(s1));
        System.out.println(Arrays.toString(o1));
        System.out.println(Arrays.toString(s2));
        //System.out.println(Arrays.toString(o2));ERROR
	}
}
Output:
[Ljava.lang.String;@7b1d7fff
[Ljava.lang.String;@7b1d7fff
[Ljava.lang.String;@7b1d7fff
[Ljava.lang.String;@7b1d7fff
--------------
[h, i]
[h, i]
[h, i]
例2:
编译正常,但不能运行

public class LK {
	public static void main(String[] args) {
		Fruit[] fruit = new  Apple[5];
		fruit[0] = new Apple();
		fruit[1] = new Orange();  //异常,抛出
		fruit[2] = new Banana();
	}
}
class Fruit{ }
class Apple extends Fruit{ }
class Orange extends Fruit{ }
class Banana extends Fruit{ }

Output:
java.lang.ArrayStoreException: test.Orange

 

数组对象的方法或属性


一、length属性:数组第一维的容量

public class COM {
	public static void main(String[] args) {
		int[][][] a1 = new int[3][4][5];
		 System.out.println(a1.length);   //数组第1维的容量:3
		 System.out.println(a1[0].length);   //数组第2维的容量:4
		 System.out.println(a1[2].length);   //数组第2维的容量:4
		 System.out.println(a1[0][0].length);   //数组第3维的容量:5
		 System.out.println(a1[2][3].length);   //数组第3维的容量:5
 	}
}

 

java.util.Arrays类:类的方法都是static的,方法的参数若为数组类型则都是一维的


一、填充:fill(),及几个toString方法的比较

public static void fill(int[] a, int val){}
public static void fill(int[] a, int fromIndex, int toIndex, int val) {}

array:要进行填充的数组(一维)。
fromIndex:指定填充的第一个元素的索引(下标)。(包括)
toIndex:指定填充至最后一个元素的索引(下标)。(不包括)
value:要填充到数组中的值。
ArrayIndexOutOfBoundsException:fromIndex < 0 or toIndex > a.length

import java.util.Arrays;
public class COM {
	public static void main(String[] args) {
		int[][][] a1 = new int[][][]{      
		    { { 1, 2, 3 }, { 4, 5, 6 } },  
		};  
		System.out.println("引用变量a1的值:"+a1.toString());
		Arrays.fill(a1[0][1], 0);
		System.out.println("a1值:"+a1.toString());
		/* Arrays.toString(int[] a)
		 * 输出数组a的所有第一维元素a1[0]~a1[a1.length-1]的值
		 */
		//此时a1的一维元素a1[0]~a1[a1.length-1]的值实质是地址,指向a1[0][0]~a1[a1.length-1][0]
		System.out.println("a1[0]~a1[a1.length-1]的值:"+Arrays.toString(a1));
		System.out.println("一维数组a1[0][0]所含元素:"+Arrays.toString(a1[0][0]));
		System.out.println("a1[0]~a1[a1.length-1]的值:"+Arrays.asList(a1).toString());
		System.out.println("多维数组a1各维度所含元素:"+Arrays.deepToString(a1));
		System.out.println("------------------------");
		int[] a2 = {1,2,3,4,5,6};	
			System.out.println("a2值:"+a2.toString());
			Arrays.fill(a2, 100);
			Arrays.fill(a2, 1,3, 0);
			System.out.println("a2值:"+a2.toString());
			System.out.println("一维数组a2所含元素:"+Arrays.toString(a2));
			System.out.println("a2值:"+Arrays.asList(a2).toString());
			/*The method deepToString(Object[]) 
			 * in the type Arrays is not applicable for the arguments (int[])
			 * 必须保证参数是Object[]数组类型,a1可以作参数,是因为
			 * a1的一维元素a1[0]~a1[a1.length-1]同样是引用变量,Object类型的。
			 * 而a2的一维元素却是 int 类型的。
			 */
			//System.out.println(Arrays.deepToString(a2));
 	}
}
Output:
引用变量a1的值:[[[I@7b1d7fff
a1值:[[[I@7b1d7fff
a1[0]~a1[a1.length-1]的值:[[[I@299a06ac]
一维数组a1[0][0]所含元素:[1, 2, 3]
a1[0]~a1[a1.length-1]的值:[[[I@299a06ac]
多维数组a1各维度所含元素:[[[1, 2, 3], [0, 0, 0]]]
------------------------
a2值:[I@383534aa
a2值:[I@383534aa
一维数组a2所含元素:[100, 0, 0, 100, 100, 100]
a2值:[[I@383534aa]
二、排序

Arrays.sort()

Java中的String类型数组的排序算法是根据字典编排顺序排序的,
因此数字排在字母前面,大写字母排在小写字母前面。

import java.util.Arrays;
public class Test {
	public static void main(String[] args) {
		char[] a2 = {'x','a','B','v','9'}; 
		Arrays.sort(a2);
		System.out.println(Arrays.toString(a2));
	}
}
Output:
[9, B, a, v, x]
三、复制数组

Arrarys.copyOf()
Arrarys.copyOfRange()

如:public static int[] copyOf(int[] original, int newLength){}
返回类型:int[]
original:要进行复制的数组。
newLength:int型常量。代表复制后的新数组的长度。
如果新数组的长度大于数组arr的长度,则用0填充,
(根据复制数组的类型来决定填充的值,整型数组则用0填充。char型数组则会使用’\u0000‘来填充)。
如果复制后的数组长度小于数组arr的长度,会从数组arr的第一个元素开始截取直到满足新数组长度为止。
int newarr[] = Arrays.copyOf(arr, 5);  

 public static int[] copyOfRange(int[] original, int from, int to){}
 from:复制的起点(下标)。(包括)  
 to:复制的终点(下标)。(不包括)  
 int newarr[] = Arrays.copyOfRange(arr, 0, 3);     
四、比较是否相等(Object.equals)

Arrays.equals()
Arrays.deepEquals()

import java.util.Arrays;
public class Test {
	public static void main(String[] args) {
		int[][] a1 = {{3},{4}};
		int[][] a2 = {{3},{4}}; 
		System.out.println(Arrays.equals(a1, a2));  //一维数组比较(只比较一维的值)
		System.out.println(Arrays.deepEquals(a1, a2)) ;   //多维数组比较(比较最内层的值)
	}
}
Output:
false
true

System.arraycopy()复制数组—浅复制


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

src : the source array.
srcPos : starting position in the source array.
dest : the destination array.
destPos : starting position in the destination data.
length : the number of array elements to be copied.

import java.util.Arrays;
public class Test {
	public static void main(String[] args) {
		int[][] a1= {{1,2},{4,5}};
		int[][] a2= new int[5][5];
		System.arraycopy(a1, 0, a2, 1, 2);
		System.out.println(Arrays.deepToString(a1));
		System.out.println(Arrays.deepToString(a2));
	}
}
Output: //a2[1],a2[2]只是复制,而没有填充剩下的,导致a2的a2[1],a2[2]的容量发生了变化。5变2。
[[1, 2], [4, 5]]
[[0, 0, 0, 0, 0], [1, 2], [4, 5], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值