为数组列表设置toString()方法

时间:2014-06-26 03:17:45

标签: java arrays arraylist

对于一个简单问题有什么问题我很抱歉,但我如何为数组列表设置toString()方法呢?

就像

一样简单
points = new ArrayList<Point>();
public String toString() {
return points.toString();}

这对我来说似乎没有用,或者它会更复杂,因为它是一个数组列表?因为某些原因,当我像这样执行我的时,它只打印第一个值或对象。

P.S我试图返回我已经添加到列表中的所有值。

更详细 建筑工

public Cloud() {
    points = new ArrayList<Point>();
}

添加点

public void addPoint(Point p) { // done
    if (points.contains(p)) {
        // if p is already in the list it does nothing
    } else {
        points.add(p); // if p was not in the list it adds it to the end
    }
}

toString

public String toString() {
    return points.toString();
}

public static void main(String[] args) {
    Cloud cloud = new Cloud();

    cloud.setDebug(false);
    System.out.println("cloud.debug OFF");

    System.out.println("initial cloud: " + cloud.toString());
    Point p1 = new Point(3.0, 1.0);
    cloud.addPoint(p1);

    Point p2 = new Point(2.0, 2.0);
    cloud.addPoint(p2);

    Point p3 = new Point(1.5, 1.5);
    cloud.addPoint(p3);

    Point p4 = new Point(3.0, 0.0);
    cloud.addPoint(p4);

    System.out.println("final cloud: " + cloud);

这只是打印最终云:(3.0,1.0),而它应该是打印最终云:[(3.0,1.0),(2.0,2.0),(1.5,1.5),(3.0,0.0)]

编辑:点数类

public class Point {

private double x;
private double y;

public static final double EPSILON = 1e-5;
public static boolean debug = false;

public Point(double x, double y) {
    this.x = x;
    this.y = y; // Done sets the x,y private types to the x,y type provided
                // in the ()
}

public Point() {
    this(0.0, 0.0); // calls the point (double x,double) constructer with
                    // the given arguments
} // inturn setting x and y == 0.0

public double getX() {
    return x; // returns the private value of x when called in the main
                // method
} // so it can't be changed by the user

public double getY() {
    return y; // return the private value of y when called in the main
                // method so it can't be changed
} // by the user

public String toString() {
    return "(" + x + "," + y + ")"; // done by teacher sets the toString
                                    // method and implemetns it
}

public boolean equals(Point p) {
    if (Math.abs(this.getX()) - Math.abs(p.x) < EPSILON) {
        return true; // checks if x - p.x is less than epsilon which covers
                        // the round off
    }
    if (Math.abs(this.getY()) - Math.abs(p.y) < EPSILON) {
        return true; // checks if y-p.y is less than epsilon which covers
                        // the round off
    }
    return false; // both these methods test for equality using epsilon,
                    // becuae we are dealing with
} // doubles, so roundof can occur

public boolean equals(Object obj) { // this was given to us
    if (obj instanceof Point) {
        Point p = (Point) obj; // This method overrides the object equals
                                // method and the calls
        return equals(p); // the clas's equals(point) method
    }
    return false;
}

// TODO Implement Point.euclidDist
/**
 * 
 * @param p
 * @return Euclidean distance of this point to point p
 */
public double euclidDist(Point p) {
    double distance = 0;
    double firstvalue;
    double secondvalue;
    distance = Math.sqrt(((this.getX() - p.x) * (this.getX() - p.x)) // calculate
                                                                        // the
                                                                        // distance
            + ((this.getY() - p.y) * (this.getY() - p.y))); // between the
                                                            // two points
    // firstvalue= Math.pow(this.getX()-p.x, 2);
    // secondvalue= Math.pow(this.getY()-p.y, 2);
    // distance = Math.sqrt(firstvalue + secondvalue);

    return distance;
}

/**
 * @param args
 *            : no args
 */
public static void main(String[] args) {

    // test all methods

    if (debug)
        System.out.println("debug ON");
    else
        System.out.println("debug OFF");

    System.out.println("EPSILON: " + Point.EPSILON);

    Point origin = new Point();
    Point p1 = new Point(0.0, 4.0);
    Point p2 = new Point(3.0000001, 3.9999999);
    Point p3 = new Point(3.0, 4.0);

    Point p4 = new Point(0.0, 5.0);
    Point p5 = new Point(12.0, 0.0);

    System.out.println("origin: " + origin);
    System.out.println("p1: " + p1);
    System.out.println("p2: " + p2);
    System.out.println("p3: " + p3);
    System.out.println("p4: " + p4);
    System.out.println("p5: " + p5);

    if (p2.equals(p3))
        System.out.println(p2 + " equals " + p3);
    else
        System.out.println(p2 + " does not equal " + p3);

    System.out.println("Euclidean distance between " + origin + " and "
            + p1 + ": " + origin.euclidDist(p1));

    System.out.println("Euclidean distance between " + p1 + " and " + p3
            + ": " + p1.euclidDist(p3));

    System.out.println("Euclidean distance between " + p3 + " and "
            + origin + ": " + p3.euclidDist(origin));

    System.out.println("Euclidean distance between " + p4 + " and " + p5
            + ": " + p4.euclidDist(p5));

}

}

1 个答案:

答案 0 :(得分:1)

您只能在自己的课程中创建toString()方法覆盖,而不能在您未覆盖的其他课程中创建。 ArrayList已经有一个有用的有效toString()方法。您只需要确保List持有的项目来自同样具有有效toString()方法的类。

请注意:

  

这似乎对我不起作用......   因为出于某种原因,当我像这样执行我的时,它只打印第一个值或对象。

这表示您没有toString()问题,但实际上您的程序存在另一个完全不同的问题,即您没有正确地将对象添加到列表中。您需要进行更多调试并显示更多相关代码。


修改
我猜测你的Point类的contains(...)方法是错误的,当它应该返回false时返回true。请告诉我们Point类。


编辑3 (已删除编辑2) 你的平等是错的:

没关系:

public boolean equals(Object obj) { // this was given to us
    if (obj instanceof Point) {
        Point p = (Point) obj; // This method overrides the object equals
                                // method and the calls
        return equals(p); // the clas's equals(point) method
    }
    return false;
}

但是在这里,如果x或者y紧密匹配并且不应该匹配,则返回等于。如果 BOTH 与之匹配,则您应该只返回true:

public boolean equals(Point p) {
    if (Math.abs(this.getX()) - Math.abs(p.x) < EPSILON) {
        return true; // checks if x - p.x is less than epsilon which covers
                        // the round off
    }
    if (Math.abs(this.getY()) - Math.abs(p.y) < EPSILON) {
        return true; // checks if y-p.y is less than epsilon which covers
                        // the round off
    }
    return false; // both these methods test for equality using epsilon,
                    // becuae we are dealing with
} // doubles, so roundof can occur

您还错误地使用Math.abs(...)。它应该绕过减法语句,而不是围绕每个变量。