簡體   English   中英

是System.out.print(Arrays.toString()); 不適用於對象數組

[英]System.out.print(Arrays.toString()); is not working for array of OBJECTS

我一直在尋找解決方法,但是找不到適合我的問題的東西。 我正在嘗試打印出一個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(){

    }
}

這部分代碼...

    }
public String toString(){

}

在包含您的toString()方法之前關閉Circle類。 因此,您應該像這樣重寫它:

    public String toString(){

    }
}

然后只需在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; 
    }
}


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM