2016-08-02 55 views
3

比方說,我有花車轉換彩車的二維數組的bool

float [,] myfloats = new float[10, 10] 
FillWithValues(myfloats); 

我如何轉換這種二維float數組的數組二維,爲二維數組布爾值,使用隨機評價方法?

我希望這樣的事情:

bool[,] mybools; 
mybools = myfloats.Select(myfloat => Evaluate(myfloat)); 

但是,這並不工作...

+0

我希望有一種映射方法,使它更加優雅。但如果這是你的答案,請張貼代碼:)林不知道如何循環通過一個二維數組.. –

+0

請參閱@Jon Skeet的答案,這應該有助於理解爲什麼「更優雅」缺乏:http:// stackoverflow。 com/questions/275073/why-do-c-sharp-multidimensional-arrays-not-implement -ienumerablet – blins

回答

3

不幸的是,多維數組不與LINQ和兼容有點難以處理。我不認爲這是解決這個雙迴路解決辦法:

bool[,] mybools = new bool[myfloats.GetLength(0), myfloats.GetLength(1)]; 
for (int x = 0; x<myfloats.GetLength(0); x++) 
    for(int y=0; y<myfloats.GetLength(1); y++) 
     mybools[x,y] = Evaluate(myfloats[x,y]); 

Array.GetLength(Int32)爲您提供了陣列的具體尺寸大小。

1

這不是特別有效,但是有兩個擴展方法,你可以這樣做:

var floats = new float[3, 3] { 
    { 1f, 2f, 3f }, 
    { 4f, 5f, 6f }, 
    { 7f, 8f, 9f } 
}; 

Func<float, bool> floatToBool = f => f > 5f; 

var bools = 
    floats 
    .ToJaggedArray() 
    .Select(array => array.Select(floatToBool).ToArray()) 
    .ToArray() 
    .To2DArray(); 

請注意,你必須處理它放回一個「數組的數組」,回到一個二維數組。

如果我們轉換爲enumerable而不是鋸齒狀數組,它會更好。然後,它可能看起來像這樣:

var bools = 
    floats 
    .ToEnumerableOfEnumerables() 
    .Select(array => array.Select(floatToBool)) 
    .To2DArray(); 

兩個在第一部分的擴展方法是這樣的:

[Pure, NotNull] 
public static T[][] ToJaggedArray<T>([NotNull] this T[,] p2DArray) { 
    var height = p2DArray.GetLength(0); 
    var width = p2DArray.GetLength(1); 

    var result = new T[height][]; 

    for (var row = 0; row < height; row++) { 
     result[row] = new T[width]; 
     for (var col = 0; col < width; col++) 
     result[row][col] = p2DArray[row, col]; 
    } 

    return result; 
} 

[Pure, NotNull] 
public static T[,] To2DArray<T>([NotNull] this T[][] pJaggedArray) { 
    if (!pJaggedArray.Any() || !pJaggedArray[0].Any()) 
     return new T[0, 0]; 

    var height = pJaggedArray.Length; 
    var width = pJaggedArray[0].Length; 

    var result = new T[height, width]; 
    for (var r = 0; r < height; r++) { 
     var subArray = pJaggedArray[r]; 
     if (subArray == null || subArray.Length != width) 
     throw new InvalidOperationException("Jagged array was not rectangular."); 

     for (var c = 0; c < width; c++) 
     result[r, c] = subArray[c]; 
    } 

    return result; 
} 

而對於第二部分是這樣的:

[Pure, NotNull] 
public static IEnumerable<IEnumerable<T>> ToEnumerableOfEnumerables<T>(
    [NotNull] this T[,] p2DArray 
) { 
    var height = p2DArray.GetLength(0); 

    for (var row = 0; row < height; row++) 
     yield return p2DArray.GetPartialEnumerable(row); 
} 

[Pure, NotNull] 
public static IEnumerable<T> GetPartialEnumerable<T>(
    [NotNull] this T[,] p2DArray, 
    int pRow 
) { 
    var width = p2DArray.GetLength(1); 

    for (var col = 0; col < width; col++) 
     yield return p2DArray[pRow, col]; 
} 

[Pure, NotNull] 
public static T[,] To2DArray<T>(
    [NotNull] this IEnumerable<IEnumerable<T>> pJaggedArray 
) { 
    if (!pJaggedArray.Any() || !pJaggedArray.First().Any()) 
     return new T[0, 0]; 

    var height = pJaggedArray.Count(); 
    var width = pJaggedArray.First().Count(); 

    var result = new T[height, width]; 
    var r = 0; 
    foreach (var subArray in pJaggedArray) { 
     if (subArray == null || subArray.Count() != width) 
     throw new InvalidOperationException("Jagged array was not rectangular."); 

     var c = 0; 
     foreach (var item in subArray) 
     result[r, c++] = item; 

     r += 1; 
    } 

    return result; 
} 
+0

Yuck!你有點過度了:Enumerable.Range(0,n).Select(x => Enumerable.Range(0,m).Select(y =>(stuff [x,y] =(x * y) )> 10).ToArray())。ToArray();' - 你既可以返回新數組,也可以通過這種方式爲現有值賦值(ToList()/ ToArray()可能會被建議實際執行枚舉)。當然返回類型將是[n] [m],而不是[n,m],因爲在這種情況下不應該使用LINQ;) – PTwr