class HalfDollars {
public static void main(String[] arguments) {
int[] denver = { 15000006, 18810000, 20752110 };
int[] philadelphia = new int[denver.length];
int[] total = new int[denver.length];
int average; philadelphia[0] = 15020000;
philadelphia[1] = 18708000;
philadelphia[2] = 21348000; total[0] = denver[0] + philadelphia[0];
total[1] = denver[1] + philadelphia[1];
total[2] = denver[2] + philadelphia[2];
average = (total[0] + total[1] + total[2]) / 3; System.out.println("1993 production: " + total[0]);
System.out.println("1994 production: " + total[1]);
System.out.println("1995 production: " + total[2]);
System.out.println("Average production: "+ average);
}
}
int[] philadelphia = new int[denver.length];//是什么意思啊? 是不是NEW 了一个新的对象 denver.length 这个是代表这里所有的元素吗? 我可以这样理解吗?{ 15000006, 18810000, 20752110 }; 我觉得这个没必要写出来 因为下面已经赋值给出了
philadelphia[0] = 15020000;
philadelphia[1] = 18708000;
philadelphia[2] = 21348000;
请帮我解释一下 谢谢!!!!
public static void main(String[] arguments) {
int[] denver = { 15000006, 18810000, 20752110 };
int[] philadelphia = new int[denver.length];
int[] total = new int[denver.length];
int average; philadelphia[0] = 15020000;
philadelphia[1] = 18708000;
philadelphia[2] = 21348000; total[0] = denver[0] + philadelphia[0];
total[1] = denver[1] + philadelphia[1];
total[2] = denver[2] + philadelphia[2];
average = (total[0] + total[1] + total[2]) / 3; System.out.println("1993 production: " + total[0]);
System.out.println("1994 production: " + total[1]);
System.out.println("1995 production: " + total[2]);
System.out.println("Average production: "+ average);
}
}
int[] philadelphia = new int[denver.length];//是什么意思啊? 是不是NEW 了一个新的对象 denver.length 这个是代表这里所有的元素吗? 我可以这样理解吗?{ 15000006, 18810000, 20752110 }; 我觉得这个没必要写出来 因为下面已经赋值给出了
philadelphia[0] = 15020000;
philadelphia[1] = 18708000;
philadelphia[2] = 21348000;
请帮我解释一下 谢谢!!!!
呵呵。够明白吧,别忘加分啦,嘻嘻
其实就是新定义一个整形数组,名字叫philadelphia,它的大小是denver.length。也就说,定义一个跟denver一样大的数组。
至于里面的内容,跟denver没有直接联系。
与denver数组一样大。
public static void main(String[] arguments) {
int[] denver = { 15000006, 18810000, 20752110 };
int[] philadelphia = new int[denver.length];
int[] total = new int[denver.length];
int average; philadelphia[0] = 15020000;
philadelphia[1] = 18708000;
philadelphia[2] = 21348000; total[0] = denver[0] + philadelphia[0];
total[1] = denver[1] + philadelphia[1];
total[2] = denver[2] + philadelphia[2];
average = (total[0] + total[1] + total[2]) / 3; System.out.println("1993 production: " + total[0]);
System.out.println("1994 production: " + total[1]);
System.out.println("1995 production: " + total[2]);
System.out.println("Average production: "+ average);
}
}
int[] philadelphia = new int[denver.length];//是什么意思啊? 是不是NEW 了一个新的对象
//声明一个int数组,容量为denver.length,就是denver数组的容量,但跟denver没关系,相互独立
denver.length 这个是代表这里所有的元素吗? 我可以这样理解吗?{ 15000006, 18810000, 20752110 }; 我觉得这个没必要写出来 因为下面已经赋值给出了
//length是数组的属性,String也有这个属性,表示数组的长度,及数组的元素个数。 philadelphia[0] = 15020000;
philadelphia[1] = 18708000;
philadelphia[2] = 21348000;
答:定义一个长度为denver.length的int数组。名字为philadelphia
只做了数组声明,没有具体赋值,JVM赋了初值 int型是0
philadelphia[0] = 15020000;
philadelphia[1] = 18708000;
philadelphia[2] = 21348000;
是对数组元素赋具体的值。
是不是NEW 了一个新的对象 denver.length 这个是代表这里所有的元素吗? 我可以这样理解吗?
denver.length 是代表你生成数据的长度{ 15000006, 18810000, 20752110 }; 我觉得这个没必要写出来 因为下面已经赋值给出了
color=#800000]这是数组初始化[/color]
Java code
philadelphia[0] = 15020000;
philadelphia[1] = 18708000;
philadelphia[2] = 21348000;
这个叫数组的动态初始化int[] philadelphia={18023829,234234,2342342};
这个叫数组的静态初始化