码迷,mamicode.com
首页 > 编程语言 > 详细

数组的初始化及输出

时间:2015-05-15 10:44:30      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:二维数组   string   初始化   输出   

二维数组可以不初始化列数(第二维)。

下面给出的例子是用两种不同的方式存储二维数组并输出:

1. 这是我们通俗易懂的二维数组存储方法:

String[][] data = new String[][] {
				{ "youth", "high", "no", "fair", "no" },
				{ "youth", "high", "no", "excellent", "no" },
				{ "middle_aged", "high", "no", "fair", "yes" }, };
		for (String[] s : data) {
			for (String str : s)
				System.out.print(str + "\t");
			System.out.println();
		}

输出结果:

youth	high	no	fair	no	
youth	high	no	excellent	no	
middle_aged	high	no	fair	yes	

2.这是把第二维定义为字符串数组的方法,输出时需要用到强制字符转换:

Object[] array = new Object[] {
				new String[] { "age", "income", "student", "credit_rating",
						"buys_computer" },
				new String[] { "youth", "high", "no", "fair", "no" },
				new String[] { "youth", "high", "no", "excellent", "no" },};
                     for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < ((String[]) array[0]).length; j++)
				System.out.print(<span style="color:#ff0000;">((String[]) array[i])[j]</span> + "\t");
			System.out.println();
		}

输出结果跟上面一样。

数组的初始化及输出

标签:二维数组   string   初始化   输出   

原文地址:http://blog.csdn.net/u013159040/article/details/45740987

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!