数组的特殊之处
- Java编程思想上有:数组与其他种类的容器之间的区别有三方面 —— 效率、类型和保存基本类型的能力。
- 数组是一种效率最高的存储和随机访问对象引用序列 的方式 —— 与之相应的代价:数组对象的大小被固定,并且其声明周期不可改变 。
- 数组可以持有基本数据类型,而泛型之前的容器则不能。 —— 但有了泛型,容器就可以指定并检查它们所持有对象的类型。
数组与泛型容器的比较(见下面代码):
package com.chenny.Entity;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class BerylliumSphere {
private static long counter;
private final long id = counter++;
public String toString(){
return "Sphere" + id;
}
}
public class ContainerComparison {
public static void main(String[] args) {
BerylliumSphere[] spheres = new BerylliumSphere[10];
for (int i = 0;i < 5; i++){
spheres[i] = new BerylliumSphere();
}
System.out.println(Arrays.toString(spheres));
System.out.println(spheres[4]);
System.out.println("======================================================================");
List<BerylliumSphere> sphereList = new ArrayList<BerylliumSphere>();
for(int i = 0; i < 5; i++){
sphereList.add(new BerylliumSphere());
}
System.out.println(sphereList);
System.out.println(sphereList.get(4));
System.out.println("======================================================================");
int[] integers = {0, 1, 2, 3, 4, 5};
System.out.println(Arrays.toString(integers));
System.out.println(integers[4]);
List<Integer> intList = new ArrayList<Integer>(Arrays.asList(0,1,2,3,4,5));
intList.add(97);
System.out.println(intList);
System.out.println(intList.get(4));
}
}
一维数组
package com.chenny.Entity;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
class BerylliumSphere {
private static long counter;
private final long id = counter++;
public String toString(){
return "Sphere" + id;
}
}
public class ArrayOptions {
public static void main(String[] args) {
BerylliumSphere[] a;
BerylliumSphere[] b = new BerylliumSphere[5];
print("b: " + Arrays.toString(b));
BerylliumSphere[] c = new BerylliumSphere[4];
for(int i = 0; i < c.length; i++)
if(c[i] == null)
c[i] = new BerylliumSphere();
BerylliumSphere[] d = { new BerylliumSphere(), new BerylliumSphere(), new BerylliumSphere()};
System.out.println(Arrays.toString(d));
a = new BerylliumSphere[]{new BerylliumSphere(), new BerylliumSphere(),};
System.out.println(Arrays.toString(a));
print("a.length = " + a.length);
print("b.length = " + b.length);
print("c.length = " + c.length);
print("d.length = " + d.length);
a = d;
print("a.length = " + a.length);
int[] e;
int[] f = new int[5];
print("f: " + Arrays.toString(f));
int[] g = new int[4];
for(int i = 0; i < g.length; i++)
g[i] = i*i;
int[] h = { 11, 47, 93 };
print("f.length = " + f.length);
print("g.length = " + g.length);
print("h.length = " + h.length);
e = h;
print("e.length = " + e.length);
e = new int[]{ 1, 2 };
print("e.length = " + e.length);
}
private static void print(String s) {
System.out.println(s);
}
}
二维数组
package com.chenny.Entity;
import java.util.Locale;
public class TwoDoubleArray {
private static void print(String s) {
System.out.println(s);
}
private static void print() {
System.out.println();
}
public static double[][] twoDDoubleArray(int xLen, int yLen, double valStart, double valEnd) {
double[][] array = new double[xLen][yLen];
double increment = (valEnd - valStart)/(xLen * yLen);
double val = valStart;
for(int i = 0; i < array.length; i++)
for(int j = 0; j < array[i].length; j++) {
array[i][j] = val;
val += increment;
}
return array;
}
public static void printArray(double[][] array) {
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++)
System.out.printf(Locale.US, "%.2f ", array[i][j]);
print();
}
}
public static void main(String args[]) {
double[][] twoD = twoDDoubleArray(4, 6, 47.0, 99.0);
printArray(twoD);
print("**********************");
double[][] twoD2 = twoDDoubleArray(2, 2, 47.0, 99.0);
printArray(twoD2);
print("**********************");
double[][] twoD3 = twoDDoubleArray(9, 5, 47.0, 99.0);
printArray(twoD3);
}
}
Arrays数组的实用功能