无法访问ArrayList内容

时间:2014-05-20 11:13:13

标签: java arraylist xstream

我使用Xsrteam将XML解析为ArrayList。它有效,但出于某种原因,我无法获得数组的内容。

处理ArrayList数据的类:

@XStreamAlias("BOARDS")
public class Boards {
    @XStreamImplicit(itemFieldName = "id")
    public ArrayList ids = new ArrayList();

    //region GETTERS-SETTERS
    public void setIds(ArrayList temp){
        this.ids = temp;
    }
    public List getIds(){
        return ids;
    }
//    endregion
}

@XStreamAlias("id")
class Id {

    @XStreamAlias("board")
    private String board;

    @XStreamAlias("description")
    private String description;

    @XStreamAlias("price")
    private String price;

    @XStreamAlias("shape")
    private String shape;

    @XStreamAlias("riding_level")
    private String ridingLevel;

    @XStreamAlias("riding_style")
    private String ridingStyle;

    @XStreamAlias("camber_profile")
    private String camber;

    @XStreamAlias("stance")
    private String stance;

    @XStreamAlias("picture")
    private String picture;

    <<public getters - setters here>>
}

我如何尝试访问getters

Boards boards = (Boards) xstream.fromXML(reader); // parse xml into array list
boards.getIds().get(0).getPrice(); //!!getPrice() cannot be resolved

firstObject first = boards.getIds().get(0);

这里使用调试器的样子:

enter image description here

1 个答案:

答案 0 :(得分:2)

Boards有一个原始类型,因为它不清楚,ArrayList ids内应该有哪些对象类型。因此,您应该明确地投射boards.getIds().get(0)的结果:

((Id) boards.getIds().get(0)).getPrice()

或生成Boards类:

public class Boards<E> {

    public List<E> ids = new ArrayList<E>();

    //region GETTERS-SETTERS
    public void setIds(ArrayList<E> temp){
        this.ids = temp;
    }
    public List<E> getIds(){
        return ids;
    }
//    endregion
}

您可以阅读有关泛型here

的内容