我無法弄清楚我試圖製作的程序存在的一些問題。它是關於從文件中獲取信息,對數據進行排序並將新的排序數據存儲到另一個文件中。但是因爲我對Java還不熟悉,所以在遇到一些功能和方法時遇到了一些麻煩。所以這是我迄今所做的:Java將新對象添加到TreeSet並對其進行排序
公共類Planina:
public abstract class Planina {
protected String mName;
protected String Country;
public Planina(String mname, String country)
{
mName = mname;
Country = country;
}
public Planina()
{
mName = "Name";
Country = "Country";
}
public void setmName(String mname)
{
mName = mname;
}
public String getmName()
{
return mName;
}
public void setCountry(String country){
Country = country;
}
public String getCountry(){
return Country;
}
public String toString()
{
return "Mountain name: " + mName + "; Country: " + Country;
}
}
公共類Vrah擴展Planina:
public class Vrah extends Planina implements Comparable<Vrah> {
private String pName;
private int Height;
public Vrah(String mname, String country, String pname, int height)
{
super(mname, country);
pName=pname;
Height=height;
if(height<0)
throw new IllegalArgumentException("Height cannot be negative!");
}
public Vrah()
{
super("Name","Country");
pName="Peak Name";
Height=0;
}
public void setpName(String pname)
{
pName = pname;
}
public void setHeight(int height)
{
Height = height;
if(height<0)
throw new IllegalArgumentException("Height cannot be negative!");
}
public String getpName()
{
return pName;
}
public double getHeight()
{
return Height;
}
public String toString()
{
return super.toString() + "; Peak Name: " + pName + "; Height: " + Height;
}
@Override
public int compareTo(Vrah o)
{
if(Height<o.Height)return -1;
if(Height>o.Height)return 1;
else return 0;
}
}
鏈比較:
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class ChainComparator implements Comparator<Vrah> {
private List<Comparator<Vrah>> listComparators;
@SafeVarargs
public ChainComparator(Comparator<Vrah>... comparators) {
this.listComparators = Arrays.asList(comparators);
}
public int compare(Vrah vr1, Vrah vr2) {
for (Comparator<Vrah> comparator : listComparators) {
int result = comparator.compare(vr1, vr2);
if (result != 0) {
return result;
}
}
return 0;
}
}
高度比較器:
import java.util.Comparator;
public class HeightComparator implements Comparator<Vrah> {
@Override
public int compare(Vrah vr1, Vrah vr2) {
return (int) (vr1.getHeight() - vr2.getHeight());
}
}
國家比較:
import java.util.Comparator;
public class CountryComparator implements Comparator<Vrah> {
@Override
public int compare(Vrah vr1, Vrah vr2) {
return vr1.getCountry().compareTo(vr2.getCountry());
}
}
公共類Varhove:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class Varhove{
private Set<Vrah>vSet = new TreeSet<Vrah>();
private String inFile = null;
private String outFile = null;
public Varhove(String infile, String outfile) throws IOException, FileNotFoundException {
inFile = infile;
outFile = outfile;
@SuppressWarnings("resource")
RandomAccessFile raf = new RandomAccessFile(inFile,"rw");
String temp_data="";
do {
temp_data="";
temp_data=raf.readLine();
if(temp_data != null){
String[] data = temp_data.split(" ");
String mname = String.valueOf(data[0]);
String country = String.valueOf(data[1]);
String pname = String.valueOf(data[2]);
int height = Integer.parseInt(data[3]);
vSet.add(new Vrah(mname, country, pname, height));
}
} while((temp_data != null) && (temp_data.compareTo("") != 0));
raf.close();
}
public void write() throws IOException,FileNotFoundException {
if(outFile==null)
return;
File original = new File(outFile);
original.delete();
BufferedWriter out = new BufferedWriter(new FileWriter(outFile));
for(Vrah it : vSet){
out.write(it.getmName() + " " + it.getCountry() + " " + it.getpName() + " " + it.getHeight());
out.newLine();
}
out.close();
}
public void printColl(){
for(Vrah it : vSet)
System.out.print(it.toString()+"\n");
}
}
主類:
import java.awt.List;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Varhove v_Coll;
System.out.println("Enter one of the following commands:");
System.out.println("1 - something..");
System.out.println("2 - something else..");
System.out.println("3 - exit");
@SuppressWarnings("resource")
Scanner scanchoice = new Scanner(System.in);
System.out.println();
int choiceentry = -1;
do {
System.out.println("Enter \"1\", \"2\" or \"3\"");
choiceentry = scanchoice.nextInt();
switch (choiceentry)
{
case 1:
try {
v_Coll = new Varhove("E:\\Programs\\Projects\\Java\\JavaProjectX\\inFile.txt", "E:\\Programs\\Projects\\Java\\JavaProjectX\\outFile.txt");
v_Coll.printColl();
v_Coll.write();
System.out.println("Data successfully saved.\n");
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
case 2:
Collections.sort(v_Coll, new ChainComparator(
new HeightComparator(),
new CountryComparator())
);
break;
case 3:
// .. exit program
break;
default:
System.out.println("Choice must be a value between 1 and 3.");
}
} while (choiceentry != 3);
}
}
所以我的第一個問題是,如何讓我的是增加了新的方法對象到數組,例如在菜單中有一個選擇,它允許您從鍵盤添加新的mName,Country,pName和Height。
我的第二個問題是關於這一行給了一個錯誤:
case 2:
Collections.sort(v_Coll, new ChainComparator(
new HeightComparator(),
new CountryComparator())
);
break;
我使用ChainedComparator在mulptiple領域數組進行排序,但它好像有一個關於它的問題:該方法排序在類型集合中不適用於參數。我知道這是關於在其他類中沒有實現Comparable的問題,但有沒有辦法解決它,仍然使用ChainedComparator。任何幫助或例子將不勝感激。感謝您的時間。
你意識到你可以使用Java 8類而不用所有額外的麻煩?另外,你在'Varhove'上有一個'Comparable'原始類型。 –
我忘了刪除關於Comparable的部分。 – Cr1ms0nStraY