交换二维数组的行列

转自:http://www.riafan.com/article.asp?id=139

package com.riafan.utils {

/**
* Class that contains static utility methods for manipulating and working
* with Arrays
* @author Flying
* @version 1
* @tiptext
*/
public class ArrayUtil {

/**
* Switch the rows and columns of a 2d array array
* @param arr The 2d array whose dimensions will be switched
* @return A new array which contains items after switch
*/
public static function switchDimensions(arr : Array) : Array {
var newArr : Array = new Array();
var rowMax : uint = arr.length;
var colMax : uint;
try {
if(arr[0] is Array) {
colMax = arr[0].length;
}else {
throw new TypeError("muse be a 2d array");
}
}
catch (error : TypeError) {
trace(error.message);
}
for (var col : uint = 0; col < colMax; col++) {
var tmpArr : Array = new Array();
for (var row : uint = 0; row < rowMax; row++) {
tmpArr[row] = (arr[row][col]);
}
newArr[col] = tmpArr;
}
return newArr;
}
}
}

你可能感兴趣的:(asp)