Java集合问题

标签 java collections linked-list

好吧,我即将进行一个小型实验室测试,这个主题对我来说相当困难。我设法从已经接受过该主题的人那里获得了一个示例主题,但我在解决它时遇到了问题。主题如下:

Create class Apartment(int nr, String orientation) and have a class LuxuryApartment that inherits from class Apartment and also has an attribute surface of type Float.

我想这非常简单,这里没有问题。

Create class MyLinkedList which extends the class LinkedList so you can store Apartment type objects in it. Add a method called sortByOrientation() which will sort the apartments alphabetically by their orientation, using a comparator defined as a normal inner class.

我在这里遇到了一些麻烦。我可以创建我的类 MyLinkedList,但我不明白方法 sortByOrientation() 应该是什么样子。

Create a test program which initializes a MyLinkedList. Add 3 apartments and 3 LuxuryApartments. Print the list, sort the list using the sortByOrientation() method and print the list using an iterator.

如果他们之前说我应该创建该类以便我只能存储公寓,我如何将 LuxuryApartments 添加到 MyLinkedList?

Move the apartments which aren't luxury into a LinkedList and the luxury ones into a LinkedList. Create a method that lists the apartments and the luxury apartments.

我想这并不难,但是移动是什么意思?我是否可以从上述 MyLinkedList 中获取我的公寓?

有人可以给我一些关于这个练习的指导吗?我真的迷失在这里了。抱歉,如果我的问题很愚蠢/不尊重准则/等等。但我真的不知道如何进行这项练习。

Apartment 类 - 我不确定它是否需要实现 Comparable 接口(interface),正如我所说,我目前对 Java 不太了解

public class Apartament implements Comparable<Apartament>{
    private int nr;
    private String orientare;
    public int getNr() {
        return nr;
    }
    public void setNr(int nr) {
        this.nr = nr;
    }
    public String getOrientare() {
        return orientare;
    }
    public void setOrientare(String orientare) {
        this.orientare = orientare;
    }

    public Apartament(int nr, String orientare){
        this.nr=nr;
        this.orientare=orientare;
    }
    public int compareTo(Apartament ap){
        return ap.orientare.compareTo(this.orientare);
    }
}

我的 LuxuryApartment 类继承 self 的 Apartment 类

public class ApartamentLux extends Apartament{
    private float suprafata;

    public float getSuprafata() {
        return suprafata;
    }

    public void setSuprafata(float suprafata) {
        this.suprafata = suprafata;
    }

    public ApartamentLux(int n, String o, float suprafata){
        super(n,o);
        this.suprafata=suprafata;
    }
}

这就是我真正迷失的地方,似乎无法让它发挥作用。

import java.text.Collator;
import java.util.Comparator;
import java.util.LinkedList;

public class MyLinkedList extends LinkedList<Apartament>{
    public MyLinkedList(){
        super();
    }
    private static class OrComp implements Comparator<String>{
        public int compare(String s1, String s2){
            if(s1.compareTo(s2)<0)
                return 1;
            else return 0;
        }


    public void sortByOrientare(OrComp list){
        list.sort(new Comparator<String>(){
            @Override
            public int compare(String s1, String s2){
                return Collator.getInstance().compare(s1, s2);
            }
        });

        }
    }
}
}

最佳答案

对于您的列表类,您应该从方法中对列表 (this) 进行排序。您创建了一个额外的 Comparator,它在应该返回 -1 时返回 0,并尝试对其进行排序。

您不需要实现 Comparable 接口(interface),因为您这样做,无论如何我相信您应该使用 this.orientare.compareTo(other.orientare) 而不是相反。

但是使用自定义方法和比较器(这似乎是练习所要求的),这是一种更简单的方法来实现这一点。

import java.text.Collator;
import java.util.Comparator;
import java.util.LinkedList;

public class MyLinkedList extends LinkedList<Apartament> {
    public void sortByOrientare(){
        sort(new MyComparator());
    }
    class MyComparator extends Comparator<Apartament> {
        @Override
        public int compare(Apartament s1, Apartament s2){
            return Collator.getInstance().compare(s1.getOrientare(), s2.getOrientare());
        }
    }
}

由于 ApartamentLux 扩展了 Apartament,因此可以将其实例添加到您的自定义列表中。 (因为它们也是 Apartament 的实例)

除了拆分列表之外,您似乎已经解决了其他所有问题。我在这里猜测,但我想他们想要的是这样的:

LinkedList<Apartament> list1 = new LinkedList<>();
LinkedList<Apartament> list2 = new LinkedList<>();
for(Apartament a : myList) {
    if(a instanceof ApartamentLux) {
        list2.add(a);
    } else {
        list1.add(a);
    }
}

然后打印出列表。

关于Java集合问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40164805/

相关文章:

java - 它是多重继承吗?如果不是,为什么?

java - 去除xml中的文本内容

java - 使我的自定义 View 可以由许多类扩展

java - 显示我的数据库中的名称时遇到问题

collections - ArrayList(集合 c)VS HashSet(集合 c)

NHibernate 自定义集合类型

java - 给定 Map 的不同 Collection View 的迭代顺序是否保证一致?

C: 双链表中的pop函数

链表中的c++析构函数

c - C 中指向单链表指针的指针