Java做acm所需要的基础知识。
以前做acm的题都是用C/C++来写代码的,在学习完Java之后突然感觉Java中的方法比C/C++丰富很多,所以就整理一下平时做题需要用到的Java基础知识。
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知识的其他文章: