英文:
How do I return a user-defined object in java?
问题
我正在编写一个程序,该程序创建一个包含名称和两个节点(x、y、z坐标)的对象(Line),然后将它们存储在一个单独的对象(LineModel类)中。在LineModel类中创建了一个名为getNode()的方法,该方法应返回节点。节点是在单独的对象(Node类)中构建的。
我的问题出现在getNode()方法中,因为我似乎无法返回我正在寻找的节点。
public class LineModel {
// 对象属性
private String name;
private Line[] lines;
private int numLines;
// 构造函数
public LineModel(String name, int maxLines) {
this.name = name;
lines = new Line[maxLines];
numLines = 0;
}
// 添加线
public void addLine(Line line) {
if (contains(line)) {
System.out.println("线 " + line.getName() + " 已经在模型中");
return;
}
if (numLines < lines.length) {
lines[numLines] = line;
numLines++;
} else {
System.out.println("增加线数组大小。");
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();
}
}
return null;
}
}
以下是Line和Node类:
public class Line {
// 对象属性
private String name;
private Node n1, n2;
// 构造函数
public Line(String name, Node n1, Node n2){
this.name = name;
this.n1 = n1;
this.n2 = n2;
}
public String getName(){ return name; }
// 对象方法
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 "线 "+name+" "+n1.getName()+"-->" + n2.getName() + " 长度 = " + length();
}
public Node getN1() { return n1;}
public Node getN2() { return n2;}
}
public class Node {
// 对象属性
private String name;
private double[] coordinates;
// 构造函数
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;
}
// 对象方法
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 "节点 "+name+" "+Arrays.toString(coordinates);
}
}
当前的错误是它必须返回Node类型,但我似乎无法弄清楚为什么会这样说。对于如此大量的代码,我很抱歉。我是编程新手,所以我不知道是否所有内容都相关。
英文:
I am writing a program that creates an object (Line) that contains a name and two nodes (x,y,z coordinates) which are then stored in a separate object (class LineModel). Within the class LineModel a method, getNode(), is created that should return the node. The nodes are constructed in a separate object (class Node).
My problem lies within the method getNode(), as I can't seem to return the node that I'm looking for.
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;
}
}
}
Below are the classes Line and 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);
}
}
The current error is that it has to return type Node, but I can't seem to figure out why it says that. Sorry for the large amount of code. I'm quite new to coding so I don't know if everthing is relevant.
答案1
得分: 0
在“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"。
英文:
The for-loop inside "getNode" always gets terminated after one iteration.
If the first node has the matching name it returns the node, otherwise it returns null which always terminates the for-loop after the first iteration without checking any further nodes inside the array.
IMHO you should change the code to something like this:
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;
}
In this case the for-loop iterates over each element of the array until it finds the correct node or until there are no more elements left. If the requested node gets found it returns the node, otherwise it returns null.
You should also do
for (int i = 0; i <= numLines; i++)
instead of
for (int i = 0; i < numLines; i++)
because, in your implementation the last element of the array will always be ignored as if the array has one element "numLines" will be "1" and "1 < 1 == false". This means, that the loop even doesn't do any iteration and if "numLines" is "2" it will only perform one iteration as "2 < 2 == false".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论