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


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[i]));
}
}



[color=green]使用到的MatrixElement类:[/color]


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;
}

@Override
public int compareTo(MatrixElement anotherEmlement) {
//比较只需要对value比较
int valueCmp = this.value - anotherEmlement.value;
return valueCmp;
}
}



[color=green]输出结果:[/color]


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




.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值