我有两个列表,具有完全相同的值,但是当我使用equal()方法时,它返回false ...有人可以帮助我......这是我的代码。
@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]
假
编辑:三角级
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;
}
}
我的多边形类(三角形的超类是=)
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());
}
}
答案 0 :(得分:0)
您的Point
课程未覆盖equals
的{{1}}方法。
您可以将方法Object
更改为Equals
或将此方法添加到equals
类:
Point
答案 1 :(得分:-1)
Triangle课程是你创造的吗?您需要使用自己的实现重载equals(Object o)方法。否则,将使用默认的equals方法。由于这些是两个不同的对象,结果将是错误的。
这只是一个开始
if($this->upload->do_upload('menu_image')){
$uploaded_data = $this->upload->data(); //get the uploaded image information
$this->resize_uploaded_image($uploaded_data); //send for resizing
//example of saving IMAGE FILENAME TO DB, YOU CAN OPT TO SAVE FULLPATH
$this->model->save_image_filename($uplodated_data['filename']);
}else{
...
}
//resizer
public function resize_uploaded_image($image_data)
{
$config = array(
'image_library' => 'gd2',
'source_image' => $image_data['full_path'], //uploaded imagepath
'create_thumb' => 'true',
'width' => 575,
'height' => 550,
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}