1

谁能告诉我这是否可以做到,如果可以,怎么做?

我有一个List<float> FocalLengthList填充了一些值的值。然后这个列表存储在一个List<List<float>> MainFocalLenghtList. 但是,在我的应用程序中,我需要使用来自MainFocalLenghtList 的值来更新对象的 3D 位置。所以我需要转换fromMainFocalLenghtList [0]int.

这可以做到吗?怎么做?

这是我的代码的一小部分来解释。将值添加到 FocalLengthList 然后将该列表添加到List<List<float>> MainFocalLenghtList

float newFocalLength = focalLength * pixelSize; 
FocalLengthList.Add(newFocalLength); 
MainFocallengthList.Add(FocalLengthList); 
FocalLengthList = new List<float>(); 

然后我打算如何使用这些值(不起作用)

int zComponent = MainFocallengthList[0];
4

5 回答 5

3

You can certainly cast a float to an int, as long as you do so explicitly (since it may involve a loss of precision).

The problem with the code you've posted is that you're indexing into a list of other lists. The value returned by MainFocallengthList[0] will itself be a List<float>. You must then index into that list to get a value you can actually cast to int.

Assuming both the target list and the target float in that list are at the first index of their respective containers:

int zComponent = (int)MainFocalLengthList[0][0];

That first index returns the FocalLengthList that you added to MainFocalLengthList. The second index returns the newFocalLength value that you added to FocalLengthList. Clear? :)

于 2012-04-09T20:37:15.563 回答
1

我可能会这样做:

int zComponent = (int)Math.Ceiling(MainFocallengthList[m][n]);

尽管您希望将实际值替换为m th中的第n项目。 FocalLengthList

于 2012-04-09T20:34:26.197 回答
1

Give this a shot:

var floatList = new List<float>();

var intList = floatList.Select(f => (int)Math.Ceiling(f)).ToList();
于 2012-04-09T20:43:27.347 回答
1

Since MainFocalLengthList is a List of List<float>

var intarr = Array.ConvertAll(MainFocalLengthList[0].ToArray(), f=>(int)f);
于 2012-04-09T20:43:33.923 回答
0

您可以这样做,但您需要内部和外部列表的索引:

// The first [0] indicates the index of the nested list within MainFocallengthList
// The second [0] indicates the index of the item that you want in the nested list
int zComponent = (int)(MainFocallengthList[0][0])
于 2012-04-09T20:34:37.880 回答