Java 使用映射对三个字符串数组进行排序
我可以使用下面的代码同时对两个数组进行排序,但现在我需要添加一个新的数组。所以我需要同时对3个数组进行排序。iceStringArray包含单词,engStringArray包含它们的翻译,soundStringArray包含它们的音频文件标题。我试图按照字母顺序对iceStringArray和其他两个数组进行排序。这段代码可以完美地处理两个数组,但我很难找到添加新数组的方法Java 使用映射对三个字符串数组进行排序,java,android,Java,Android,我可以使用下面的代码同时对两个数组进行排序,但现在我需要添加一个新的数组。所以我需要同时对3个数组进行排序。iceStringArray包含单词,engStringArray包含它们的翻译,soundStringArray包含它们的音频文件标题。我试图按照字母顺序对iceStringArray和其他两个数组进行排序。这段代码可以完美地处理两个数组,但我很难找到添加新数组的方法 Map<String, String> myLocalMap = new HashMap&l
Map<String, String> myLocalMap = new HashMap<>();
for (int indx = 0; indx < iceStringArray.length; indx++) {
myLocalMap.put(iceStringArray[indx], engStringArray[indx]);
}
Arrays.sort(iceStringArray);
for (int indx = 0; indx < iceStringArray.length; indx++) {
engStringArray[indx] = myLocalMap.get(iceStringArray[indx]);
}
// soundStringList is the new array I want to add
lAdapter = new ListAdapter(TrainingActivity.this, iceStringArray, engStringArray, soundStringList);
lView.setAdapter(lAdapter);
Map myLocalMap=newhashmap();
对于(int indx=0;indx
这显然是一对一的映射
所以我们可以有一个封装英语翻译和声音数据的类
class Data{
String translation;
String sound;
//have getter setters for these
}
现在您可以拥有这样一个数组,使用
iceStringArray
进行迭代,这显然是一对一映射的情况
所以我们可以有一个封装英语翻译和声音数据的类
class Data{
String translation;
String sound;
//have getter setters for these
}
现在您可以使用iceStringArray
使用映射对三个字符串数组进行排序
您从两个阵列开始,现在有三个阵列。明天,你可能需要第三个、第四个等等,每次你都可能会遇到同样的问题。与其引入第三个数组,我建议您创建一个新类型,例如
class MyType {
String iceString;
String engString;
String soundString;
public MyType(String iceString, String engString, String soundString) {
this.iceString = iceString;
this.engString = engString;
this.soundString = soundString;
}
// getters, setters, and other methods
}
然后你像这样使用它
MyType[] objects = new MyType[the-required-size];
objects[0] = new MyType("iceString1", "engString1", "soundString1");
objects[1] = new MyType("iceString2", "engString2", "soundString2");
// ...more objects
Arrays.sort(objects);
使用映射对三个字符串数组进行排序
您从两个阵列开始,现在有三个阵列。明天,你可能需要第三个、第四个等等,每次你都可能会遇到同样的问题。与其引入第三个数组,我建议您创建一个新类型,例如
class MyType {
String iceString;
String engString;
String soundString;
public MyType(String iceString, String engString, String soundString) {
this.iceString = iceString;
this.engString = engString;
this.soundString = soundString;
}
// getters, setters, and other methods
}
然后你像这样使用它
MyType[] objects = new MyType[the-required-size];
objects[0] = new MyType("iceString1", "engString1", "soundString1");
objects[1] = new MyType("iceString2", "engString2", "soundString2");
// ...more objects
Arrays.sort(objects);
您可以在排序之前存储
iceStringArray
值到其索引的映射,然后使用索引对齐其他数组
Map<String, Integer> myLocalMap = new HashMap<>();
for (int indx = 0; indx < iceStringArray.length; indx++) {
myLocalMap.put(iceStringArray[indx], indx);
}
Map myLocalMap=newhashmap();
对于(int indx=0;indx
然后在排序之后:
// Create new temp arrays
String[] sortedEngStringArray = new String[iceStringArray.length];
String[] sortedSoundStringArray = new String[iceStringArray.length];
for (int indx = 0; indx < iceStringArray.length; indx++) {
sortedEngStringArray[indx] = engStringArray[myLocalMap.get(iceStringArray[indx])];
sortedSoundStringArray[indx] = soundStringArray[myLocalMap.get(iceStringArray[indx])];
}
//创建新的临时数组
String[]sortedEngStringArray=新字符串[iceStringArray.length];
String[]sortedSoundStringArray=新字符串[iceStringArray.length];
对于(int indx=0;indx
您可以在排序之前存储iceStringArray
值到其索引的映射,然后使用这些索引对齐其他数组
Map<String, Integer> myLocalMap = new HashMap<>();
for (int indx = 0; indx < iceStringArray.length; indx++) {
myLocalMap.put(iceStringArray[indx], indx);
}
Map myLocalMap=newhashmap();
对于(int indx=0;indx
然后在排序之后:
// Create new temp arrays
String[] sortedEngStringArray = new String[iceStringArray.length];
String[] sortedSoundStringArray = new String[iceStringArray.length];
for (int indx = 0; indx < iceStringArray.length; indx++) {
sortedEngStringArray[indx] = engStringArray[myLocalMap.get(iceStringArray[indx])];
sortedSoundStringArray[indx] = soundStringArray[myLocalMap.get(iceStringArray[indx])];
}
//创建新的临时数组
String[]sortedEngStringArray=新字符串[iceStringArray.length];
String[]sortedSoundStringArray=新字符串[iceStringArray.length];
对于(int indx=0;indx
不要并行排序数组,请使用对象。不要并行排序数组,请使用对象。对不起,我不完全了解如何添加第三个数组。当你对第三个数组说//相同时,你是什么意思,因为我得到的最接近的结果是添加engStringArray[indx]=engStringArray[myLocalMap.get(iceStringArray[indx]);there@CroquetaFregidaAmbPastanagaAddedI不完全了解如何添加第三个数组,抱歉。当你对第三个数组说//相同时,你是什么意思,因为我得到的最接近的结果是添加engStringArray[indx]=engStringArray[myLocalMap.get(iceStringArray[indx]);there@CroquetaFregidaAmbPastanaga补充