gpt4 book ai didi

java - 如何列出自己的 vector 元素?

转载 作者:行者123 更新时间:2023-11-30 04:35:06 25 4
gpt4 key购买 nike

我有一个自己的 vector .类和多边形.类 vector .类

package org.onvif.ver10.schema;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "Vector")

public class Vector {

@XmlAttribute(name = "x")
protected Float x;
@XmlAttribute(name = "y")
protected Float y;

/**
* Gets the value of the x property.
*
* @return
* possible object is
* {@link Float }
*
*/
public Float getX() {
return x;
}

/**
* Sets the value of the x property.
*
* @param value
* allowed object is
* {@link Float }
*
*/
public void setX(Float value) {
this.x = value;
}

/**
* Gets the value of the y property.
*
* @return
* possible object is
* {@link Float }
*
*/
public Float getY() {
return y;
}

/**
* Sets the value of the y property.
*
* @param value
* allowed object is
* {@link Float }
*
*/
public void setY(Float value) {
this.y = value;
}
}

和多边形类

public class Polygon {

@XmlElement(name = "Point", required = true)
protected List<Vector> point;

/**
* Gets the value of the point property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the point property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getPoint().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link Vector }
*
*
*/
public List<Vector> getPoint() {
if (point == null) {
point = new ArrayList<Vector>();
}
return this.point;
}

}

所以,我给出 vector 元素:

org.onvif.ver10.schema.Vector MyVector = new Vector(); // létrehozzuk az
// onvif féle
// vector-t
org.onvif.ver10.schema.Polygon op = new org.onvif.ver10.schema.Polygon();

for (int i = 1; i <= p.npoints; i++) {
// IJ.log("X: "+ i);
// MyVector.setX((float) p.xpoints[i]); // hozzáadjuk az elemet
// MyVector.setY((float) p.ypoints[i]);

MyVector.setX((float)p.xpoints[i-1]);
op.getPoint().add(MyVector);
IJ.log("Vector X Elements "+i+" :"+ MyVector.getX());

}
IJ.log("Op size " + op.getPoint().size());

我的问题是如何获取 op(onvif 多边形)元素?因为无论我如何尝试,我都只获得了最后一个元素 10 次。

最佳答案

您的错误是,您将相同的 Vector 实例添加到多边形中 10 次,并一遍又一遍地在该实例上设置点。

您的代码应该更改为将 new Vector() 行移动到循环内部:

    org.onvif.ver10.schema.Polygon op = new org.onvif.ver10.schema.Polygon();

for (int i = 1; i <= p.npoints; i++) {
org.onvif.ver10.schema.Vector MyVector = new Vector();
MyVector.setX((float)p.xpoints[i-1]);
op.getPoint().add(MyVector);
IJ.log("Vector X Elements "+i+" :"+ MyVector.getX());

}

代码的风格问题包括但不限于:

  • 变量名称应以小写字母开头,因此 myVector 不是 MyVector
  • 切勿创建与 JDK 类同义的类名(例如 Vector)

关于java - 如何列出自己的 vector 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13702429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com