Java数组 --- 数组的基本用法

    以二维数组为例,一位数组类似

/**
 * 二维数组的基本用法
 * @author Administrator
 *
 */
public class Test01 {
	public static void main(String[] args) {
		//声明与赋值
		int[][] a = {
				{1,2},
				{3,4,0,9},
				{5,6,7}
		
		};
		//只声明第一维长度 ---> 合法
		//只声明第二维长度 ---> 非法
		int[][] b = new int[3][];
		b[0] = new int[2];
		b[1] = new int[4]; 
		b[2] = new int[3];
		b[0][0]=1;
		b[0][1]=2;
		
		b[1][0]=3;
		b[1][1]=4;
		b[1][2]=0;
		b[1][3]=9;
		
		b[2][0]=5;
		b[2][1]=6;
		b[2][2]=7;
		// a[0] b[0]为对象,虽然内容相同但地址不同,返回flase;而具体到a[][]b[][]层次,是具体到某一个数值,相同返回true
		System.out.println(a[0][0]==b[0][1]);
		System.out.println(a[0]==b[0]);
	}
		
		

}

    一些其他的问题

/**
 * 枚举的用法
 * 枚举的常量列表可以由数字、字母、下划线及$组成,且不能用数字开头,与变量命名规则一致
 * 确定一个字符对应的Unicode编码位置
 * 二维数组的一些问题
 * @author Administrator
 *
 */
public class Test02 {
	public static void main(String[] args) { 
//		int x = 12L;
//		long y = 8.0;
//		float y = 6.89f;
		//枚举的用法
//		Weekend x = Weekend.s;
//		System.out.println(x);
//		char[] c = {'你','我','他'};
//		for (int i = 0; i < c.length; i++) {
		//确定一个字符在Unicode编码中的位置,进行强制类型转换
//			System.out.println(c[i]+"位置为"+(int)c[i]);
		
//		}
		double a[][] = {{1,2,3},{4,5,6},{7,8,9}};
		double b[][] = {{1.0,2.2,3.3,4.4},{5.5,6.6,7.7,8.8}};
		//对于一个二维数组,a[0]与b[0]仍是对象,里面存放着后续维度的数组,b00为false
		boolean b00 = (a[0]==b[0]);
		System.out.println(b00);
		//a[0][0],b[0][0]具体到一个数,b01为true
		boolean b01 = (a[0][0]==b[0][0]);
		System.out.println(b01);
		a[0]=b[0];
		a[1]=b[1];
		System.out.println(a==b);
		System.out.println(a.length);
		System.out.println(a[0][3]);
		System.out.println(a[1][3]);
		
	}
	
}
enum Weekend{
	s,s2,s3,s4,s5,s6,s7,int_long,$Boy26
	
}

二维数组的简单应用,矩阵加和并打印

/**
 * 计算矩阵的和并打印
 * @author Administrator
 *
 */
public class Matrix {

	public static void print(int[][] c) {
		for (int i = 0; i < c.length; i++) {
			for (int j = 0; j < c.length; j++) {
				System.out.print(c[i][j] + "\t");
			}
			System.out.println();

		}
	}

	public static int[][] add(int[][] a, int[][] b) {

		int[][] c = new int[a.length][a.length];
		for (int i = 0; i < c.length; i++) {
			for (int j = 0; j < c.length; j++) {
				c[i][j] = a[i][j] + b[i][j];

			}

		}
		return c;
	}

	public static void main(String[] args) {
		int[][] a = { { 1, 2, 4 }, { 4, 5, 7 }, { 7, 8, 1 } };
		int[][] b = { { 8, 7, 8 }, { 4, 1, 5 }, { 3, 7, 8 } };

		int[][] c = add(a, b);
		print(c);

	}

}

猜你喜欢

转载自blog.csdn.net/qq_30007589/article/details/80867020