日期:2014-05-20  浏览次数:20732 次

请大虾帮帮我 数组问题!!!!
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;
  请帮我解释一下 谢谢!!!!

------解决方案--------------------
是NEW一个空间,但是denver.length可不是代表所有的元素,是代表空间的大小,“.length”是指长度,NEW一个denver的长度的空间。denver里有{15000006, 18810000, 20752110 }; 共三组数,所以denver.length就代表的是3。其实就是NEW出来的空间大小为3。
呵呵。够明白吧,别忘加分啦,嘻嘻
------解决方案--------------------
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 了一个新的对象
//声明一个int数组,容量为denver.length,就是denver数组的容量,但跟denver没关系,相互独立
denver.length 这个是代表这里所有的元素吗? 我可以这样理解吗?{ 15000006, 18810000, 20752110 }; 我觉得这个没必要写出来 因为下面已经赋值给出了 
//length是数组的属性,String也有这个属性,表示数组的长度,及数组的元素个数。 philadelphia[0] = 15020000; 
philadelphia[1] = 18708000; 
philadelphia[2] = 21348000;