抽象类扩展器数组的Array.sort

问题描述 投票:0回答:3

i 类 Shape 具有一个私有字段 shapeColor,类 Triangle 、 Rectangle 和 Circle 扩展了 Shape 并拥有自己的其他字段。 我写了Shape的比较方法。 (我需要按颜色名称对它们进行排序)

public int compare (Shape obj) {
        return (this.getShapeColor().compareTo(obj.getShapeColor()));
    }

在 psvm 的主类中我有下一个代码:

Shape[] arr3 = new Shape[10];
arr3[6] = new Rectangle("RED", 11, 22);
arr3[1] = new Rectangle("GREEN", 12, 23);
arr3[7] = new Rectangle("BLACK", 13, 24);
arr3[3] = new Rectangle("YELLOW", 14, 25);
arr3[4] = new Rectangle("WHITE", 15, 26);
arr3[5] = new Circle("BLUE", 4);
arr3[0] = new Circle("BROWN", 5);
arr3[2] = new Triangle("BROWN", 5, 3, 4);
arr3[8] = new Triangle("ROYAL BLUE", 5, 7, 8);
arr3[9] = new Triangle("CHILD'S SURPRISE", 6, 4, 6);

        Arrays.sort(arr3, Shape::compare);

问题是 Shape 类在我的任务中必须是抽象的,但是 Arrays.sort(arr3, Shape::compare);没用。我该如何解决这个问题?

java arrays sorting abstract-class
3个回答
1
投票

我建议这样一个形状的抽象基类:

public abstract class Shape implements Comparable<Shape> {
    private String color;

    public Shape(String color) {
        super();
        this.color = color;
    }
    
    // geters & setters
    
    @Override
    public int compareTo(Shape other) {
        if ((getAsciiSum(this.color)).compareTo(getAsciiSum(other.color)) < 0) {
            return -1;
        }
        if ((getAsciiSum(this.color)).compareTo(getAsciiSum(other.color)) == 0) {
            return 0;
        }
        return 1;
    }
    
    // toString
    
    public int getAsciiSum() {
        return getAsciiSum(this.color);
    }

    private Integer getAsciiSum(String str) {
        int sum = 0;
        for (Character ch : str.toCharArray()) {
            int asciiValue = ch;
            sum += asciiValue;
        }
        return sum;
    }
    
    // finish

形状扩展器可以如下

圆圈:

public class Circle extends Shape implements ShapeType {

    private int radius;

    public Circle(String color) {
        super(color);
    }

    public Circle(String color, int radius) {
        this(color);
        this.radius = radius;
    }       
    
    // geters & setters, toString

矩形:

public class Rectangle extends Shape implements ShapeType {

    int width;
    int highth;

    public Rectangle(String color) {
        super(color);
    }

    public Rectangle(String color, int width, int highth) {
        this(color);
        this.width = width;
        this.highth = highth;
    }

    // geters & setters, toString 

三角形:

public class Triangle extends Shape implements ShapeType {

    private int sideOne;
    private int sideTwo;
    private int sideThree;

    public Triangle(String color) {
        super(color);
    }

    public Triangle(String color, int sideOne, int sideTwo, int sideThree) {
        this(color);
        this.sideOne = sideOne;
        this.sideTwo = sideTwo;
        this.sideThree = sideThree;
    }


    // geters & setters, toString 

并执行以下命令后:

    System.out.println("\nbefore sort:");
    print(arr3);

    Arrays.sort(arr3, Shape::compareTo);

