首页 > 解决方案 > 如何计算完成程序所需步骤的输出

问题描述

嗨,我有这个程序,我想在运行 10 次时计算并打印完成该程序所需的总步骤。(计算总控制台行数)

任何建议表示赞赏

哈希表的代码:

public class HashFunction {

String[] theArray;
int arraySize;
int itemsInArray = 0;
    int K = 0;

/**
 *
 * @param args
 */
public static void main(String[] args) {


HashFunction theFunc = new HashFunction(101);

String[] elementsToAdd2 = new String[101];

for (int i = 0; i <= 100; i++) {
elementsToAdd2[i] = Integer.toString(RandomNumbers.getRandomNumberInRange(0, 15024267));
}



    theFunc.hashFunction2(elementsToAdd2, theFunc.theArray);

}


public void hashFunction2(String[] stringsForArray, String[] theArray) {

    for (int n = 0; n < stringsForArray.length; n++) {

        String newElementVal = stringsForArray[n];

        // Create an index to store the value in by taking
        // the modulus

        int arrayIndex = Integer.parseInt(newElementVal) % 101;

        System.out.println("P" + arrayIndex + " " + "I" + newElementVal + "@" + arrayIndex );

        // Cycle through the array until we find an empty space

        while (theArray[arrayIndex] != "-1") {

            ++arrayIndex;

            System.out.println( "P" + arrayIndex);

            // If we get to the end of the bucket go back to index 0

            arrayIndex %= arraySize;

        }

        theArray[arrayIndex] = newElementVal;

    }

}

HashFunction(int size) {

    arraySize = size;

    theArray = new String[size];

    Arrays.fill(theArray, "-1");

}

}

运行课程 10 次的程序:

public class RunTenTimes
    {
public static void main(String[] args)
  {
      for(int i=1; i<=10; i++)
          HashFunction.main(args); 
  }
}

我应该在主类还是 RunTenTimes 类中添加代码?

标签: javarandomhash

解决方案


推荐阅读