我有一类实例如下:
package com.sample;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement (name = "instance")
public class Instance {
int id;
double startValue;
double endValue;
double duration;
String Code;
public int getId() {
return id;
}
@XmlElement (name = "ID")
public void setId(int id) {
this.id = id;
}
public double getStart() {
return startValue;
}
@XmlElement
public void setStart(double start) {
this.startValue = start;
}
public double getEnd() {
return endValue;
}
@XmlElement
public void setEnd(double end) {
this.endValue = end;
}
public void setDuration() {
duration = endValue - startValue;
}
public String getCode() {
return Code;
}
@XmlElement
public void setCode(String code) {
Code = code;
}
}
每个实例对象都通过jaxb unmarshaller映射到事件的XML文件中的值。然后将这些实例对象存储在名为Instances的列表类中,如下所示:
package com.sample;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement (name = "ALL_INSTANCES")
@XmlAccessorType(XmlAccessType.FIELD)
public class Instances {
@XmlElement(name = "instance")
private List<Instance> instances = null;
public List<Instance> getInstances() {
return instances;
}
public void setInstances(List<Instance> instances) {
this.instances = instances;
}
public int getLength() {
return instances.size();
}
public static List<Instances> getUnion(List<Instances> one, List<Instances> two) {
List<Instances> union = new ArrayList<Instances>(one);
union.addAll(two);
return union;
}
}
在main方法中,我尝试在调用unmarshalling进程之后调用两个实例列表的getUnion()方法连接成一个。
Instances ins = (Instances) jaxbUnmarshaller.unmarshal(file);
Instances ins1 = (Instances) jaxbUnmarshaller.unmarshal(file1);
Instances.getUnion(ins, ins1);
但是说我得到&#34; Instances类型中的方法getUnion(List,List)不适用于参数(Instances,Instances)&#34;。不确定我是否正确调用此设计或设计正确。
基本上,我需要一个Instances类的方法,它将两个实例连接在一起组成一个List来处理。
感谢。
答案 0 :(得分:0)
该方法采用List<Instance>
类型的两个参数,而不是类型Instances
,这有点令人困惑,您可能最好更改getUnion
方法。但就目前看来,你可以像这样调用它:
Instances ins = (Instances) jaxbUnmarshaller.unmarshal(file);
Instances ins1 = (Instances) jaxbUnmarshaller.unmarshal(file1);
List<Instance> result = Instances.getUnion(ins.getInstances(), ins1.getInstances());