2017-11-08 9 views
-1

私は同じ値を持つ2つのリストを持っていますが、equal()メソッドを使うとfalseを返します。誰かが助けてくれますか?falseを返す等しいリスト - Java

@Test 
public void testPolygonConstructor(){ 
    try { 
     Triangle t = new Triangle("Triangle 3 4 9 4 3 8"); 
     Point a1 = new Point(3,4); 
     Point a2 = new Point(9,4); 
     Point a3 = new Point(3,8); 
     List<Object> list = new ArrayList<Object>(Arrays.asList(a1,a2,a3)); 
     System.out.println(t.coordenadas); 
     System.out.println(list); 
     System.out.println(t.coordenadas.equals(list)); 

    } catch (Exception e) { 

    } 
} 

私の出力である:

[3 4、9 4、3~8]
[3 4、9 4、3~8]

EDIT:三角クラス

public class Triangle extends Polygon { 
    public Triangle(String s) throws Exception { 
     super(s); 
     if (!isValid()) { 
      throw new Exception(); 
     } 
    } 

    public boolean isValid() { 
     if (isAllEqual() || coordenadas.size() != 3) { 
      return false; 
     } 
     double lado1 = coordenadas.get(0).dist(coordenadas.get(1)); 
     double lado2 = coordenadas.get(0).dist(coordenadas.get(2)); 
     double lado3 = coordenadas.get(1).dist(coordenadas.get(2)); 
     if (lado1 + lado2 > lado3 && lado1 + lado3 > lado2 && lado2 + lado3 > lado1) { 
      return true; 
     } 

     return false; 
    } 

} 

と私のポリゴンclお尻(三角のスーパークラス=です)

import java.util.List; 
import java.util.Scanner; 
import java.util.ArrayList; 

public abstract class Polygon { 
    protected List<Point> coordenadas = new ArrayList<Point>(); 

    public Polygon(String s) throws Exception { 
     coordenadas = readPoints(s); 
     if (coordenadas.isEmpty()) { 
      throw new Exception(); 
     } 
    } 

    private List<Point> readPoints(String s) { 

     s = s.replaceAll("[^0-9]+", " "); 
     s.trim().split(" "); 
     Scanner scanner = new Scanner(s); 
     List<Integer> list = new ArrayList<Integer>(); 
     while (scanner.hasNextInt()) { 
      list.add(scanner.nextInt()); 
     } 
     scanner.close(); 
     int j = 0; 
     for (int i = 0; i < list.size() - 1; i += 2) { 
      Point k = new Point(list.get(i), list.get(i + 1)); 
      coordenadas.add(j, k); 
      j++; 
     } 
     return coordenadas; 

    } 

    public Point centroid() { 
     double x = 0; 
     double y = 0; 
     for (int i = 0; i < coordenadas.size(); i++) { 
      x += coordenadas.get(i).getX(); 
      y += coordenadas.get(i).getY(); 
     } 

     x = x/coordenadas.size(); 
     y = y/coordenadas.size(); 
     return new Point((int) x, (int) y); 

    } 

    public double perimeter() { 
     double distance = 0; 
     int len = coordenadas.size(); 
     for (int i = 0; i < len; i++) { 
      distance += coordenadas.get(i).dist(coordenadas.get((i + 1) % len)); 
     } 
     return distance; 
    } 

    public boolean isAllEqual() { 
     Point first = coordenadas.get(0); 
     for (int i = 1; i < coordenadas.size(); i++) { 
      if (!coordenadas.get(i).Equals(first)) 
       return false; 
     } 
     return true; 
    } 

    public abstract boolean isValid();       

} 

私は問題が "coordenadas"が保護されているかもしれないと思いますか?

EDIT2:ここではポイントクラスの悲惨な男です!

public class Point { 

    private double x = 0, y = 0; 

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

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

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

    public boolean Equals(Point p) { 
     return this.x == p.getX() && this.y == p.getY(); 
    } 

    public double dist(Point p) { 
     double dx = getX() - p.getX(); 
     double dy = getY() - p.getY(); 
     return Math.sqrt(dx * dx + dy * dy); 
    } 

    public String toString() { 
     return ((int) getX() + " " + (int) getY()); 
    } 
} 
+2

't.coordenadas'の種類は何ですか? –

+5

にはTriangleクラスとPointクラスのコードが必要ですが、Pointクラスにequals/hashCodeを実装していない可能性があります。 –

+0

私のクラスではポリゴン 保護されたリスト coordenadas = new ArrayList (); –

答えて

-1

作成したTriangleクラスはありますか? equals(Object o)メソッドを独自の実装でオーバーロードする必要があります。それ以外の場合は、デフォルトのequalsメソッドが使用されます。これらは2つの異なるオブジェクトであるため、結果はfalseになります。

これはあなたのPointクラスがObjectからequalsメソッドをオーバーライドしないだけでスタート

public boolean equals(List input) { 

     return compare(point1, input) && compare(point2, input) && compare(point3, input); 

    } 
+2

なぜ 'ポイント'と 'リスト'を比較していますか? – azurefrog

+0

ちょうどスタート...おそらくポイントは異なる順序です。各点をチェックする必要があります。 compare()は悪い命名でした。 – rhettmaxwell

0

です。

あなたは方法Equals

equalsに変更したり、Pointクラスにこのメソッドを追加することができます。

public boolean equals(Object obj) { 
    if (obj instanceof Point) { 
     Point pt = (Point) obj; 
     return (x == pt.x) && (y == pt.y); 
    } 
    return false; 
} 
+0

コードがうまく実行され、ユニットケースをテストするためにjunitを実行しています... –

+0

私はあなたの問題を解決して答えを編集しました。 –

関連する問題