本文共 3280 字,大约阅读时间需要 10 分钟。
有了额外的自动包装机制和泛型,在容器中持有基本类型就变得易如反掌了,而这也进一步促使你用容器来替换数组。因为泛型可以产生类型安全的容器,因此数组面对这一变化,已经变得毫无优势,泛型对数组是极大的威胁。
当你使用Java编程时,应该优先选择容器而不是数组,只有在证明性能成为问题(并且切换到数组对性能提高有所帮助)时,你才应该将程序重构为使用数组。 1、数组是一个有界的线性序列,大小被固定、随机访问速度非常快(超过集合);
2、数组可以存储基本类型,也可以存储引用类型;
3、数组如果没被初始化则为null,数组如果没被显式初始化,则会自动初始化。其中的值与数组元素类型的默认初始化值相同;
4、数组可以有多维的,但是,一维数组要比多维的快很多。在对效率要求很高的程序中,一般都不用多维数组,需要用的时候,也常常将多维数组转换为一维的存储方式;
5、数组的声明不能使用泛型,其实也没必要在数组上使用泛型;
比如int[] a = int[3]; 实际上与int[] a= {0,0,0}相同。
int[][] a1 = {
{1,2,3},{4,5,6}};
Arrays类是一个非常有用数组工具类,里面有很多工具方法,检索、填充、排序、比较、toString()等。
import java.util.Arrays;
/**
* 数组综合测试
*
* @author leizhimin 2009-7-28 12:35:41
*/ public class TestArrays {
public static void main(String[] args) {
int[] i =
new int[10];
//填充数组 Arrays.fill(i, 2);
//遍历数组 for (
int x : i) {
System.out.print(x +
" ");
}
//toString()数组 System.out.println(
"\n" + Arrays.toString(i));
//复制数组 int[] b =
new int[12];
System.arraycopy(i, 0, b, 2, 5);
System.out.println(Arrays.toString(b));
//一维数组的比较 int[] c =
new int[3];
int[] d =
new int[3];
Arrays.fill(c, 3);
Arrays.fill(d, 3);
System.out.println(c.equals(d));
System.out.println(Arrays.equals(c, d));
System.out.println(
"-------------");
int[][] a1 = {
{1, 2, 3}, {4, 5, 6}};
int[][] a2 = {
{1, 2, 3}, {4, 5, 6}};
System.out.println(a1.equals(a2));
System.out.println(Arrays.equals(a1, a2));
System.out.println(Arrays.deepEquals(a1, a2));
//深度toString() System.out.println(Arrays.toString(a1));
System.out.println(Arrays.deepToString(a1));
//数组的排序 int[] a3 = {3, 2, 5, 4, 1};
System.out.println(Arrays.toString(a3));
Arrays.sort(a3);
System.out.println(Arrays.toString(a3));
//一维数组数值检索 int index1 = Arrays.binarySearch(a3, 4);
int index2 = Arrays.binarySearch(a3, -12);
int index3 = Arrays.binarySearch(a3, 8);
System.out.println(index1 +
" " + index2 +
" " + index3);
}
}
2 2 2 2 2 2 2 2 2 2
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
[0, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0]
false true -------------
false false true [[I@3e25a5, [I@19821f]
[[1, 2, 3], [4, 5, 6]]
[3, 2, 5, 4, 1]
[1, 2, 3, 4, 5]
3 -1 -6
Process finished with exit code 0
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
参数: src
- 源数组。 srcPos
- 源数组中的起始位置。 dest
- 目标数组。 destPos
- 目标数据中的起始位置。 length
- 要复制的数组元素的数量。 2、数组元素的访问:数组名[index],或数组名[index1][index2];
提供了动态创建和访问 Java 数组的方法。 SQL 类型 ARRAY
在 Java 编程语言中的映射关系。 1、填充数组,Arrays.fill()方法只能填充一维基本类型数组。
2、数组复制,不但适合一维数组,也适合多维数组,只是多维数组赋值时候要特别小心,有时候会出一些莫名其妙的问题
int[][] a1 = {
{1, 2, 3}, {4, 5, 6}};
int[][] ax =
new int[3][3];
System.arraycopy(a1,1,ax,1,1);
System.out.println(Arrays.deepToString(ax));
[[0, 0, 0], [4, 5, 6], [0, 0, 0]]
int[][] a1 = {
{1, 2, 3}, {4, 5, 6}};
int[][] ax =
new int[2][4];
System.arraycopy(a1,0,ax,0,1);
int[] x1 = ax[0];
System.out.println(
"二维数组第一个元素的长度:"+x1.length);
System.out.println(
"-----");
for (
int[] t : ax) {
for (
int _i : t) {
System.out.println(_i);
}
System.out.println(
"-----");
}
二维数组第一个元素的长度:3
-----
1
2
3
-----
0
0
0
0
-----
3、数组的排序,只能排序基本类型数组,如果是其他类型,可以考虑使用集合工具Collections来做。
4、尽可能不要使用多维数组,多维数组效率会有很大的损失。
转载地址:http://yjbkb.baihongyu.com/