Home > Java > javaTutorial > body text

Solution to the problem of returning a sequence number array after sorting a two-dimensional array

巴扎黑
Release: 2016-12-20 13:51:29
Original
1932 people have browsed it

MatrixElement class used by

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]));
}
}
Copy after login

:

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;
}
}
Copy after login

Output result:

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


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!