0

这是主程序

public class MainPerson {

public MainPerson(){}

public static void StartMainPerson()
{
    int i = 1;
    ArrayList<SortedPair> PersonList;
    ArrayList<Person> InitializedPersonList;        
    final int NUMBER_OF_PEOPLE = 6;

    PersonList = new ArrayList<>();
    InitializedPersonList = new ArrayList<>();

    InitializedPersonList.add(new Person( "Assaf Malki" , 1984));
    InitializedPersonList.add(new Person( "Assaf Malki" , 1984));
    InitializedPersonList.add(new Person( "Breanna Romberger" , 1920));
    InitializedPersonList.add(new Person( "Latrice Lane" , 1930));
    InitializedPersonList.add(new Person( "Bridgett Couch" , 1940));
    InitializedPersonList.add(new Person( "Roxane Sova" , 1950));
    InitializedPersonList.add(new Person( "Donnette Weston" , 1960));
    InitializedPersonList.add(new Person( "Milton Kopecky" , 1970));
    InitializedPersonList.add(new Person( "Rosella Vandoren" , 1980));
    InitializedPersonList.add(new Person( "Kayleigh Willimas" , 1990));
    InitializedPersonList.add(new Person( "Zula Lashua" , 2000));
    InitializedPersonList.add(new Person( "Ralph Baudoin" , 2010));
    InitializedPersonList.add(new Person( "Kenton Tseng" , 2005));
    InitializedPersonList.add(new Person( "Brendon Gilchrest" , 1995));

    Iterator itr = InitializedPersonList.iterator();

    while(itr.hasNext())
    {
        System.out.print("Pair " + i + " is " + itr.next() + " and " + itr.next() + ".\n");
        i = i + 1;
    }

    i = 0;
    System.out.print("\n");
    Iterator itr1 = InitializedPersonList.iterator();

    while(i < NUMBER_OF_PEOPLE)
    {

        try{
            System.out.println("Adding pair number " + (i+1) + "..");
            PersonList.add(new SortedPair((Person) itr1.next(), (Person) itr1.next()));
        }
        catch (IllegalPair e)
        {
            System.out.println("Invalid input, same person name, try again.");
            continue;
        }

        i = i + 1;

    }

    System.out.println("");

    for (SortedPair Value : PersonList)
    {
        System.out.println(Value);
    }
}

}

这是排序对

public class SortedPair <E extends Comparable<E>>
{
    E Max_Element;
    E Min_Element;

    public SortedPair(E FirstElement, E SecondElemnt) throws IllegalPair
    {
        int Compare_Result = FirstElement.compareTo(SecondElemnt);

        if (Compare_Result == 0)
        {
            Max_Element = null;
            Min_Element = null;
            throw new IllegalPair(FirstElement.toString(), SecondElemnt.toString());
        }
        else if (Compare_Result > 0)
        {
            Max_Element = FirstElement;
            Min_Element = SecondElemnt;
        }
        else 
        {
            Max_Element = SecondElemnt;
            Min_Element = FirstElement;

        }
    }

    public E getFirst()
    {
        return Max_Element;
    }

    public E getSecond()
    {
        return Min_Element;
    }

    @Override
    public String toString()
    {
        return String.format("%s is bigger then %s.", getFirst(), getSecond());
    }

}

我收到以下警告

MainPerson.java:60: warning: [unchecked] unchecked call to SortedPair(E,E) as a member of the raw type SortedPair
                PersonList.add(new SortedPair((Person) itr1.next(), (Person) itr1.next()));
                               ^
  where E is a type-variable:
    E extends Comparable<E> declared in class SortedPair

而且我不明白。

我尝试更改ArrayList<Person> InitializedPersonList;为,ArrayList<Comperable> InitializedPersonList;但没有成功。

有什么想法吗?

我认为这个问题很清楚,我可以添加多少文本 Common..

4

2 回答 2

2

你需要的是

List<SortedPair<Person>> PersonList = new ArrayList<SortedPair<Person>>();
List<Person> InitializedPersonList = new ArrayList<Person>();
于 2013-05-17T14:50:48.430 回答
0

请使用正确的 Java 格式。变量从不以大写字母开头。这使得其他 Java 开发人员阅读起来非常令人不安。

顺便说一句:我认为 Marco 是对的。

于 2013-05-17T14:57:01.360 回答