如何在Java中返回用户定义的对象?

问题描述

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

我的问题出在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 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);
    }
}

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

解决方法

“ 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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...