gpt4 book ai didi

java - 我有一个将值存储到 double[][] 实例变量中的构造函数。我想在我的主函数中使用双数组作为变量

转载 作者:行者123 更新时间:2023-11-29 03:27:40 26 4
gpt4 key购买 nike

我有一个构造函数,它接收一个 txt 文件的行数/列数和文件位置,并将这些值存储到一个 String 数组中。

int rows;
int columns;
int count;
File fileLocation;
String[][] stringArray;
Double[][] doubleArray; // Used in my convertToDoubleArray() method

public StoreArray(int rowsI, int columnsI, File fileLocationI){

int i;
int j;

InputStream fileInputStream;
BufferedReader bufferedReader;
String line;

rows = rowsI;
columns = columnsI;
count = 0;
fileLocation = fileLocationI;
stringArray = new String[rows][columns];

try{
fileInputStream = new FileInputStream(fileLocation);
bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, Charset.forName("UTF-8")));

for(i = 0; i < rows; i++){ // iterate through row
for(j = 0; j < columns; j++){ // iterate through column
while((line = bufferedReader.readLine())!= null){ // while the next line is not null
stringArray[i][j] = line; // assign i-th j-th index as line (RGB value)
count++;
break;
}
}
}
bufferedReader.close();
}catch(Exception e){
e.printStackTrace();
}
}

这样做的目的是因为我正在使用一个名为 JHeatChart 的 Java 库,它接收 double 数组并将包含数组的值显示为热图。

所以我使用的主要功能是:

File timeValuePath = new File(filePath);
int timeValueRow = 93;
int timeValueColumn = 14;

StoreArray timeValueArray = new StoreArray(timeValueRow,timeValueColumn,timeValuePath);
timeValueArray.convertToDoubleArray();

convertToDoubleArray() 是我编写的一种方法,它只接受每个 String 输入(因为 BufferedReader 只允许 String 参数)并将其转换为 double。当输入不是 Double 时,它​​也会返回错误。

因为 JHeatChart 的输入是一个 double[][],我的构造函数确实将值存储到一个 double[][]例如,如何将 timeValueArray 对象“使用”为 double 组?如果我没有正确使用术语,我深表歉意。谢谢。

最佳答案

public Double[][] convertToDoubleArray(){
int i;
int j;
doubleArray = new Double[rows][columns];

for(i = 0; i < rows; i++){ // iterate through row
for(j = 0; j < columns; j++){ // iterate through column
doubleArray[i][j] = Double.parseDouble(stringArray[i][j]); // cast each string value to
}
}
return doubleArray;
}

这实际上是一个非常简单的修复。这只是我对对象的根本误解。我创建了一个返回 double 数组的方法。

关于java - 我有一个将值存储到 double[][] 实例变量中的构造函数。我想在我的主函数中使用双数组作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20197339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com