1.第一种,不知道具体的内容
String[] names = new String[34];
double[] scores = new double[34];
char[] sexs = new char[34];
int[] ages = new int[34];
一般配合for循环来给数组赋值。
2.第二种,知道具体的内容,直接赋值
String[] names1 = { "张三", "李四", "王五", "赵四" };
double[] scores1 = { 85, 98, 76, 90 };
char[] sexs1 = { '男', '男', '男', '女' };
int[] ages1 = { 23, 24, 25, 26 };
//数组
double[] scores = new double[10];
for(int i = 0;i<scores.length;i++){
scores[i] = Math.random();
}
String[] cities = {"武汉","上海"};
//输出变量
for(int i = 0;i<scores.length;i++){
System.out.println(scores[i]);
}
for(int i = 0;i<cities.length;i++){
System.out.println(cities[i]);
}
//输出foreach的方式
//打印分数
for(double score : scores){
System.out.println(score);
}
//打印城市
for(String city :cities){
System.out.println(city);
}
String[] names = new String[34];
double[] scores = new double[34];
char[] sexs = new char[34];
int[] ages = new int[34];
一般配合for循环来给数组赋值。
2.第二种,知道具体的内容,直接赋值
String[] names1 = { "张三", "李四", "王五", "赵四" };
double[] scores1 = { 85, 98, 76, 90 };
char[] sexs1 = { '男', '男', '男', '女' };
int[] ages1 = { 23, 24, 25, 26 };
//数组
double[] scores = new double[10];
for(int i = 0;i<scores.length;i++){
scores[i] = Math.random();
}
String[] cities = {"武汉","上海"};
//输出变量
for(int i = 0;i<scores.length;i++){
System.out.println(scores[i]);
}
for(int i = 0;i<cities.length;i++){
System.out.println(cities[i]);
}
//输出foreach的方式
//打印分数
for(double score : scores){
System.out.println(score);
}
//打印城市
for(String city :cities){
System.out.println(city);
}