根据长数组

时间:2016-09-12 07:42:43

标签: java arrays

基本上,我正在尝试进行高分显示,比较名称和时间(更快更好)。我采用数组,并根据值(从最小到最大)对它们进行排序。然后,我希望根据时间重新安排名称,这是我无法做到的。

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

  

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

输出为:

  

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

    }   
} 

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分: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))); 
  }
}

答案 2 :(得分:-2)

一个未经优化的简单解决方案是重新排列数组(交换名称和值位置),​​然后对其进行排序(数组,排序)

    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