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 [email protected] 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]));
}
}
使用した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;
}
@Override
public 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]
..。