    System.out.println("\nafter sort:");
    print(arr3);

我们可以获得结果 -

排序前:

Circle [AsciiSum=392, color=BROWN, radius=5]|i=0

Rectangle [AsciiSum=369, color=GREEN, width=12, highth=23]|i=1

Triangle [AsciiSum=392, color=BROWN, sideOne=5, sideTwo=3, sideThree=4]|i=2

Rectangle [AsciiSum=476, color=YELLOW, width=14, highth=25]|i=3

Rectangle [AsciiSum=385, color=WHITE, width=15, highth=26]|i=4

Circle [AsciiSum=296, color=BLUE, radius=4]|i=5

Rectangle [AsciiSum=219, color=RED, width=11, highth=22]|i=6

Rectangle [AsciiSum=349, color=BLACK, width=13, highth=24]|i=7

Triangle [AsciiSum=719, color=ROYAL BLUE, sideOne=5, sideTwo=7, sideThree=8]|i=8

Triangle [AsciiSum=1147, color=CHILDS SURPRISE, sideOne=6, sideTwo=4, sideThree=6]|i=9

排序后:

Rectangle [AsciiSum=219, color=RED, width=11, highth=22]|i=0

Circle [AsciiSum=296, color=BLUE, radius=4]|i=1

Rectangle [AsciiSum=349, color=BLACK, width=13, highth=24]|i=2

Rectangle [AsciiSum=369, color=GREEN, width=12, highth=23]|i=3

Rectangle [AsciiSum=385, color=WHITE, width=15, highth=26]|i=4

Circle [AsciiSum=392, color=BROWN, radius=5]|i=5

Triangle [AsciiSum=392, color=BROWN, sideOne=5, sideTwo=3, sideThree=4]|i=6

Rectangle [AsciiSum=476, color=YELLOW, width=14, highth=25]|i=7

Triangle [AsciiSum=719, color=ROYAL BLUE, sideOne=5, sideTwo=7, sideThree=8]|i=8

Triangle [AsciiSum=1147, color=CHILD'S SURPRISE, sideOne=6, sideTwo=4, sideThree=6]|i=9

-- 编辑 --

当然,颜色字段中的字符串可以按字典顺序进行比较,然后compareTo方法就可以很简单,如:

@Override
public int compareTo(Shape other) {
    if ((this.color).compareTo(other.color) < 0) {
        return -1;
    }
    if ((this.color).compareTo(other.color) == 0) {
        return 0;
    }
    return 1;
}

排序后数组元素的顺序如下:

Rectangle [AsciiSum=349, color=BLACK, width=13, highth=24]|i=0

Circle [AsciiSum=296, color=BLUE, radius=4]|i=1

Circle [AsciiSum=392, color=BROWN, radius=5]|i=2

Triangle [AsciiSum=392, color=BROWN, sideOne=5, sideTwo=3, sideThree=4]|i=3

Triangle [AsciiSum=1147, color=CHILD'S SURPRISE, sideOne=6, sideTwo=4, sideThree=6]|i=4

Rectangle [AsciiSum=369, color=GREEN, width=12, highth=23]|i=5

Rectangle [AsciiSum=219, color=RED, width=11, highth=22]|i=6

Triangle [AsciiSum=719, color=ROYAL BLUE, sideOne=5, sideTwo=7, sideThree=8]|i=7

Rectangle [AsciiSum=385, color=WHITE, width=15, highth=26]|i=8

Rectangle [AsciiSum=476, color=YELLOW, width=14, highth=25]|i=9

    

-1
投票

Arrays.sort()
将需要一个数组并且
Comparator

您可以像这样定义您的
Comparator

Comparator shapeComparator = new Comparator<Shape>() {
                                     @Override
                                     public int compare(Shape o1, Shape o2) {
                                          return o1.color.compareTo(o2.color);
                                     }
                            };

你可以像这样将定义的

shapeComparator
赋予
Arrays.sort()

Arrays.sort(shapes, shapeComparator);

-1
投票
Class Shape{
protected String color;

@Override
public int hashCode(){
         //give your definition
}

@Override
public int equals(){
  //give your definition
}

}
Class Rectangle extends Shape{
  private int height;
  private int length;
}
Class Circle extends Shape{
  private int redius;
}

Class Triangle extends Shape{
  //declare your member variable 
}

尝试编写一个比较器类并在 Arrays.sort(arr, shapeComparator) 中传递它的引用。

并自动 sort() 方法对形状类数组进行排序。

class SortbyColor implements Comparator<Shape> {
    // Used for sorting in ascending order of
    // Color
    public int compare(Shape a, Shape b)
    {
        return a.color.compareTo(b.color);
    }
}

OR 上面的比较器代码也可以重写为

Comparator shapeComparator = new Comparator<Shape>() {
                                     @Override
                                     public int compare(Shape a, Shape b) {
                                          return a.color.compareTo(b.color);
                                     }
                            };

现在调用

Arrays.sort(arr, new SortbyColor())
,你的数组就会自动变短

© www.soinside.com 2019 - 2024. All rights reserved.