在我的for循环中获取ArrayOutofBoundsException

时间:2014-11-16 18:20:18

标签: java arrays loops for-loop

public class WeightOnPlanetsV1
{
  public static double[] calcWeight(double[] gravity, double[]mass)
  {
    double[] weight = new double[gravity.length];

    for (int i = 0; i < gravity.length; i++) {
        weight[i] = (mass[i] * 1000) / gravity[i];
        weight[i] = weight[i] / 433.59237;
    }

    return weight;
  }

  public static double[] takeFromFile(double[] gravity)throws IOException
  {
    File fileName = new File("GravityResults.txt");
    Scanner inFile = new Scanner(fileName);

    for (int i = 0; i < gravity.length; i++) {
        gravity[i] = inFile.nextDouble();
        gravity[i] = gravity[i] / 10;
    }
    inFile.close();

    return gravity;
  }

  public static void printResults(String[] names, double[] gravity, double weight[])
  {
    System.out.printf("%37s \n","My Weight on the Planets");
    System.out.printf("%5s %20s %15s \n","Planet","Gravity","Weight(lbs)");
    System.out.println("---------------------------------------");
    for (int i = 0; i < names.length; i++) {
        System.out.printf("%5s %10f %15f \n",names[i], gravity[i], weight[i]);
    }
  }

  public static void main(String[] args)throws IOException
  {        
    String[] names = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
    double[] weightOnPlanets = {100, 100, 100, 100, 100, 100, 100, 100, 100};
    double[] gravity = {};
    double[] masses = {3.3022 * Math.pow(10,23), 4.8685 * Math.pow(10,24), 5.9736 * Math.pow(10,24), 6.4185 * Math.pow(10,23), 1.8986 * Math.pow(10,27), 5.6846 * Math.pow(10,26), 8.6810 * Math.pow(10,25), 1.0243 * Math.pow(10,26), 1.312 * Math.pow(10,22)};

    double[] gravities = takeFromFile(gravity);
    double[] finalWeights = calcWeight(gravities,masses);

    printResults(names, gravities, finalWeights);
  }  
}

我的错误来自

for (int i = 0; i < names.length; i++) {
    System.out.printf("%5s %10f %15f \n",names[i], gravity[i], weight[i]);
}

当我尝试打印结果时。它给了我

java.lang.ArrayIndexOutOfBoundsException: 1
    at WeightOnPlanetsV1.printResults(WeightOnPlanetsV1.java:45)
    at WeightOnPlanetsV1.main(WeightOnPlanetsV1.java:63)

作为错误

5 个答案:

答案 0 :(得分:3)

您检查names数组的范围,并访问gravityweight,假设它们的长度与names相同或更长

答案 1 :(得分:1)

您的问题从takeFromFile开始,它接收一个空数组(double[] gravity = {};)并返回一个空数组。

稍后,遍历gravity(或gravities)数组的所有循环都不执行任何操作,而循环从0迭代到names.length - 1(在printResults中)导致ArrayIndexOutOfBoundsException,因为它假定namesgravity数组具有相同的长度。

答案 2 :(得分:1)

基本上,你初始化: 主要功能double[] gravity = {};

然后你打电话给:double[] gravities = takeFromFile(gravity);因为 gravity 没有元素,并且因为你的 takeFromFile(double [])函数你有这个:

 for (int i = 0; i < gravity.length; i++) {
    gravity[i] = inFile.nextDouble();
    gravity[i] = gravity[i] / 10;
}

基本上读取文件 gravity.lenght 次(这是一个非常糟糕的想法,顺便说一下。如果文件的数量少于重力[] )

无论如何,正如有人先前所说,这一切都会导致重力和重力都变空, finalWeights [] 也是如此,因为你使用以下方法初始化它:

double[] finalWeights = calcWeight(gravities,masses);

因为 gravities 有0个元素......好吧,那就发生了。 现在, names [] 有9个元素,大于0,这就是为什么在:

System.out.printf("%5s %10f %15f \n",names[i],gravity[i], weight[i]);

你得到一个ArrayOutOfBounds异常(因为你没有检查 gravity [] weight [] 大小,结果都是0)。这意味着您需要确保使用比名称[]

更大的 lenght 来初始化 gravity []

我个人会重写该代码。制作一个名为&#34; Planet&#34;使用质量重力名称属性和函数 calcWeight() calcWeight(双质量) )在该对象内部,与您的功能相同。然后,您可以在主函数中创建 Planet [] 数组,并在 takeFromFile(Planet []行星)中使用它,通过文件初始化所有行星数组,而不仅仅是权重。但这只是我的偏好。

答案 3 :(得分:0)

double[] gravity = {};是一个空数组,所以你必须像其他数组一样填充它

答案 4 :(得分:0)

修改代码以使用对象&amp;名单。它变得更加清晰:

class Planet {
    String name;
    double weightOnPlanet;
    double gravity;
    double mass;
    public Planet(...) { ... } // Constructor.
}

public static void main(String[] args) throws IOException {
    List<Planet> planets = new ArrayList<>();
    planets.add(new Planet(...));
    ...
}