您的位置:首页 > 其它

2013十大突破性科技

2013-12-17 18:41 351 查看
有如下一些数据:
1-2-3-4-5
2-3-2
2-4-4-2
1-6
保存在一个文本文档中(ANSI编码格式)。要求排序成如下结果:

1-6
1-2-3-4-5
2-4-4-2
2-3-2

即以每一行的最后一个元素进行排序,如果相同再以之前一个排序。都是降序。。

有兴趣的可以试试。

package com.watersoft.file;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Sort {
private int lineCount;
private int numberCount;
private String path;
private int[][] matrix;

/**
* *
*
* @param args
*            contains the path of a text file which include the data being
*            sorted
*/
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("Specific a file please!");
return;
}
System.out.println(">>> Information: sort start ..\n");
Sort s = new Sort();
s.setPath(args[0]);
s.doSort();
s.display();
System.out.println("\n>>> Information: sort end ..");
}

/** * do sort: initialize, parse, then sort. */
public void doSort() {
init();
parse();
for (int index = 0; index < this.lineCount - 1; index++)
for (int innerIndex = 0; innerIndex < this.lineCount - 1; innerIndex++)
sort(innerIndex, this.numberCount - 1);
}

/**
* * sort the given data according to the given row index and column index *
*
* @param rowIndex
*            current row *
* @param colIndex
*            current column
*/
private void sort(int rowIndex, int colIndex) {
int aInt = this.matrix[rowIndex][colIndex];
int bInt = this.matrix[rowIndex + 1][colIndex];
int temp = 0;
if (aInt < bInt) { /* * swap two rows */
for (int index = 0; index < this.numberCount; index++) {
temp = this.matrix[rowIndex][index];
this.matrix[rowIndex][index] = this.matrix[rowIndex + 1][index];
this.matrix[rowIndex + 1][index] = temp;
}
} else if (aInt == bInt) { /* * compare previous elements of two rows */
colIndex--;
sort(rowIndex, colIndex);
}
}

/**
* * read file to make sure the dimension of matrix *
*
* @return boolean value, true if successfully done, else false
*/
private boolean init() {
try {
FileReader fr;
fr = new FileReader(this.path);
BufferedReader br = new BufferedReader(fr);
String tempString;
int lc = 0;
int nc = 0;
while ((tempString = br.readLine()) != null) {
lc++;
nc = (nc < tempString.split("-").length) ? tempString
.split("-").length : nc;
}
this.lineCount = lc;
this.numberCount = nc;
} catch (FileNotFoundException e) {
System.err.println("Error: cannot find file:[" + this.path + "]");
return false;
} catch (IOException e) {
System.err.println("Error: cannot read file..");
return false;
}
return true;
}

private boolean parse() { /* * initialize matrix */
this.matrix = new int[this.lineCount][this.numberCount];
for (int index = 0; index < this.lineCount; index++)
for (int innerIndex = 0; innerIndex < this.numberCount; innerIndex++)
this.matrix[index][innerIndex] = Integer.MAX_VALUE;
try {
FileReader fr;
fr = new FileReader(this.path);
BufferedReader br = new BufferedReader(fr);
String tempString;
String[] tempArray;
int lc = 0;
int baseIndex = 0;
while ((tempString = br.readLine()) != null) {
tempArray = tempString.split("-");
baseIndex = this.numberCount - tempArray.length;
for (int index = baseIndex; index < this.numberCount; index++)
this.matrix[lc][index] = Integer.parseInt(tempArray[index
- baseIndex]);
lc++;
}
} catch (FileNotFoundException e) {
System.err.println("Error: cannot find file:[" + this.path + "]");
return false;
} catch (IOException e) {
System.err.println("Error: cannot read file..");
return false;
}
return true;
}

/** * display sorted result */
public void display() {
for (int index = 0; index < this.lineCount; index++) {
for (int innerIndex = 0; innerIndex < this.numberCount; innerIndex++) {
if (this.matrix[index][innerIndex] != Integer.MAX_VALUE)
if (innerIndex + 1 != this.numberCount)
System.out.print(this.matrix[index][innerIndex] + "-");
else
System.out.print(this.matrix[index][innerIndex]);
}
System.out.println();
}
}

public void setPath(String path) {
this.path = path;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: