Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

arraylist - How to link two lists in java?

I am trying to create a kind of highscore system in a program. I have a list of scores and then a list of names. I want to add the scores and names to the end of the ArrayList and then sort the list of scores using collections. The problem is that I need to sort the list of names in the exact same way.

Start [1, 3, 2] ["name 1" , "name 2" , "name 3"]

Sorted [3, 2, 1] ["name 2" , "name 3" , "name 1"]

Like that.

question from:https://stackoverflow.com/questions/65891032/how-to-link-two-lists-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could map each of the names to a rank as an int, then sort the map by value

import Java.util.HashMap;

HashMap<String, Integer> scoresMap = new HashMap<String, Integer>();

//add a value into a map like this
scoresMap.put("name 1", 3);

//then make a function to sort the map
HashMap<String, Integer> sortedScoresMap;
sortedScoresMap = sortMapByValue(scoresMap);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...