首页 > 代码库 > java 二维数组,三维数组 ,糊糙数组,多维数组示例代码

java 二维数组,三维数组 ,糊糙数组,多维数组示例代码

java 二维数组,三维数组 ,糊糙数组,多维数组示例代码

package org.rui.array;
import java.util.Arrays;
/**
 * 三维数组示例
 * @author lenovo
 *
 */
public class ThreeDWithNew {

	public static void main(String[] args) {
		int[][][] a=new int[2][2][4];
		System.out.println(Arrays.deepToString(a));
	}
}
/**
 * output:
 * [[[0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0]]]
 * */
package org.rui.array;

import java.util.Arrays;
import java.util.Random;

/**
 * 粗糙数组
 * 数组中构成的矩阵的每个向量都可以具有任意的长度
 * @author lenovo
 *
 */
public class RaggedArray {
	public static void main(String[] args) {
		Random random=new Random(47);
		int[][][] a=new int[random.nextInt(7)][][];
		for(int i=0;i<a.length;i++)
		{
			a[i]=new int[random.nextInt(5)][];
			for(int j=0;j<a[i].length;j++)
			{
				a[i][j]=new int[random.nextInt(5)];
			}
		}
		System.out.println(Arrays.deepToString(a));
	}

}
/**
[[], [[0], [0], [0, 0, 0, 0]], [[], [0, 0], [0, 0]], [[0, 0, 0], [0], [0, 0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0], []], [[0], [], [0]]]
*/

package org.rui.array;

public class Bean {

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	

	
	
	
}



package org.rui.array;

import java.util.Arrays;
/**
 * 可以用类似的方式处理非基本类型的对象数组。
 * 下面,你可以看到如何 用花括号把多个new 表达式组织到一起:
 * @author lenovo
 *
 */
public class MultidimensionalObjectArrays {
	public static void main(String[] args) {
		Bean[][] b=
			{
				{new Bean(),new Bean(),},
				{new Bean(),new Bean(),
				 new Bean(),new Bean(),},
				{new Bean(),new Bean(),
				 new Bean(),new Bean(),
				 new Bean(),new Bean(),
				 new Bean(),new Bean(),},
		    };
		System.out.println(Arrays.deepToString(b));
	}
}
/*
[[org.rui.array.Bean@1afae45, org.rui.array.Bean@da4b71], [org.rui.array.Bean@d9660d, org.rui.array.Bean@bb0d0d, org.rui.array.Bean@55e55f, org.rui.array.Bean@145c859], [org.rui.array.Bean@2c1e6b, org.rui.array.Bean@811c88, org.rui.array.Bean@785d65, org.rui.array.Bean@3bc257, org.rui.array.Bean@153f67e, org.rui.array.Bean@15bdc50, org.rui.array.Bean@1dd3812, org.rui.array.Bean@8c436b]]
*/

package org.rui.array;

import java.util.Arrays;

/**
 * 自动包装机制对数组初始化也起作用
 * @author lenovo
 *
 */
public class AutoboxingArrays {
	public static void main(String[] args) {
		Integer[][]a={
				{1,2,3,4,5,6,7,8,9,10},
				{11,22,33,44,55,66,77,88,99,100},
				{21,22,23,24,25,26,27,28,29,210,666},
				{31,32,33,34,35,36,37,38,39,310,888},
		};
		System.out.println(Arrays.deepToString(a));
	}

}
/**output:
[
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 
 [11, 22, 33, 44, 55, 66, 77, 88, 99, 100], 
 [21, 22, 23, 24, 25, 26, 27, 28, 29, 210, 666],
 [31, 32, 33, 34, 35, 36, 37, 38, 39, 310, 888]
]
*/

package org.rui.array;

import java.util.Arrays;
/**
 * 下面这个展示了可以如何逐个地,部分地构建一个非基本类型的对象数组
 * 
 * @author lenovo
 *
 */
public class AssemblingMultidimensionalArrays {
	public static void main(String[] args) {
		Integer[][] a;
		a=new Integer[3][];
		for(int i=0;i<a.length;i++)
		{
			a[i]=new Integer[3];
			for(int j=0;j<a[i].length;j++)
				a[i][j]=i*j;//autoboxing
		}
		System.out.println(Arrays.deepToString(a));		
	}
}
/**
output:
[[0, 0, 0], [0, 1, 2], [0, 2, 4]]
*/

package org.rui.array;

import java.util.Arrays;
/**
 * 在Ingeger Double数组中,爪哇SE5的自动包装机制再次为我们创建了包装器对象
 * @author lenovo
 *
 */
public class MultiDimWrapperArry {
	public static void main(String[] args) {
		Integer[][] al=
			{
				{1,2,3},
				{4,5,6}
			};
		
		Double[][][] a2=
			{
				{{1.1,2.2},{3.3,5.0}},
				{{6.6,7.7},{8.8,9.9}},
				{{10.6,10.7},{1.8,1.9}}
			};
		
		String[][] a3=
			{
				{"january","february"},
				{"march","april","may"},
				{"june","july","august","september","october"},				
			};
		System.out.println("al:"+Arrays.deepToString(al));
		System.out.println("a2:"+Arrays.deepToString(a2));
		System.out.println("a3:"+Arrays.deepToString(a3));
	}
/**
al:[[1, 2, 3], [4, 5, 6]]
a2:[[[1.1, 2.2], [3.3, 5.0]], [[6.6, 7.7], [8.8, 9.9]], [[10.6, 10.7], [1.8, 1.9]]]
a3:[[january, february], [march, april, may], [june, july, august, september, october]]
 */
}