渗透程序中的错误:线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:0

时间:2014-11-01 14:50:50

标签: java

您好我在尝试运行此程序时遇到此错误,我无法弄清楚它有什么问题:

  

线程中的异常" main" java.lang.ArrayIndexOutOfBoundsException:0   在PercolationStats.main

这是代码:

/****************************************************************
  * Compilation:  javac PercolationStats.java
  * Execution:    java PercolationStats N T
  * Dependencies: Percolation.java, StdStats.java, StdRandom.java,
  *               StdOut.java
  * 
  * % java PercolationStats 20 1000
  *      mean                    = 0.591100
  *      stddev                  = 0.046068
  *      95% confidence interval = 0.582071, 0.600129
  * 
  * % java PercolationStats 200 100
  *      mean                    = 0.593257
  *      stddev                  = 0.016242
  *      95% confidence interval = 0.592251, 0.594264
  * 
  ***************************************************************/

public class PercolationStats { 

    private int experimentsCount; 
    private Percolation pr; 
    private double[] fractions; 

    public PercolationStats(int N, int T) { 
        if (N <= 0 || T <= 0) { 
            throw new IllegalArgumentException("Given N <= 0 || T <= 0"); 
        } 

        experimentsCount = T; 
        fractions = new double[experimentsCount]; 
        for (int expNum = 0; expNum < experimentsCount; expNum++) { 
            pr = new Percolation(N); 
            int openedSites = 0; 
            while (!pr.percolates()) { 
              int i = StdRandom.uniform(1, N + 1); 
                int j = StdRandom.uniform(1, N + 1); 
                if (!pr.isOpen(i, j)) { 
                    pr.open(i, j); 
                    openedSites++; 
                } 
            } 
            double fraction = (double) openedSites / (N * N); 
            fractions[expNum] = fraction; 
        } 
    } 

    public double mean() { 
        return StdStats.mean(fractions); 
    } 

    public double stddev() { 
        return StdStats.stddev(fractions); 
    } 

    public double confidenceLo() { 
        return mean() - ((1.96 * stddev()) / Math.sqrt(experimentsCount)); 
    } 

    public double confidenceHi() { 
        return mean() + ((1.96 * stddev()) / Math.sqrt(experimentsCount)); 
    } 

    public static void main(String[] args) { 
        int N = Integer.parseInt(args[0]); 
        int T = Integer.parseInt(args[1]); 
        PercolationStats ps = new PercolationStats(N, T); 
        String confidence = ps.confidenceLo() + ", " + ps.confidenceHi(); 
        StdOut.println("mean                    = " + ps.mean()); 
        StdOut.println("stddev                  = " + ps.stddev()); 
        StdOut.println("95% confidence interval = " + confidence); 
    } 
}

1 个答案:

答案 0 :(得分:2)

这看起来像是来自其他来源的一些相当仔细编写的代码。

几乎可以肯定的是,它期望你向程序提供一些参数。您可以在代码顶部的注释中查看如何执行此操作。尝试使用

运行它
java PercolationStats 20 1000

看看会发生什么。

如果你是从eclipse中运行它,你可以在运行配置中设置参数。