关于返回二维数组排序后序号数组的问题求解
package com.cdl.matrix;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;/** *@source:http://blog.csdn.net/justinavril/archive/2009/12/14/5003467.aspx **@function: 矩阵A ** 68 36 22 ** 59 77 39 ** 81 20 17 ** 将矩阵A每列排序之后(升序排列)应该得到的矩阵是: ** 矩阵B: ** 59 20 17 ** 68 36 22 ** 81 77 39 ** 这样矩阵A中每个元素在矩阵B中对应的位置就是 ** 矩阵C: ** 2 3 3 ** 1 1 1 ** 3 2 2 ** 问:由矩阵A得到矩阵C的算法? ** @author ocaicai@yeah.net 2011-6-27 **/public class MatrixColSort {/** * @param args */public static void main(String[] args) {int[][] initArray = { { 68, 36, 22 }, { 59, 77, 39 }, { 81, 20, 17 } };System.out.println("原来的数组:");printArray(initArray);System.out.println("排序后的数组:");MatrixElement[][] transferedArray = transferByElement(initArray);MatrixElement[][] sortedArray = sortArrayCols(transferedArray);int[][] targerArray = getRowsArray(sortedArray);printArray(targerArray);}/** * 将数组中单个的value转换成Element(value,rowIndex) ** @param sourceArray * @return */public static MatrixElement[][] transferByElement(int[][] sourceArray) {int rows = sourceArray.length;int cols = sourceArray.length;MatrixElement[][] elementArray = new MatrixElement;for (int row = 0; row < rows; row++) {for (int col = 0; col < cols; col++) {elementArray = new MatrixElement(sourceArray, row + 1);}}return elementArray;}/** * 获取某一列的值存入一个临时的tempList中, ** 然后对这个tempList赋整列的值然后对其排序, ** 再把排序后的tempList赋值回列去 ** @param elementArray * 被转换的对象 * @return */public static MatrixElement[][] sortArrayCols(MatrixElement[][] elementArray) {int rows = elementArray.length;int cols = elementArray.length;for (int colIndex = 0; colIndex < cols; colIndex++) {List<MatrixElement> tempList = new ArrayList<MatrixElement>();for (int rowIndex = 0; rowIndex < rows; rowIndex++) {tempList.add(elementArray);}Collections.sort(tempList);for (int rowIndex = 0; rowIndex < rows; rowIndex++) {elementArray = tempList.get(rowIndex);}}return elementArray;}/** * @param elementArray * 从其中获取rowIndex,做成一个数组返回去 * @return */public static int[][] getRowsArray(MatrixElement[][] elementArray) {int rows = elementArray.length;int cols = elementArray.length;int[][] rowArray = new int;for (int row = 0; row < rows; row++)for (int col = 0; col < cols; col++)rowArray = elementArray.getRowIndex();return rowArray;}/** * 打印二维数组 ** @param array */public static void printArray(int[][] array) {for (int i = 0; i < array.length; i++)System.out.println(Arrays.toString(array));}}使用到的MatrixElement类:
package com.cdl.matrix;public class MatrixElement implements Comparable<MatrixElement> {int value;int rowIndex;public int getValue() {return value;}public void setValue(int value) {this.value = value;}public int getRowIndex() {return rowIndex;}public void setRowIndex(int rowIndex) {this.rowIndex = rowIndex;}public MatrixElement(int value, int rowIndex) {this.value = value;this.rowIndex = rowIndex;}@Overridepublic int compareTo(MatrixElement anotherEmlement) {//比较只需要对value比较int valueCmp = this.value - anotherEmlement.value;return valueCmp;}}
输出结果:
原来的数组:排序后的数组:
.
页:
[1]