首页 > 解决方案 > 如何在java中返回用户定义的对象?

问题描述

我正在编写一个程序,该程序创建一个对象(Line),其中包含一个名称和两个节点(x、y、z 坐标),然后将它们存储在一个单独的对象(LineModel 类)中。在 LineModel 类中创建了一个方法 getNode(),它应该返回节点。节点是在一个单独的对象(类节点)中构造的。

我的问题在于方法 getNode(),因为我似乎无法返回我正在寻找的节点。

public class LineModel {

    // Object attributes
    private String name;
    private Line[] lines;
    private int numLines;

    // Constructor
    public LineModel(String name, int maxLines) {
        this.name = name;
        lines = new Line[maxLines];
        numLines = 0;
    }
    
    // Add lines
    public void addLine(Line line) {
        if (contains(line)) {
            System.out.println("Line " + line.getName() + " already in model");
            return;
        }
        if (numLines < lines.length) {
            lines[numLines] = line;
            numLines++;
        } else {
            System.out.println("Increase lines array size.");
            System.exit(1);
        }
    }

    public Node getNode(String name) {
        for (int i = 0; i < numLines; i++) {
            if (lines[i].getN1().getName().equals(name) || lines[i].getN2().getName().equals(name)) {
                return lines[i].getN1();
            } else {
                return null;
            }
        }
    }

下面是 Line 和 Node 类

public class Line {
    // Object attributes
    private String name;
    private Node n1, n2;
    
    // Constructor(s)
    public Line(String name, Node n1, Node n2){
        this.name = name;
        this.n1 = n1;
        this.n2 = n2;
    }
    
    public String getName(){ return name; }
    
    // Object methods
    public double length(){
        double[] n1C = n1.getCoordinates();
        double[] n2C = n2.getCoordinates();
        if(n1C.length == n2C.length){
            double pythagoras = 0;
            for (int i = 0; i < n1C.length; i++) {
                double dv = n2C[i] - n1C[i];
                pythagoras += dv*dv;
            }
            return Math.sqrt(pythagoras);
        }
        return Double.NaN;
    }

    @Override
    public String toString(){
        return "Line "+name+" "+n1.getName()+"-->"+n2.getName()+" Length = "+length();
    }
    
    public Node getN1() { return n1;}
    public Node getN2() { return n2;}
public class Node {
    // Object attributes
    private String name;
    private double[] coordinates;

    // Constructor(s)
    public Node(String name, double x) {
        this.name = name;
        coordinates = new double[1];
        coordinates[0] = x;
    }

    public Node(String name, double x, double y) {
        this.name = name;
        coordinates = new double[2];
        coordinates[0] = x; coordinates[1] = y;
    }

    public Node(String name, double x, double y, double z) {
        this.name = name;
        coordinates = new double[3];
        coordinates[0] = x; coordinates[1] = y; coordinates[2] = z;
    }

    // Object methods
    public String getName(){
        return name;
    }
    
    public double[] getCoordinates(){
        return coordinates;
    }
    
    public double getX() {
        if (coordinates.length > 0){
            return coordinates[0];
        } else {
            return Double.NaN;
        }
    }
    
    public double getY() {
        if (coordinates.length > 1){
            return coordinates[1];
        } else {
            return Double.NaN;
        }
    }
    
    public double getZ() {
        if (coordinates.length > 2){
            return coordinates[2];
        } else {
            return Double.NaN;
        }
    }

    public String toString() {
        return "Node "+name+" "+Arrays.toString(coordinates);
    }
}

当前的错误是它必须返回类型节点,但我似乎无法弄清楚它为什么这么说。抱歉,代码量很大。我对编码很陌生,所以我不知道一切是否相关。

标签: javaobjectmethodscoordinatesline

解决方案


“getNode”中的 for 循环总是在一次迭代后终止。如果第一个节点具有匹配的名称,则返回该节点,否则返回 null,这始终在第一次迭代后终止 for 循环,而不检查数组中的任何其他节点。

恕我直言,您应该将代码更改为以下内容:

public Node getNode(String name) {
        for (int i = 0; i <= numLines; i++) {
            if (lines[i].getN1().getName().equals(name) || lines[i].getN2().getName().equals(name)) {
                return lines[i].getN1();
            }
        }
        return null;
    }

在这种情况下,for 循环遍历数组的每个元素,直到找到正确的节点或直到没有更多元素为止。如果找到请求的节点,则返回该节点,否则返回 null。

你也应该这样做

for (int i = 0; i <= numLines; i++)

代替

for (int i = 0; i < numLines; i++)

因为,在您的实现中,数组的最后一个元素将始终被忽略,就好像数组有一个元素“numLines”将是“1”和“1 < 1 == false”。这意味着,循环甚至不进行任何迭代,如果“numLines”为“2”,它只会执行一次迭代,即“2 < 2 == false”。


推荐阅读