在课堂上,我们做了这门课,对双打排序并从分钟开始排列。最大。我有兴趣将此类更改为从具有名称的文本文件中读取并按字母顺序对它们进行排序,而不是从数字文本文件中读取的双精度数。
package versionDouble;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Datos {
double[] datos;
public Datos(String fileString) {
int cuantos = contarDatos( fileString );
this.datos = new double[ cuantos ];
leerDatos( fileString );
} // Datos
private void leerDatos(String fileString) {
try {
Scanner scanner = new Scanner(new File(fileString));
for (int i = 0; i < this.datos.length; i++) {
this.datos[i] = scanner.nextDouble();
} // for
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} // leerDatos
} // Datos class
@Override
public String toString() {
String string = "";
string = Arrays.toString( this.datos );
return string;
} // toString
private int contarDatos(String fileString) {
int contador = 0;
try {
Scanner scanner = new Scanner( new File(fileString));
while ( scanner.hasNext() ) {
scanner.nextDouble();
contador++;
} // while
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} // catch
return contador;
} // contarDatos
public void selectionSort() {
for (int startScan = 0; startScan < this.datos.length; startScan++) {
int minIndex = startScan;
double minValue = this.datos[ minIndex ];
for (int index = 0; index < this.datos.length; index++) {
if( this.datos[ index ] < minValue) {
minIndex = index;
minValue = this.datos[ index ];
} // if
} // for interno
if( minIndex != startScan ) {
double temporary = this.datos[startScan];
this.datos[ startScan ] = this.datos[ minIndex ];
this.datos [ minIndex ] = temporary;
} // swap
} // for externo
Arrays.sort( this.datos );
} // selectionSort
} // Class