是System.out.print(Arrays.toString());不适用于OBJECTS数组

时间:2016-04-08 03:17:47

标签: java object printing

我一直在寻找如何解决这个问题,但我找不到适用于我的问题的东西。我正在尝试打印出Circles的数组。我所知道的是,我必须覆盖toString()方法。

我一直得到以下输出:

  

[Heather $ Circle @ 2a139a55,Heather $ Circle @ 15db9742,   Heather $ Circle @ 6d06d69c,Heather $ Circle @ 7852e922]

import java.util.*;
public class heather {
    public static void main(String[] args) {
        heather c = new heather();

        Circle c1 = c.new Circle(4,6,4);
        Circle c2 = c.new Circle(4,5,4);
        Circle c3 = c.new Circle(5,4,4);
        Circle c4 = c.new Circle(5,4,3);

        Circle[] a = {c1, c2, c3, c4};

        Arrays.sort(a);
        System.out.print(Arrays.toString(a));
    }

    public class Point {
        private int x;
        private int y;

        public Point(int x, int y){
            this.x = x; this.y = y;
        }

        public int getX(){
            return this.x;
        }

        public int getY(){
            return this.y;
        }
    }

    public class Circle extends Point implements Comparable<Circle> {
        private double radius;
        private Point point;

        public Circle(int x, int y, double radius) {
            super(x, y); this.radius = radius;
        }

        public double getRadius() {
            return this.radius;
        }

        public Point getPoint() {
            return this.point;
        }

        public int area() {
            return (int) (Math.PI*radius*radius);
        }

        public int compareTo(Circle other){
            if(this.area()>other.area()) {
                return 1;
            }

            if(this.area()<other.area()) {
                return -1;
            } else if(this.getX()>other.getX()) {
                return 1;
            }

            if (this.getX()<other.getX()){
                return -1;
            } else if(this.getY()<other.getY()) {
                return -1;
            } else {
                return 1;
            }
        }
    }
    public String toString(){

    }
}

1 个答案:

答案 0 :(得分:4)

这部分代码......

    }
public String toString(){

}

在包含Circle方法之前关闭toString()类。因此,你应该像......一样重写它。

    public String toString(){

    }
}

然后在toString()方法中填写您想要的任何内容。也许像是......

return "Circle of radius " + radius; 

如果您主动整理代码以提高可读性,您可能会发现这些问题更容易被发现。我已经清理了您发布的代码以供参考...

package Shift;

import java.util.*;

public class Shift {

    public static void main(String[] args) {

        Shift c = new Shift();

        Circle c1 = c.new Circle(4,6,4); 
        Circle c2 = c.new Circle(4,5,4); 
        Circle c3 = c.new Circle(5,4,4);
        Circle c4 = c.new Circle(5,4,3);


        Circle[] a = {c1, c2, c3, c4};

        Arrays.sort(a); 
        System.out.print(a[0]);
    }



public class Point{

    private int x;
    private int y;

    public Point(int x, int y){
        this.x = x; this.y = y; 
    }

    public int getX(){
        return this.x;
    }

    public int getY(){
        return this.y;
    }
}



public class Circle extends Point implements Comparable<Circle>{

    private double radius;
    private Point point;

    public Circle(int x, int y, double radius) {
            super(x, y);
            this.radius = radius;
    }

    public double getRadius(){
        return this.radius;
    }

    public Point getPoint(){
        return this.point;
    }

    public int area(){
        return (int) (Math.PI*radius*radius);
    }

    public int compareTo(Circle other){
        if(this.area()>other.area())
            return 1;


        if(this.area()<other.area())
            return -1;
        else if(this.getX()>other.getX())
                return 1;


        if (this.getX()<other.getX())
                return -1;
        else if(this.getY()<other.getY())
            return -1;
        else
            return 1;
    }

    @Override
    public String toString(){
        return "Circle of radius " + radius; 
    }
}


}