我写了一个小的临时程序来检查按行或列循环遍历二维数组的时间,因为我记得大数据集的一种方法更快,但我不记得哪种方式.当我运行我的程序时,我遇到了一个有趣的错误.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at testing.ForLoopTest.main(ForLoopTest.java:10)
Run Code Online (Sandbox Code Playgroud)
现在在我的测试中,我使用在eclipse中使用默认堆设置运行的int [10000] [10000].我知道我可以增加堆大小,但我的直觉告诉我,这应该运行得很好而不需要我这样做.这是正常的吗?在我的代码中有些愚蠢的东西我没有看到吗?
这是我的代码
public static void main(String[] args){
final int arrSize = 10000;
long start = 0;
long rowFirstTime = 0;
long colFirstTime = 0;
int[][] arr = new int[arrSize][arrSize];
start = System.currentTimeMillis();
for(int x = 0; x < arrSize; x++){
for(int y = 0; y < arrSize; y++){
arr[x][y] = -1;
}
}
rowFirstTime = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
for(int y = 0; y < …Run Code Online (Sandbox Code Playgroud)