|
Array using
package arrays;
import java.util.*;
/**
* <p>Title:Multidimensional Arrays </p>
* <p>Description: some examples</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company mencathay </p>
* @author not attributable
* @version 1.0
*/
public class Arrays {
static Random rand=new Random();
static int pRand(int mod){
return Math.abs(rand.nextInt())%mod+1;
}
static void prt(String s){
System.out.println(s);
}
public static void main(String[] args) {
int[][] a1={
{1,2,3},{4,5,6}
};
for(int i=0;i<a1.length;i++)
for(int j=0;j<a1.length;j++)
prt("a1["+i+"]["+j+"]="+a1[j]);
//3-Darray with fixed length;
int[][][] a2=new int[2][2][4];
for(int i=0;i<a2.length;i++)
for(int j=0;j<a2.length;j++)
for(int k=0;k<a2[j].length;k++)
prt("a2["+i+"]["+j+"]["+k+"]="+a2[j][k]);
//3-D array with varied-length vectors
int[][][] a3=new int[pRand(7)][][];
for(int i=0;i<a3.length;i++){
a3 = new int[pRand(5)][];
for (int j = 0; j < a3.length; j++)
a3[j] = new int[pRand(5)];
}
for(int i=0;i<a2.length;i++)
for(int j=0;j<a2.length;j++)
for(int k=0;k<a2[j].length;k++)
//prt("a3["+i+"]["+j+"]["+k+"]="+a3[j][k]);
prt("a3.length="+a3.length);
//Array of nonprimitive objects
Integer[][] a4={
{new Integer(1),new Integer(2)},
{new Integer(3),new Integer(4)},
{new Integer(5),new Integer(6)},
};
for(int i=0;i<a4.length;i++)
for(int j=0;j<a4.length;j++)
prt("a4["+i+"]["+j+"]="+a4[j]);
}
}
//prt("a3["+i+"]["+j+"]["+k+"]="+a3[j][k]);
在a4的array中,有时候会出现异常,不知道是什么原因,提示错误:
java.lang.ArrayIndexOutOfBoundsException: 3
at arrays.Arrays.main(Arrays.java:44)
[/COLOR] |
|