将动态2D ArrayList转换为2D浮点数组 [英] Convert Dynamic 2D ArrayList to 2D Float Array

查看:93
本文介绍了将动态2D ArrayList转换为2D浮点数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了下面方法的返回部分.请参阅代码中的注释.经过多次尝试,我仍无法解决问题,因此需要一些新鲜的眼睛来寻找问题的根源.有什么建议或意见(例如ArrayList与List,其他)?非常感谢您的帮助.
谢谢
奥斯卡

//创建动态2D浮点ArrayList的方法,该方法通过从文件中读取数据数组来使其拥挤;然后返回2D浮点数组.

I got stuck in the return part of the method below. See the notes within the code. I have not been able to solve the problem after multiple attempts and need some fresh eyes to find the source of the problem. Any suggestions or comments (e.g., ArrayList vs. List, others)? Your help is greatly appreciated.
Thank you
Oscar

// Method that creates a dynamic 2D float ArrayList, crowds it by reading an array of data from a file; then returns a 2D float array.

public float[,] GetFoo()
{
string inputFileName = "MyData.txt";
//// Values in file MyData.txt
//3 -2 1 0 4
//-2 5 -1 1 7
//1 -1 -11 -2 2
//0 1 -2 12 3
//string outputFileName = "MyTest.txt";
char[] separator = { '\t'};
FileStream inFile = new FileStream(inputFileName, FileMode.Open);
StreamReader sr = new StreamReader(inFile);
//FileStream outFile = new FileStream(outputFileName, FileMode.Create);
//StreamWriter sw = new StreamWriter(outFile);
//System.Collections.ArrayList al = new ArrayList();
ArrayList al = new ArrayList();
int counter = 0;
string a = " ";
while (sr.Peek() != -1)
{
string strLine = sr.ReadLine();
strLine = strLine.Trim(' ');
string[] aValues = strLine.Split(separator);
int NumOfCols = aValues.Length;
for (int j = 0; j < NumOfCols; j++)
{
ArrayList alCol = new ArrayList();
al.Add(alCol);
float value;
value = Convert.ToSingle(aValues[j]);
((ArrayList) al[counter]).Add(value);
// This line is fine and displays the contents of the 2D array list
a += Convert.ToString(((ArrayList)al[counter])[j]) + '\t';
}
counter += 1;
a += '\n';
}
sr.Close();
//sw.Close();
MessageBox.Show(a, "Array A Test");
// This is the line with the problem! See message below.
return (float[,])al.ToArray(typeof(float[][]));
// Window Message
//"InvalidCastException" was unhandled.
//At least one element in the source array could not be cast down to the destination array type.
//When casting from a number, the value must be a number less than infinity.
}

推荐答案

您可能会混淆多维数组和锯齿状数组,尤其是在这里:
You might be confusing multidimensional arrays and jagged arrays, particularly here:
return (float[,])al.ToArray(typeof(float[][]));


float[,]float[][]之间有很大的区别-请参见此处 [ ^ ]和 ^ ]中的区别.

ArrayListArrayList与多维数组相比,更类似于锯齿数组. ToArray最有可能失败,因为它试图将al中的元素转换为floatfloat[]float[,]的一部分,并且不能因为它们是更多的ArrayList而不是浮空的".我敢打赌,您将必须手动将ArrayList转换为float[,],而不是使用ToArray来使您的方法正常工作.

同样,如今,List<>ArrayList更受青睐.如果您更改了该部分,我认为您会更容易理解我在说什么,因为您有一个List<List<float>>,很明显,外部列表包含更多列表,内部列表包含实际的浮点数.使用ArrayList时,类型会丢失,因此不清楚集合中的内容是什么.


There''s a big difference between a float[,] and a float[][] - see here[^] and here[^] for the differences.

The ArrayList of ArrayLists is more akin to a jagged array than a multidimensional one. The ToArray is most likely failing because it''s trying to convert the elements in al to either a float, float[] or part of a float[,] and it can''t because they''re more ArrayLists instead of something "float-ish". I''m betting that you''ll have to manually convert the ArrayList to the float[,] instead of using ToArray to get your method to work correctly.

Also, List<>s are preferred over ArrayLists nowadays. If you changed that part I think it''d be easier to see what I''m talking about because you have a List<List<float>> and it''s clear that the outer list holds more lists and the inner list holds the actual floats. With ArrayLists the types are lost so it''s not as clear what the things in the collection are.


这篇关于将动态2D ArrayList转换为2D浮点数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