2 つの方法を使用して、プログラムをファイルに書き込もうとしています。エラーなしでコンパイルされますが、書き込み先のファイルを開くと、(0,0) の座標のみが出力されます。配列から調整された10000のランダムを出力することになっています。(0,0) 座標のみを出力するのはなぜですか? また、返品ステートメントを正しい場所に配置しましたか?
import java.io.*;
public class program
{
public static void main (String [] args) throws IOException
{
int points = 10000, dimension = 2, lengthA = 100;//int variables are declared and initialized
PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayPoints.txt"));
double length [] = new double [dimension];//array for length is declared
double coordinate [][] = new double [points][dimension];//coordinate array is declared
for (int i = 0; i < points; i++){
fileOut.println(java.util.Arrays.toString(coordinate[i]));
}
fileOut.close();//writes to file
}//end main method
public static double writeTofile (double length[], double coordinate[][], int points, int dimension, int lengthA)
{
int x = 0, y = 0;
for(int z = 0; z < dimension; z++){//fills the length array with the the set value of lengthA
length[z] = lengthA;
}
for(x = 0; x < points; x++){//runs 1000 times to print 1000 data points
for (y = 0; y < dimension; y++){//runs 2 times to print an x and y coordinate
coordinate [x][y]= (2 *Math.random() - 1) * length[y];// finds a random number in the range and assiigns it to the coordinate array
}//end for
}//end for
return coordinate[x][y];
}//main method
}//program
コードを変更し、コンパイルしました。ただし、印刷先のファイルを見ると、ランダムな文字 ([D@76f1fad1[D@889ec59...)] が見られます。なぜこうなった?
import java.io.*;
public class program
{
public static void main (String [] args) throws IOException
{
int points = 10000, dimension = 2, lengthA = 100;//int variables are declared and initialized
PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayPoints.txt"));
double length [] = new double [dimension];//array for length is declared
double coordinate [][] = new double [points][dimension];//coordinate array is declared
writeTofile(length, coordinate, points, dimension, lengthA);
for (int i = 0; i < points; i++){
fileOut.println(Arrays.toString(coordinate[i]));
}
fileOut.close();//writes to file
}//end main method
public static void writeTofile (double length[], double coordinate[][], int points, int dimension, int lengthA)
{
int x = 0, y = 0;
for(int z = 0; z < dimension; z++){//fills the length array with the the set value of lengthA
length[z] = lengthA;
}
for(x = 0; x < points; x++){//runs 1000 times to print 1000 data points
for (y = 0; y < dimension; y++){//runs 2 times to print an x and y coordinate
coordinate [x][y]= (2 *Math.random() - 1) * length[y];// finds a random number in the range and assiigns it to the coordinate array
}//end for
}//end for
}//main method
}//program