2016-09-12 34 views
1

基本上,我试图做一个高分显示,比较名字和时间(越快越好)。我花了一些时间,并根据值(从最小到最大)对它们进行排序。然后我想要根据时间重新安排名字,这是我遇到的麻烦。根据长阵的顺序改变字符串数组的顺序

所以基本上,我想要的输入是这样的:

约翰 - 8
莎莉 - 5
詹姆斯 - 2
弗雷德 - 4

而输出为:

詹姆斯 - 2
佛瑞德 - 4
萨利 - 5
约翰 - 8

目前,输出为:

约翰 - 2
莎莉 - 4
詹姆斯 - 5
弗雷德 - 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();   

    } 
} 
+4

基本上,为作业使用正确的数据类型:1)创建一个类来捕获名称/时间/ etc; 2)不要使用字符串来表示时间 - 理想情况下使用java.time中的某些东西,但可能使用整数毫秒或其他值。然后,您可以轻松地对所有数组或项目集合进行排序。 –

+4

请勿使用并行阵列/列表。 Java是一种面向对象的语言。 *使用它!*用你的值创建一个类,并有一个数组/该类的对象列表。现在,你可以分类到你的内心,价值观将永远保持在一起。 – Andreas

+0

@JonSkeet我使用了一个以毫秒为单位的long值来表示时间。它只需要首先从文件中读取字符串,然后将其转换为long。另一个课程也将捕获名称和时间,它只是没有显示在这里,因为其他课程将它保存到这个文件 – Blaine

回答

1

正如许多人已经提到,你应该尝试使用另一种数据结构。我会尽量保持你的逻辑,并在需要的地方做一些小的改进。

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

import javax.swing.JTable; 
import javax.swing.table.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

import javax.swing.JFrame; 

import java.io.*; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Vector; 
import java.util.concurrent.TimeUnit; 

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); removed 

    /* removed and replaced with a method which returns HH:mm:ss for given milliseconds 
    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); 
    }*/ 

    class Player implements Comparable<Player>{  //added a class player with the atr. name and time 
     String name; 
     Long time; 

     Player(String name, Long time){ 
      this.name = name; 
      this.time = time; 
     } 
     @Override 
     public int compareTo(Player p) {  // a compareTo method which is needed to sort a list of players 
      return time.compareTo(p.time);  // sorting by time, you can change this to sort by name age or sth.else 
     } 
    } 

    ArrayList<Player> playerslist = new ArrayList<>(); // an ArrayList to hold your data 
    for(int i=0;i<100;i++){ 
     Player p = new Player(name[i],longTime[i]); 
     playerslist.add(p); 
    } 
    Collections.sort(playerslist); 

    // 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(playerslist.get(i).time >1000000)//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(convertSecondsToHMmSs(playerslist.get(i).time)); 
      row.add(playerslist.get(i).name); 
      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 static String convertSecondsToHMmSs(long millis) {  // method to change milliseconds to HH:mm:ss 
    return String.format("%02d:%02d:%02d", 
    TimeUnit.MILLISECONDS.toHours(millis), 
    TimeUnit.MILLISECONDS.toMinutes(millis) - 
    TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), 
    TimeUnit.MILLISECONDS.toSeconds(millis) - 
    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); 
    } 
} 
+0

谢谢!这是我正在寻找的例子。说明如何在整个程序中实际使用它的人 – Blaine

2

创建一个有na我和时间作为属性

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)) 
+2

或'highscores.sort(Comparator.comparing(Highscore :: getTime))'在Java 8中。 – Andreas

+2

要比较两个'long'值,请使用['Long.compare hs1.getTime(),hs2.getTime())'](https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html#compare-long-long-)。当然,你需要一个getter方法才能工作。 – Andreas

+0

我怀疑这可以解决问题,但我真的不知道如何使用它 – Blaine

-2

一个简单的解决方案,这是不optimzed,将而重新排列的阵列(交换名称和值的位置),然后对它进行排序(阵列,排序)

public class SortArray { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     String [] arr = new String[4]; 

     arr[0] = "John--8"; 
     arr[1] = "Sally--5"; 
     arr[2] = "James--2"; 
     arr[3] = "Fred--4"; 
     sortArr(arr); 
     for(int i = 0; i < arr.length; ++i){ 
      String [] splitArr = arr[i].split("--"); 
      System.out.println(splitArr[1] +"--"+splitArr[0]); 
     } 
    } 
    public static void sortArr(String [] arr) { 

     int len = arr.length; 

     for(int i = 0; i < len; ++i) { 
      String [] splitArr = arr[i].split("--"); 

      arr[i] = splitArr[1] +"--" +splitArr[0]; 

     } 

     Arrays.sort(arr); 


    } 

} 

输出是

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

这是一个不好的解决方案,IMO。使用适当的对象方向远远优于将所有东西都放入字符串中。 –

+0

实际上,此刻,我推崇一个不太合适的解决方案,我实际上对我的有限知识 – Blaine

+0

仍然了解,问题在于这两个值都在同一个变量中 – Blaine