1 - “A”
5 - “E”
3 - “D”
2 - “B”
3 - “C”比如有以上两个数组,一个为数字数组(可排序),另一个为字符数组,它们一一对应,我想对数字数组进行排序后1,2,3,3,5,输出对应的字符数组A,B,C,D,E,我使用了Hashmap,但是输出时将数字数组中的第二个3对应的字符D输出为C,请教一下??
不知道用什么办法解决???
5 - “E”
3 - “D”
2 - “B”
3 - “C”比如有以上两个数组,一个为数字数组(可排序),另一个为字符数组,它们一一对应,我想对数字数组进行排序后1,2,3,3,5,输出对应的字符数组A,B,C,D,E,我使用了Hashmap,但是输出时将数字数组中的第二个3对应的字符D输出为C,请教一下??
不知道用什么办法解决???
将两个数组的内容依次作为作为参数生成对象A的实例插入对象A的数组中,使用Arrays的sort方法进行比较。比较麻烦!不过可以实现!
public static void sort(char[] a)
public static void sort(int [] a)
也就是你可以直接用Arrays.sort(你的那些Array)
好抽象哦,能再具体一点吗?
自己写个类,两个域分别是两个数组的一对pair,实现comparable接口,定义compareto方法,按照第一个数组,也就是这里的int域进行比较就可以了,然后可以存在数组里,也可以存在容器里,分别用Arrays.sort和Collections.sort进行排序就好了
你把要查找的数组要先进行排序,后用Arrays.binarySearch会返回你所要的数组下标
先用1,2,3,4,5...排序
如果相同,则用a,b,c,d,e...排序
其实这种情况经常遇到.
下面给出源代码:1:Person.java文件:-------------------------------
public class Person{
String name;
int age;
public Person(String name,int age){
this.name = name;
this.age = age;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}}
2:Mycomparator.java-------------------------------
//实现Comparator接口,也就是定义排序规则,你几乎可以定义任何规则
package com.infoearth;
import java.util.*;
public class Mycomparator implements Comparator{ public int compare(Object o1,Object o2) {
Person p1=(Person)o1;
Person p2=(Person)o2;
if(p1.age<p2.age)
return 1;
else
return 0;
}}3:ListSort.java------------------------------------package com.infoearth;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;public class ListSort {
public static void main(String[] args){
ArrayList list = new ArrayList();
list.add(new Person("lcl",28));
list.add(new Person("fx",23));
list.add(new Person("wqx",29));
Comparator comp = new Mycomparator();
Collections.sort(list,comp);
for(int i = 0;i<list.size();i++){
Person p = (Person)list.get(i);
System.out.println(p.getName());
}
}}
numList.addAll(Arrays.asList(new Object[]{new Integer(3),new Integer(3),new Integer(5),new Integer(4)}));
List valueList=new ArrayList();
valueList.addAll(Arrays.asList(new Object[]{"A","B","C","D"}));
// build a HashMap to maintain the relationship;
用IdentityHashMap就可以了
Map map=new IdentityHashMap();
for(int i=0;i<numList.size();i++){
map.put(numList.get(i),valueList.get(i));
}
// sort the numList
Collections.sort(numList);
// output the value sorted by key.
for(int i=0;i<numList.size();i++){
System.out.println(map.get(numList.get(i)));
}
利用TreeMap也是行不通的,如果Comparator和equals不符合,处理起来就有错误!
将两个数组的内容依次作为作为参数生成对象A的实例插入对象A的数组中,使用Arrays的sort方法进行比较。
import java.util.*;
public class ListStor{
public static void main(String args[])
{
Employee [] staff=new Employee[5];
staff[0]=new Employee(1,"A");
staff[1]=new Employee(5,"E");
staff[2]=new Employee(4,"D");
staff[3]=new Employee(2,"B");
staff[4]=new Employee(3,"C");
Arrays.sort(staff);
for(int i=0;i<staff.length;i++)
{
Employee e=staff[i];
System.out.println((int)e.getNum()+"-"+e.getStr());
}
}
}class Employee implements Comparable
{
Employee(int n,String s)
{
num=n;
str=s;
}
public double getNum()
{
return num;
}
public String getStr()
{
return str;
}
public int compareTo(Object otherObject)
{
Employee other=(Employee)otherObject;
if(num>other.num) return 1;
else return 0;
}
private String str;
private double num;
}