高分java与arraylist

时间:2016-10-06 12:15:20

标签: java arrays sorting arraylist

我正在使用ArrayLists和void方法编写以下高分游戏。我遇到了所需的输出问题,它应该类似于以下内容:

Top Scorers:
Name1: 600
Name2: 400
Name3: 300
Name4: 200
Name5: 100

我遇到问题的复杂部分是在分数按降序排序后,与输入分数匹配的输入名称。以下是我到目前为止的代码:

import java.util.Scanner;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;



public class high_scores {

//Write a program that records high-score data for a fictitious game. 
    //The program will ask the user to enter five names, and five scores. 
    //It will store the data in memory, and print it back out sorted by score.





    //array lists declared and initialized in main method; invoke other three methods
    public static void main(String args []) 
    {

        //creating an arraylist with names
        ArrayList<String> names = new ArrayList<String>();

        //creating an arraylist with scores
        ArrayList<Integer> scores = new ArrayList<Integer>();


        //invoke other three methods
        initialize(names, scores);
        sort(names, scores);
        display(names, scores);

    }



    //user input of five names and five scores; 
    public static void initialize(ArrayList<String> names, ArrayList<Integer> scores)
    {

        //for user input
        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter the name for score # 1: ");
        names.add(keyboard.next());
        System.out.print("Enter the score for score # 1: ");
        scores.add(keyboard.nextInt());

        System.out.print("Enter the name for score # 2: ");
        names.add(keyboard.next());
        System.out.print("Enter the score for score # 2: ");
        scores.add(keyboard.nextInt());

        System.out.print("Enter the name for score # 3: ");
        names.add(keyboard.next());
        System.out.print("Enter the score for score # 3: ");
        scores.add(keyboard.nextInt());

        System.out.print("Enter the name for score # 4: ");
        names.add(keyboard.next());
        System.out.print("Enter the score for score # 4: ");
        scores.add(keyboard.nextInt());

        System.out.print("Enter the name for score # 5: ");
        names.add(keyboard.next());
        System.out.print("Enter the score for score # 5: ");
        scores.add(keyboard.nextInt());


    }




    //function sorts both array lists based on values in scores array list
    public static void sort(ArrayList<String> names, ArrayList<Integer> scores)
    {


            String array_names[] =  new String[names.size()];
            for(int x = 0; x < names.size(); x++) {
                array_names[x] = names.get(x);
            } 


            Integer array_scores[] = new Integer[scores.size()];
            for(int y = 0; y < scores.size(); y++) {
                array_scores[y] = scores.get(y);
            }


    }


    //method that displays the contents of the two arraylists
    public static void display(ArrayList<String> names, ArrayList<Integer> scores)
    {   



        String array_names[] =  new String[names.size()];
        for(int x = 0; x < names.size(); x++) {
            array_names[x] = names.get(x);      
        } 


        Integer array_scores[] = new Integer[scores.size()];
        for(int y = 0; y < scores.size(); y++) {
            array_scores[y] = scores.get(y);

        }


            Arrays.sort(array_scores);


        System.out.println(array_names[4] + " : " + array_scores[4]);
        System.out.println(array_names[3] + " : " + array_scores[3]);
        System.out.println(array_names[2] + " : " + array_scores[2]);
        System.out.println(array_names[1] + " : " + array_scores[1]);
        System.out.println(array_names[0] + " : " + array_scores[0]);


    }
}

根据我们目前的水平,有些事情我可能不应该包括,例如compareTo, Collections, or Array.sort Descending.不确定......给定的方法无法改变。到目前为止,我只是理解了第一个&#34; void initialize"方法和"void display"方法。 "void sort"方法令人困惑,这就是为什么&#34; sort&#34;和&#34;显示&#34;很相似。这是我到目前为止所采取的步骤:

1.) Initialize method: Array List user input
2.) Sort method: Converts Array List to Array; grabs Array List data
3.) Display method: Similar to sort; still working on display

我的"void display"方法是我遇到的最麻烦的地方。我已设法按降序打印输入的分数,但我似乎无法匹配array_names的索引和array_scores AFTER array_scores已排序。任何帮助都将非常有帮助。

2 个答案:

答案 0 :(得分:0)

public class Score {
     private String name = null;
     private int score = 0;

     public Score(String name, int score) {
         this.name = name;
         this.score = score;
     }
}

然后您可以将此类用于收集

List<Score> allScores = new ArrayList<Score>();

并按以下方式添加您的输入:

allScores.add(new Score("Name1",600));

您可以使用自定义比较器对此列表进行排序。

希望这有帮助。

答案 1 :(得分:0)

如果你不能使用比较器,你需要有一些解决方法。 首先,您需要一个带有键的地图,其中包含名称的分数和值列表

  Map<Integer,List<String>> map = new HashMap<Integer,List<String>>;

在排序之前填写此地图

      for(int i=0;i<scores;i++) {

            if(map.get(scores.get(i)) == null)
                 map.put(scores.get(i),new ArrayList<String>());
           map.get(scores.get(i)).add(names.get(i));
       }

对分数列表进行排序后,您只需要打印每个条目的映射列表

     for(int i=0;i<scores;i++)
          printList(map.get(scores.get(i));