我有这个构造类,用于汽车的制造,型号,年份和里程。
public class Car {
private String make,model;
private int year,mileage;
public String toString() {
return "Car [make=" + make + ", model=" + model +", "
+ "year =" + year + ", mileage=" + mileage + "]"; }
public String getMake() {
return make;
}
public Car(String make, String model, int year, int mileage) {
super();
this.make = make;
this.model = model;
this.year = year;
this.mileage = mileage;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMileage() {
return mileage;
}
public void setMileage(int mileage) {
this.mileage = mileage;
}
}
现在,我有一个主类,可以从文本文件中读取数据,该文本文件具有该品牌,型号,年份,里程的顺序,并将其存储到数组中。
public class storing {
public static void main(String[] args) {
TextFileInput fileInput = new TextFileInput("CarDetails.txt");
ArrayList <Car> cars = new ArrayList<Car>();
String line = fileInput.readLine();
while(line!=null) {
String [] values = new String [4];
StringTokenizer st = new StringTokenizer(line,",");
int numOfCommas = st.countTokens();
for(int i = 0;i<numOfCommas;i++) {
values[i]=(st.nextToken(","));
}
cars.add(new Car(values[0],values[1],Integer.parseInt(values[2]),Integer.parseInt(values[3])));
line = fileInput.readLine();
}
CarGUI gui = new CarGUI(cars);
}
}//main
我在理解应该如何实现Gui部分方面遇到麻烦,但是我的选择排序似乎无效。
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.util.ArrayList;
public class CarGUI extends JFrame {
private JTextArea unsorted,sorted;
private ArrayList<Car> cars;
public CarGUI(ArrayList<Car> c) {
cars = c;
setTitle("CarGUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,400);
setLocationRelativeTo(null);
setComponents();
setVisible(true);
}
private void setComponents() {
setLayout(new GridLayout(1,2));
unsorted = new JTextArea();
unsorted.setBackground(Color.CYAN);
unsorted.setFont(new Font(Font.DIALOG, Font.BOLD, 12));
unsorted.setEditable(false);
unsorted.setBorder(BorderFactory.createLineBorder(Color.black));
for(int i=0;i<cars.size();i++) {
unsorted.append(cars.get(i).toString()+"\n");
}
add(unsorted);
selectionSort(cars);
sorted = new JTextArea();
sorted.setBackground(Color.YELLOW);
sorted.setFont(new Font(Font.DIALOG, Font.BOLD, 12));
sorted.setEditable(false);
sorted.setBorder(BorderFactory.createLineBorder(Color.black));
for(int i=0;i<cars.size();++i) {
sorted.append(cars.get(i).toString()+"\n");
}
add(sorted);
}
private void selectionSort(ArrayList<Car> c) {
for (int i = 0; i<c.size()-1;i++) {
int indexLowest = i;
for(int j = i + 1;j<c.size();j++) {
if (c.get(j).make() < c.get(indexLowest).make()) {indexLowest = j;}
}
if ( c.get(indexLowest) != c.get(i) ) {
Car temp = c.get(indexLowest);
c.set(indexLowest,c.get(i));
c.set(i, temp);
}
}
}//SelectionSort
}
我在这里的代码没有运行,但是我不确定为什么。请帮助我了解我出了什么问题。