# Java做acm所需要的基础知识之排序问题

1116人阅读 评论(0)

Java做acm所需要的基础知识。

## 1、Arrays.sort()

### （1）数字排序

int[] num =newint[]{4,2,3,5,-8,5,2,3};

Arrays.sort(num);

for(int i = 0; i < 8;i++)

System.out.print(num[i]+”,”);

输出结果：-8,2,2,3,3,4,5,5,

### （2）字符串排序（先大写后小写原则）

String[] s = new String[]{"Z","a","A","z"};

Arrays.sort(s);

for(int i = 0; i < 4;i++)

System.out.print(s[i] +",");

输出结果：A,Z,a,z,

### （3）严格按照字母表排序（即忽略大小写）

String[] s = new String[]{"Z","a","A","z"};

Arrays.sort(s,String.CASE_INSENSITIVE_ORDER);

for(int i = 0; i < 4;i++)

System.out.print(s[i] +",");

输出结果：a,A,Z,z,

### （4）忽略大小写反向排序

String[] s = new String[]{"Z","a","A","z"};

Arrays.sort(s,String.CASE_INSENSITIVE_ORDER);

Collections.reverse(Arrays.asList(s));

for(int i = 0; i < 4;i++)

System.out.print(s[i] +",");

输出结果：z,Z,A,a,

### （5）对象的排序（类似于C中的结构体的排序）

 import java.util.Arrays;
import java.util.Comparator;
public class Main
{
public static void main(String[] args)
{
Dogd1 = new Dog("dog1",1);
Dogd2 = new Dog("dog2",2);
Dogd3 = new Dog("dog3",3);
Dogd4 = new Dog("dog4",4);
Dogd5 = new Dog("dog5",5);
Dog[]dogs = new Dog[] {d1,d2,d3,d4,d5};
Arrays.sort(dogs,new ByWeightComparator());
for(int i = 0; i <dogs.length; i++)
{
Dog dog = dogs[i];
System.out.print(dog.getName()+",");
}
}
}
class Dog //定义一个“狗类”
{
private String name;
private int weight;
public Dog(String name, int weight)
{
this.setName(name);
this.weight = weight;
}
public int getWeight()
{
return weight;
}
public void setWeight(int weight)
{this.weight = weight;}
public void setName(Stringname)
{this.name = name;}
public String getName()
{return name;}
}
class ByWeightComparator implements Comparator
{
public final int compare(Object pFirst, Object pSecond)
{
int aFirstWeight = ((Dog)pFirst).getWeight();
int aSecondWeight = ((Dog)pSecond).getWeight();
int diff = aFirstWeight - aSecondWeight;
if(diff >= 0)
return -1;
else if(diff < 0)
return 1;
return 0;
}
}

输出结果：dog5,dog4,dog3,dog2,dog1,

http://www.360doc.com/content/12/0619/15/7823806_219215040.shtml

http://blog.csdn.net/q3498233/article/details/5776028

Java做acm所需要的基础知识之排序问题

2
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：62329次
• 积分：1191
• 等级：
• 排名：千里之外
• 原创：55篇
• 转载：14篇
• 译文：0篇
• 评论：3条
阅读排行