简体   繁体   English

程序编译但未正确写入文件

[英]Program compiles but doesn't write to file correctly

The program compiles but when it writes to a file this is what comes up: 该程序可以编译,但是当它写入文件时,将显示以下内容:

[[D@92ca580, [D@52257b34, [D@1abbbd0e, [D@1b78efd8..... and it keeps going on [[D @ 92ca580,[D @ 52257b34,[D @ 1abbbd0e,[D @ 1b78efd8 .....]

Whats wrong with my code? 我的代码有什么问题? Did I structure it properly? 我的结构是否正确? Also when calling the writefile method in the main method, did I do it correctly? 另外,在main方法中调用writefile方法时,是否正确执行了操作?

UPDATE** I fixed it up and now this what comes up on the file: 更新**我修复了它,现在文件中出现了以下内容:

[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]... [[0.0,0.0],[0.0,0.0],[0.0,0.0],[0.0,0.0],[0.0,0.0] ...

I think the problem is that i didnt directly call the writefile method.. is that why? 我认为问题是我没有直接调用writefile方法。为什么? and how can i fix it? 我该如何解决?

 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

Arrays.toString() does only create a string for your first array. Arrays.toString()只会为您的第一个数组创建一个字符串。 locations is a multi dimensional array (the array values contain another array). locations是一个多维数组(数组值包含另一个数组)。

You should try Arrays.deepToString() instead: 您应该尝试使用Arrays.deepToString()

Returns a string representation of the "deep contents" of the specified array. 返回指定数组的“深层内容”的字符串表示形式。 If the array contains other arrays as elements, the string representation contains their contents and so on. 如果数组包含其他数组作为元素,则字符串表示形式包含其内容,依此类推。 This method is designed for converting multidimensional arrays to strings. 此方法设计用于将多维数组转换为字符串。

Try using Arrays.deepToString instead, which 尝试改用Arrays.deepToString

Returns a string representation of the "deep contents" of the specified array. 返回指定数组的“深层内容”的字符串表示形式。 If the array contains other arrays as elements, the string representation contains their contents and so on. 如果数组包含其他数组作为元素,则字符串表示形式包含其内容,依此类推。 This method is designed for converting multidimensional arrays to strings . 此方法设计用于将多维数组转换为字符串

The assumption, however, is that you don't want the numbers printed in a specific format. 但是,假设是您不希望数字以特定格式打印。 In that case you are probably left with the choice of looping over the elements and printing them with desired formatting. 在这种情况下,您可能会选择循环遍历元素并以所需的格式打印它们。

In my opinion the above method is rather useful for logging/debugging operations. 我认为上述方法对于日志记录/调试操作非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM