java中定义数组的3种方式

直接上代码和截图 

public static void main(String[] args) {
		// 定义数组的3种方式
		// 第一种
		String[] role = new String[3];
		role[0] = "建宁公主";
		role[1] = "双儿";
		role[2] = "阿珂";
		System.out.println(role[0] + "," + role[1] + "," + role[2]);

		// 也可以把[]放在变量名后面,如下:
		String role2[] = new String[4];
		role2[0] = "苏荃";
		role2[1] = "方怡";
		role2[2] = "曾柔";
		role2[3] = "沐剑屏";
		System.out.println(role2[0] + "," + role2[1] + "," + role2[2] + "," + role2[3]);
		
		// 第二种
		int[] score = {88, 66, 89, 73};
		System.out.println(score[0] + "," + score[1] + "," + score[2] + "," + score[3]);
		
		int score2[] = {25, 39, 17, 92};
		System.out.println(score2[0] + "," + score2[1] + "," + score2[2] + "," + score2[3]);

		// 第三种
		double[] ratings = new double[]{0.39, 0.76};
		System.out.println(ratings[0] + "," + ratings[1]);
		
		double ratings2[] = new double[]{8.76, 12.93};
		System.out.println(ratings2[0] + "," + ratings2[1]);
	}

运行结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值