从文件中读取并选择按价格排序存储的对象数组无效



我真的被困在这里了,我不知道为什么我得到了一个错误的选择排序列表列表。我将对象Pencilbox存储在两个不同的数组中,其中一个数组必须按价格对数组进行排序。按价格排序没有按预期进行,我尝试了很多方法来解决它,但仍然不起作用。这是我从中提取信息的文本文件-(每个参数如下:铅笔盒的高度、宽度、价格(

12,1,1.49
10,2,2.59
8,1,1.23
3,3,3.33
12,1,1.49
6,2,3.50
10,2,2.59
11
8,2,4.00
7,2,3.00
7,3,1.49
11,2
4,2,2.34
14,2,6.99
10,2,2.59
8,1,2.35

//铅笔盒的高度、宽度、价格

我得到了这个输出:

12, 1, 1.49
7, 3, 1.49
8, 1, 1.23   <=== 1.49 > 1.23 so that's incorrect
12, 1, 1.49
8, 1, 2.35
4, 2, 2.34   <=== this one's the same, 2.34 < 2.35
10, 2, 2.59
10, 2, 2.59
10, 2, 2.59
7, 2, 3.0
3, 3, 3.33
6, 2, 3.5
8, 2, 4.0
14, 2, 6.99

这是我的全部代码:

// 1st java file starts here
import java.util.StringTokenizer;
public class Experiment1 {
public static void main(String[] args) {
String filename = args[0];
System.out.println("Filename = "+filename);
TextFileInput in = new TextFileInput(filename);   //I'm using TextFileInput.java file here to make it read from a file
String line;
StringTokenizer st;
Pencilbox[] pencilbox1 = new Pencilbox[100];    //making two arrays, the 2nd one is to be sorted
Pencilbox[] pencilbox2 = new Pencilbox[100];
int j = 0;
for(int i = 0; i < 100; i++){
line = in.readLine();
if(line == null) {
continue;
}
st = new StringTokenizer(line, ",");
if(st.countTokens() == 3) {     //If there's less than 3 tokens, print it
int height = Integer.parseInt(st.nextToken());
int width = Integer.parseInt(st.nextToken());
double price = Double.parseDouble(st.nextToken());
pencilbox1[i] = new Pencilbox(height, width, price);   //unsorted
pencilbox2[j] = new Pencilbox(height, width, price);   //the one to be sorted
j++;
}
else {
System.out.println("This line doesn't have three tokens: " + line);
}
}
selectionSort(pencilbox2, j);     //calling method selection sort
PencilboxGUI pencilboxgui = new PencilboxGUI("PencilboxGUI");   
pencilboxgui.display(pencilbox1, pencilbox2);     //to display result in a GUI
}   
private static void selectionSort(Pencilbox[] array, int length) {
for (int i = 0; i < length - 1; i++) { 
int indexLowest = i; 
for (int j = i + 1; j < length; j++) { 
if (array[indexLowest].getPrice() > array[j].getPrice()) {
indexLowest = j;
}
Pencilbox temp = array[i];
array[i] = array[indexLowest];
array[indexLowest] = temp;
}
}
}
}
//2nd java file
public class Pencilbox {
private int height;
private int width;
private double price;

public Pencilbox() {
}
public Pencilbox(int height, int width, double price) {
this.height = height;
this.width = width;
this.price = price;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return height + ", " + width + ", " + price;
}
}
//3rd java file
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
class PencilboxGUI extends JFrame {
private JScrollPane scrollPaneEast;
private JScrollPane scrollPaneWest;
private JTextArea pencilbox1TextArea;
private JTextArea pencilbox2TextArea;
public PencilboxGUI() {
}
public PencilboxGUI(String title) {
super(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(100, 100); 
this.setLocation(300, 100);
this.setLayout(new GridLayout(1, 2));
this.pencilbox1TextArea = new JTextArea("Unsorted Pencilboxes: nn");
this.pencilbox2TextArea = new JTextArea("Sorted Pencilboxes by Price: nn");
this.scrollPaneEast = new JScrollPane(this.pencilbox1TextArea);
this.scrollPaneWest = new JScrollPane(this.pencilbox2TextArea);
this.getContentPane().add(this.scrollPaneEast, BorderLayout.EAST);
this.getContentPane().add(this.scrollPaneWest, BorderLayout.WEST);
}
public void display(Pencilbox[] pencilbox1, Pencilbox[] pencilbox2) {
showGui();
String unsortedString = getPencilboxsArrayString(pencilbox1);
String sortedString = getPencilboxsArrayString(pencilbox2);
this.pencilbox1TextArea.append(unsortedString);
this.pencilbox2TextArea.append(sortedString);
this.pack();
}
public void showGui() {
this.setVisible(true);
}
private String getPencilboxsArrayString(Pencilbox[] pencilbox) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pencilbox.length; i++) {
if(pencilbox[i] == null) {
continue;
}
sb.append(pencilbox[i].toString() + "nn");
}
return sb.toString();
}
}

基本上,代码在三个不同的文件中,但我在这里将它们组合在一起。我标记了出现分离的区域。如果格式看起来有问题,我真的很抱歉,我尽力修复了它。我感谢的任何反馈

在selectionSort((方法中,内部forj循环应仅用于查找最低索引,并且应在内部forj循环完成后进行值交换:

for (int i = 0; i < length - 1; i++) { 
int indexLowest = i; 
for (int j = i + 1; j < length; j++) { 
if (array[indexLowest].getPrice() > array[j].getPrice()) {
indexLowest = j;
}
}
Pencilbox temp = array[i];
array[i] = array[indexLowest];
array[indexLowest] = temp;
}

最新更新