gpt4 book ai didi

java - 根据长数组的顺序更改字符串数组的顺序

转载 作者:行者123 更新时间:2023-11-30 07:09:31 25 4
gpt4 key购买 nike

基本上,我正在尝试进行高分显示,比较名称和时间(越快越好)。我获取时间数组,并根据值(从小到大)对它们进行排序。然后我希望根据时间重新排列名称,但我在这方面遇到了麻烦。

所以基本上,我希望输入是这样的:

John--8
Sally--5
James--2
Fred--4

输出为:

James--2
Fred--4
Sally--5
John--8

目前,输出为:

John--2
Sally--4
James--5
Fred--8

这是我的代码(注意:您需要 these text documents ):

//not actually sure which of these import statements are really necessary, lol
import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.*;
import java.util.Vector;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Canvas;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.io.*;

import java.util.Arrays;

class HallOfFame extends JFrame implements ActionListener
{

private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 350;

int numberOfRows=20;

String[] name = new String[100];
String[] time = new String[100];

//long[] longArrayTime = new long[100];

String[] saneTime = new String[100];

/*it's been a */long[] longTime = new long[100];

long second;
long minute;
long hour;
long miliseconds;
//Declare your objects here...

public static void main(String[] args)
{
HallOfFame frame = new HallOfFame();
frame.setVisible(true); // Display the frame
}

@SuppressWarnings("unchecked") //without this line, a warning appears. i can find no need for it and it's annoying, so i got rid of it :P (It is in relation to the table)
public HallOfFame( )
{

try
{
File inFile= new File("names.txt");
FileReader fileReader= new FileReader(inFile);
BufferedReader bufReader = new BufferedReader(fileReader);

for(int i=0; i<100; i++)
{
name[i]=bufReader.readLine();

}
bufReader.close();

}
catch(IOException tada)
{
JOptionPane.showMessageDialog(null, "Error loading high scores. Please ensure file system is accesible, and that 'name.txt' exists");
}

try
{
File inFile= new File("times.txt");
FileReader fileReader= new FileReader(inFile);
BufferedReader bufReader = new BufferedReader(fileReader);

for(int i=0; i<100; i++)
{
time[i]=bufReader.readLine();

}
bufReader.close();

}
catch(IOException tada)
{
JOptionPane.showMessageDialog(null, "Error loading high scores. Please ensure file system is accesible, and that 'name.txt' exists");
}

//finished reading scores
for(int i=0;i<100;i++)
{
if(time[i]==null||time[i].equals(("0")))
{
time[i]="3699989"; //a very large value so that non-existant values are at the end of a sorted array
}

longTime[i] = Long.parseLong(time[i]);
}
Arrays.sort(longTime);


for(int i=0;i<100;i++)
{
second = (longTime[i] / 1000) % 60;
minute = (longTime[i] / (1000 * 60)) % 60;
hour = (longTime[i] / (1000 * 60 * 60)) % 24;
miliseconds = Long.parseLong((""+longTime[i]).substring(0,2));

saneTime[i] = String.format("%02d:%02d:%d", minute, second, miliseconds);
}

// set the frame properties
setTitle("High Scores");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLocationRelativeTo(null);
setResizable(false);
Image icon = new ImageIcon("trophy.png").getImage(); //why does this not work????
setIconImage(icon);

Object columns[] = { "Rank", "Time", "Name" };
DefaultTableModel model = new DefaultTableModel(columns, 0);

for(int i=0;i<numberOfRows;i++)
{
if(time[i]=="3699989")//stop adding scores when array is empty (this large number is "empty" as 0 is defined to it above to make sorting the array work)
{
break;
}
Vector row = new Vector(numberOfRows);
row.add((i+1));
//row.add(time[i]);
row.add(saneTime[i]);
row.add(name[i]);
model.addRow( row );
}

// set the content pane properties
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout());
contentPane.setBackground(Color.white);

JTable table = new JTable( model );
JScrollPane scrollPane = new JScrollPane(table);
contentPane.add(scrollPane, BorderLayout.CENTER);

for(int i=0;i<numberOfRows;i++)
{
table.setRowHeight(i, 30);
}

//create and place items frame's content pane

// register 'Exit' upon closing as default close operation
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent event) // Actions
{
JButton clickedButton = (JButton) event.getSource();

}
}

最佳答案

创建一个具有名称和时间作为属性的类

public class Highscore {
private String name;
private long time;
public Highscore(String name, long time) {
this.name = name;
this.time = time;
}
public String getName() {
return this.name;
}
public long getTime() {
return this.time;
}
}

然后将所有 Highscore 对象放入一个集合中。例如:ArrayList 并使用 Comparator 对这些对象进行排序。

Collections.sort(highscores, new Comparator<Highscore>() {
@Override
public int compare(Highscore hs1, Highscore hs2) {
return Long.compare(hs1.getTime(), hs2.getTime());
}
});

或者按照 @Andreas 的建议,如果您使用的是 Java 8,则可以使用缩短的表达式对 ArrayList 进行排序:

highscores.sort(Comparator.comparing(Highscore::getTime))

关于java - 根据长数组的顺序更改字符串数组的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39445497/

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