在java中,如何从try-catch块返回数组?
我通过读取文件创建了一个二维数组,我想返回这个数组。但是我使用try-catch块,因为我正在读取文件。此时,我无法从此函数返回此数组。另外,当我尝试在try-catch块中写入此数组的元素时,它可以工作,但在块外则不能。我应该如何获得阵列 我的代码如下:在java中,如何从try-catch块返回数组?,java,arrays,try-catch,Java,Arrays,Try Catch,我通过读取文件创建了一个二维数组,我想返回这个数组。但是我使用try-catch块,因为我正在读取文件。此时,我无法从此函数返回此数组。另外,当我尝试在try-catch块中写入此数组的元素时,它可以工作,但在块外则不能。我应该如何获得阵列 我的代码如下: public static double[][] readFile(String fileName) { final double[][]data = new double[178][13]; int x=0, y=
public static double[][] readFile(String fileName) {
final double[][]data = new double[178][13];
int x=0, y=0;
try{
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while((line = reader.readLine())!= null){
String[]values=line.split(",");
for(String str:values){
double str_double=Double.parseDouble(str);
data[x][y]=str_double;
System.out.print(data[x][y]+" ");
}
System.out.println();
}
reader.close();
}
catch(Exception e){}
return data;
}
最后,在大家的帮助下,我找到了一个解决办法。我的一个错误是定义y。因为每行有14个元素,但我把它分配给了13个。我还改变了数组的赋值方法。也许任何人都需要,所以我将其发布到我的解决方案:
public static double[][] readFile(String fileName) {
double[][]data=new double[178][14];
int x=0, y;
try{
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
String[]values;
while((line = reader.readLine())!= null){
values=line.split(",");
for(y=0; y<values.length; y++){
data[x][y]=Double.parseDouble(values[y]);
}
x++;
}
reader.close();
}
catch(Exception e){System.out.println("Aborting due to error: " + e);}
return data;
}
publicstaticdouble[]readFile(字符串文件名){
双精度[]数据=新双精度[178][14];
int x=0,y;
试一试{
BufferedReader reader=新的BufferedReader(新文件读取器(文件名));
弦线;
字符串[]值;
而((line=reader.readLine())!=null){
值=行分割(“,”);
对于(y=0;y,原始代码在数组中只分配一个值,即元素data[0][0]
,因为x
和y
从不从0更改
假设x
是行,y
是列(行中的元素),则以下操作应有效:
public static double[][] readFile(String fileName) {
final double[][]data = new double[178][13];
int x, y;
try{
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
x = 0; // Reset the line counter
while((line = reader.readLine()) != null) { // Read a new line
y=0; // Reset the column counter, since this is a new line
String[]values=line.split(",");
for(String str:values){
double str_double=Double.parseDouble(str);
data[x][y]=str_double;
System.out.print(data[x][y]+" ");
++y; // Advance to the next column
}
++x; // Advance to the next line
System.out.println();
}
reader.close();
} catch (Exception e) {
System.out.println("Aborting due to error: " + e);
}
return data;
}
当然,上面假设调用readLine()
或parseDouble()
不会导致任何错误。如果发生任何此类错误,或者发生ArrayIndexOutOfBoundsException
异常,则返回的数组将只包含到目前为止读取的元素
这是一个改进的版本,再次尽可能多地保留原始代码:
public static double[][] readFile(String fileName) {
ArrayList<double[]> numbers = new ArrayList<double[]>();
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine()) != null) { // Read a new line
int y=0; // Reset the column counter, since this is a new line
String[] values = line.split(",");
if (values.length > 0) {
double[] elements = new double[values.length];
for (String str:values) {
double str_double;
try {
str_double = Double.parseDouble(str);
elements[y] = str_double;
System.out.print(elements[y] + " ");
++y; // Advance to the next column
} catch (Exception e) {
continue; // Not a double, ignore
}
}
numbers.add(elements);
} else {
numbers.add(new double[0]); // No numbers read from the line
}
System.out.println();
}
reader.close();
} catch (Exception e) {
System.out.println("Aborting due to error: " + e);
}
return numbers.toArray(new double[0][0]);
}
publicstaticdouble[]readFile(字符串文件名){
ArrayList编号=新的ArrayList();
试一试{
BufferedReader reader=新的BufferedReader(新文件读取器(文件名));
弦线;
而((line=reader.readLine())!=null){//读取新行
int y=0;//重置列计数器,因为这是新行
字符串[]值=行。拆分(“,”);
如果(value.length>0){
double[]元素=新的double[values.length];
for(字符串str:values){
双stru_双;
试一试{
str_double=double.parseDouble(str);
元素[y]=str_-double;
系统输出打印(元素[y]+“”);
++y、 //前进到下一列
}捕获(例外e){
continue;//不是双精度的,忽略
}
}
数字。添加(元素);
}否则{
numbers.add(新的双精度[0]);//没有从该行读取数字
}
System.out.println();
}
reader.close();
}捕获(例外e){
System.out.println(“由于错误而中止:+e”);
}
返回号码。toArray(新的双精度[0][0]);
}
这两个版本都没有经过测试,但它们都应该接近一个可行的解决方案
第二种方法更通用,应该是首选方法,尽管为了避免在double
和double
之间装箱/拆箱,它假设,如果读取为字符串的任何元素不是有效的double
,则元素
数组中的相应位置将由下一个有效的double填充e> ,如果有。因此,例如,如果一行中有10个元素,并且总共只有8个元素有效,那么数组的前8个位置将包含这些元素的值,最后2个位置将包含0
(在元素
数组初始化期间设置).由于x
和y
从不从0更改,因此原始代码在数组中仅分配一个值,即元素数据[0][0]
假设x
是行,y
是列(行中的元素),则以下操作应有效:
public static double[][] readFile(String fileName) {
final double[][]data = new double[178][13];
int x, y;
try{
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
x = 0; // Reset the line counter
while((line = reader.readLine()) != null) { // Read a new line
y=0; // Reset the column counter, since this is a new line
String[]values=line.split(",");
for(String str:values){
double str_double=Double.parseDouble(str);
data[x][y]=str_double;
System.out.print(data[x][y]+" ");
++y; // Advance to the next column
}
++x; // Advance to the next line
System.out.println();
}
reader.close();
} catch (Exception e) {
System.out.println("Aborting due to error: " + e);
}
return data;
}
当然,上面假设调用readLine()
或parseDouble()
不会导致任何错误。如果发生任何此类错误,或者发生ArrayIndexOutOfBoundsException
异常,则返回的数组将只包含到目前为止读取的元素
这是一个改进的版本,再次尽可能多地保留原始代码:
public static double[][] readFile(String fileName) {
ArrayList<double[]> numbers = new ArrayList<double[]>();
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine()) != null) { // Read a new line
int y=0; // Reset the column counter, since this is a new line
String[] values = line.split(",");
if (values.length > 0) {
double[] elements = new double[values.length];
for (String str:values) {
double str_double;
try {
str_double = Double.parseDouble(str);
elements[y] = str_double;
System.out.print(elements[y] + " ");
++y; // Advance to the next column
} catch (Exception e) {
continue; // Not a double, ignore
}
}
numbers.add(elements);
} else {
numbers.add(new double[0]); // No numbers read from the line
}
System.out.println();
}
reader.close();
} catch (Exception e) {
System.out.println("Aborting due to error: " + e);
}
return numbers.toArray(new double[0][0]);
}
publicstaticdouble[]readFile(字符串文件名){
ArrayList编号=新的ArrayList();
试一试{
BufferedReader reader=新的BufferedReader(新文件读取器(文件名));
弦线;
而((line=reader.readLine())!=null){//读取新行
int y=0;//重置列计数器,因为这是新行
字符串[]值=行。拆分(“,”);
如果(value.length>0){
double[]元素=新的double[values.length];
for(字符串str:values){
双stru_双;
试一试{
str_double=double.parseDouble(str);
元素[y]=str_-double;
系统输出打印(元素[y]+“”);
++y、 //前进到下一列
}捕获(例外e){
continue;//不是双精度的,忽略
}
}
数字。添加(元素);
}否则{
numbers.add(新的双精度[0]);//没有从该行读取数字
}
System.out.println();
}
reader.close();
}捕获(例外e){
System.out.println(“由于错误而中止:+e”);
}
返回号码。toArray(新的双精度[0][0]);
}
奈斯