gpt4 book ai didi

java - 从文件中读取并选择按价格对存储的对象数组进行排序不起作用

转载 作者:行者123 更新时间:2023-12-01 22:26:31 25 4
gpt4 key购买 nike

我真的被困在这里了,我不知道为什么我得到了错误的选择排序列表。我将 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

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

And I'm getting this output:

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

And this is my whole code:

// 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: \n\n");
this.pencilbox2TextArea = new JTextArea("Sorted Pencilboxes by Price: \n\n");

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() + "\n\n");
}

return sb.toString();
}
}

Basically, the codes were in three different files but I combined them here. I marked the areas where separation occurs. I'm really sorry if the formatting looks wrong, I tried my best to fix it. I appreciate any feedback

最佳答案

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

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;
}

关于java - 从文件中读取并选择按价格对存储的对象数组进行排序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58565602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com