直接上代码和截图
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]);
}
运行结果如下: