程序編譯但是當它寫入一個文件,這是會發生什麼吧:程序編譯,但不會寫入文件正確
[d @ 92ca580,[d @ 52257b34,[d @ 1abbbd0e ,[D @ 1b78efd8 .....並且它一直在繼續
我的代碼有什麼問題?我的結構是否正確? 當在主要方法中調用writefile方法時,我是否正確執行了這個操作?
更新** 我固定它,現在這是什麼文件上出現:
[0.0,0.0],[0.0,0.0],[0.0,0.0],[0.0, 0.0],[0.0,0.0] ...
我認爲問題在於我沒有直接調用writefile方法..那是爲什麼?我該如何解決它?
import java.io.*;
public class j4 {
public static void main (String [] args) throws IOException
{
int numpoints = 100, dimension = 2, length = 100;//numpoints is set to 100, dimension is set to 2, length is set to 100
//arrays are initializewd and declared
double [] lengthscale = new double [dimension];
double [][] locations = new double [numpoints][dimension];
PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayNumPoints.txt"));
for(int m=0; m <length; m++){//for loop
fileOut.println(java.util.Arrays.toString(locations) + ", ");//writes to file
}
fileOut.close();//close file
}//end main
public static Double writefile(Double locations[][], Double lengthscale[], int dimension, int numpoints, Double length)throws IOException
{
for (int a = 0; a < dimension; a++){//for loop runs while a is less than dimension
lengthscale[a] = length;//stores array
}//end for loop
for (int x=0; x < numpoints; x++){//for loop runs while x is less than numpoints
for (int y=0; y < dimension; y++){//nested for loop runs while y is less than dimension
locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];//creates the range and choses random point within
return locations[x][y];
}//end nested for loop
}//end for loop
//if program doesnt run through loop.. alternative return statement (but
double b= 1;
return b;
}//end writefile methos
}//end class
如果你描述你的程序是由_supposed_做的話,它會有很大幫助。 – MikeThomas
這就是數組的''#toString()''。正如我想你需要字符串內容,你需要迭代嵌套數組。 – qqilihq
@qqilihq嗨!你是什麼意思,你需要的字符串的內容和迭代嵌套數組?我是一個初學者,所以我有點困惑 – user101