六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 42|回复: 0

关于返回二维数组排序后序号数组的问题求解

[复制链接]

升级  5.33%

60

主题

60

主题

60

主题

举人

Rank: 3Rank: 3

积分
216
 楼主| 发表于 2013-1-26 12:40:47 | 显示全部楼层 |阅读模式
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[0].length;MatrixElement[][] elementArray = new MatrixElement[rows][cols];for (int row = 0; row < rows; row++) {for (int col = 0; col < cols; col++) {elementArray[row][col] = new MatrixElement(sourceArray[row][col], row + 1);}}return elementArray;}/** * 获取某一列的值存入一个临时的tempList中, *  * 然后对这个tempList赋整列的值然后对其排序, *  * 再把排序后的tempList赋值回列去 *  * @param elementArray *            被转换的对象 * @return */public static MatrixElement[][] sortArrayCols(MatrixElement[][] elementArray) {int rows = elementArray.length;int cols = elementArray[0].length;for (int colIndex = 0; colIndex < cols; colIndex++) {List<MatrixElement> tempList = new ArrayList<MatrixElement>();for (int rowIndex = 0; rowIndex < rows; rowIndex++) {tempList.add(elementArray[rowIndex][colIndex]);}Collections.sort(tempList);for (int rowIndex = 0; rowIndex < rows; rowIndex++) {elementArray[rowIndex][colIndex] = tempList.get(rowIndex);}}return elementArray;}/** * @param elementArray *            从其中获取rowIndex,做成一个数组返回去 * @return */public static int[][] getRowsArray(MatrixElement[][] elementArray) {int rows = elementArray.length;int cols = elementArray[0].length;int[][] rowArray = new int[rows][cols];for (int row = 0; row < rows; row++)for (int col = 0; col < cols; col++)rowArray[row][col] = elementArray[row][col].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;}}

输出结果:

原来的数组:[68, 36, 22][59, 77, 39][81, 20, 17]排序后的数组:[2, 3, 3][1, 1, 1][3, 2, 2]




.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表