package com.snail.exam.javaexam;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class App {
public static void main( String[] args ) {
List<USCurrency> prices = new ArrayList<USCurrency>();
prices.add(new USCurrency(1,23));
prices.add(new USCurrency(10,99));
prices.add(new USCurrency(10,0));
prices.add(new USCurrency(5,43));
Collections.sort(prices);
for(USCurrency price : prices) {
System.out.println(price);
}
}
}
package com.snail.exam.javaexam;
public class USCurrency implements Comparable{
private int myDollar;
private int myCent;
public USCurrency(int dollar,int cent) {
myDollar = dollar;
myCent = cent;
}
public int getCent() {
return myCent;
}
public int getDollar() {
return myDollar;
}
@Override
public int compareTo(Object o){
USCurrency cmp = (USCurrency)o;
if (this.getDollar() == cmp.getDollar()) {
return this.getCent() - cmp.getCent();
}
return this.getDollar() - cmp.getDollar();
}
@Override
public String toString(){
return myDollar + "." + myCent;
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.snail.exam</groupId>
<artifactId>JavaExam</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>JavaExam</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
com/snail/exam/javaexam/App.java:[21,24] 警告:[unchecked] 無検査メソッド呼び出 し: java.util.Collections の <T>sort(java.util.List<T>) は (java.util.List<com.snail.exam.javaexam.USCurrency>) に適用されます。java.util.Collections でもちゃんと Generics を使えと言うことか
package com.snail.exam.javaexam;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class App {
public static void main( String[] args ) {
List<USCurrency> prices = new ArrayList<USCurrency>();
prices.add(new USCurrency(1,23));
prices.add(new USCurrency(10,99));
prices.add(new USCurrency(10,0));
prices.add(new USCurrency(5,43));
// FIXED.
Collections.<USCurrency>sort(prices);
for(USCurrency price : prices) {
System.out.println(price);
}
}
}
package com.snail.exam.javaexam;
// FIXED.
public class USCurrency implements Comparable<USCurrency>{
private int myDollar;
private int myCent;
public USCurrency(int dollar,int cent) {
myDollar = dollar;
myCent = cent;
}
public int getCent() {
return myCent;
}
public int getDollar() {
return myDollar;
}
@Override
// FIXED.
public int compareTo(USCurrency o){
if (this.getDollar() == o.getDollar()) {
return this.getCent() - o.getCent();
}
return this.getDollar() - o.getDollar();
}
@Override
public String toString(){
return myDollar + "." + myCent;
}
}