Java - 使用数组创建不同的方法

标签 java arrays methods return

我正在尝试使用两种方法让我的程序写入文件。它编译没有错误,但是当打开它写入的文件时,只打印带有 (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

最佳答案

您没有调用用 main 方法中的值填充坐标数组的方法。

你可以这样调用它:

...
double coordinate [][] = new double [points][dimension];//coordinate array is declared

writeTofile(length, coordinate, points, dimension, lengthA)

for (int i = 0; i < points; i++){
    ...

至于 return 语句,它在正确的位置,但它会抛出 ArrayIndexOutOfBoundsException,因为它正在访问数组中不存在的偏移量。你想要一个返回值有什么用?

关于Java - 使用数组创建不同的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18969194/

相关文章:

java - SecureClassLoader 的目的是什么?

java - 如何在 Java 中将字符串添加到字符串数组?

java - 没有完全爆炸耳朵的 Ant 耳朵更新

java - 从 CalenderView 禁用星期五

android - 如何正确设置数组的大小?

iphone - 将按钮的目标和选择器设置为多种方法

iphone - 无限循环添加 subview

java - 如何为已经存在的类创建一个方法,以便它使用一个变量,并从中将其作为参数调用?

java - 如何使用 JPL 在 java 和 prolog 之间共享对象状态?

javascript - Javascript 中的数组未更新