Java作业MaxSumTest
我目前正在尝试修改我的数据结构类的 Java 家庭作业程序,该程序在表中显示 MaxSumTest 程序的输出。
我创建了四个数组,并将它们放置在计算计时信息的循环之一内。我试图仅使用一种算法的计时信息填充每个数组。所以每个数组有 4 个元素。然而,每次我运行程序的修订版时,我都会收到越界错误。
我修改的唯一方法是 getTimingInfo。这是我的修订:
public static void getTimingInfo( int n, int alg )
{
int [] test = new int[ n ];
long startTime = System.currentTimeMillis( );;
long totalTime = 0;
//create an array for each Algorithm
long[] alg4;
long[] alg3;
long[] alg2;
long[] alg1;
//allocate memory for 5 long ints
alg4 = new long[5];
alg3 = new long[5];
alg2 = new long[5];
alg1 = new long[5];
int i;
int j;
int index = 0;
for( i = 0; totalTime < 4000; i++ )
{
for( j = 0; j < test.length; j++ )
test[ j ] = rand.nextInt( 100 ) - 50;
index = j;
switch( alg )
{
case 1:
maxSubSum1( test );
break;
case 2:
maxSubSum2( test );
break;
case 3:
maxSubSum3( test );
break;
case 4:
maxSubSum4( test );
break;
}
totalTime = System.currentTimeMillis( ) - startTime;
}
alg1[index] = totalTime * 1000 / i;
alg2[index] = totalTime * 1000 / i;
alg3[index] = totalTime * 1000 / i;
alg4[index] = totalTime * 1000 / i;
//Build first column of table
System.out.println("Size of N Algorithms\t" + "250\t" + "2500\t" + "25000\t" + "250000");
System.out.println("Alg #4\t" + alg4[0] + "\t" + alg4[1] + "\t" + alg4[2] + "\t" + alg4[3] + "\t" + alg4[4]);
System.out.println("Alg #3\t" + alg3[0] + "\t" + alg3[1] + "\t" + alg3[2] + "\t" + alg3[3] + "\t" + alg3[4]);
System.out.println("Alg #2\t" + alg2[0] + "\t" + alg2[1] + "\t" + alg2[2] + "\t" + alg2[3] + "\t" + alg2[4]);
System.out.println("Alg #1\t" + alg1[0] + "\t" + alg1[1] + "\t" + alg1[2] + "\t" + alg1[3] + "\t" + alg1[4]);
/*
System.out.println( "Algorithm #" + alg + "\t"
+ "N = " + test.length
+ "\ttime = " + ( totalTime * 1000 / i ) + " microsec" );
*/
}
任何正确方向的建议或指示将不胜感激。
I am currently attempting to revise a Java Homework program for my Data Structures class that displays the output of the MaxSumTest program in a table.
I've created four arrays, and placed them inside one of the loops that calculates the timing info. I am trying to populate each array with only the timing info for one algorithm. So each array would have 4 elements. However, every time I run my revision of the program, I get an out of bounds error.
The only method I revised is getTimingInfo. Here is my revision:
public static void getTimingInfo( int n, int alg )
{
int [] test = new int[ n ];
long startTime = System.currentTimeMillis( );;
long totalTime = 0;
//create an array for each Algorithm
long[] alg4;
long[] alg3;
long[] alg2;
long[] alg1;
//allocate memory for 5 long ints
alg4 = new long[5];
alg3 = new long[5];
alg2 = new long[5];
alg1 = new long[5];
int i;
int j;
int index = 0;
for( i = 0; totalTime < 4000; i++ )
{
for( j = 0; j < test.length; j++ )
test[ j ] = rand.nextInt( 100 ) - 50;
index = j;
switch( alg )
{
case 1:
maxSubSum1( test );
break;
case 2:
maxSubSum2( test );
break;
case 3:
maxSubSum3( test );
break;
case 4:
maxSubSum4( test );
break;
}
totalTime = System.currentTimeMillis( ) - startTime;
}
alg1[index] = totalTime * 1000 / i;
alg2[index] = totalTime * 1000 / i;
alg3[index] = totalTime * 1000 / i;
alg4[index] = totalTime * 1000 / i;
//Build first column of table
System.out.println("Size of N Algorithms\t" + "250\t" + "2500\t" + "25000\t" + "250000");
System.out.println("Alg #4\t" + alg4[0] + "\t" + alg4[1] + "\t" + alg4[2] + "\t" + alg4[3] + "\t" + alg4[4]);
System.out.println("Alg #3\t" + alg3[0] + "\t" + alg3[1] + "\t" + alg3[2] + "\t" + alg3[3] + "\t" + alg3[4]);
System.out.println("Alg #2\t" + alg2[0] + "\t" + alg2[1] + "\t" + alg2[2] + "\t" + alg2[3] + "\t" + alg2[4]);
System.out.println("Alg #1\t" + alg1[0] + "\t" + alg1[1] + "\t" + alg1[2] + "\t" + alg1[3] + "\t" + alg1[4]);
/*
System.out.println( "Algorithm #" + alg + "\t"
+ "N = " + test.length
+ "\ttime = " + ( totalTime * 1000 / i ) + " microsec" );
*/
}
Any advice or pointers in the right direction would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新数组元素的行不应该位于内部循环内部吗?
您在循环外部分配它们,其中
index
的值为 5,因此超出范围。Shouldn't the lines that update the array elements be inside the inner loop?
You are assigning them outside the loop, where the value of
index
is 5, so it's out of bounds.我不确定您到底想做什么,但我认为您真正想做的是多次调用
getTimingInfo()
,然后将结果分配给表,而不是更改getTimingInfo()
的内部工作原理。这要求您定义将在
getTimingInfo()
外部存储计时数据的数组,然后getTimingInfo()
内部的唯一更改将存储到该数组(已经定义的)数组,而不是打印。I'm not sure exactly what you are trying to do, but I think that what you really want to do is call
getTimingInfo()
a number of times and then assign the results to a table, not change the internal workings ofgetTimingInfo()
.This requires that you define the arrays in which you will store the timing data OUTSIDE of
getTimingInfo()
, and then the only change inside ofgetTimingInfo()
would be storing to that (already defined) array, rather than printing.可以更好地表达为
“写得更好”“
实际上做同样的事情”。
只是挑几点。
Can be better expressed as
Is written nicer as
Actually do the same thing.
Just to pick a few points.
您将 alg4、alg3、alg2 和 alg1 设置为大小 5,而不是您所说的需要的大小 4。
You are setting the alg4, alg3, alg2, and alg1 to size 5, not size 4 as you said you needed.