开发者

Multiple parameters in List

I want to do something like this

List<Integer, String, String>

I want to be able to iteratively retrieve either of these three parameters. How can I go abou开发者_开发知识库t it? Thanks


What you need is a Tuple class:

public class Tuple<E, F, G> {
    public E First;
    public F Second;
    public G Third;    
}

Then you can iterate over the list of the tuple, and look at each entry in the tuple.

List<Tuple<Integer, String, String> listOfTuple;
for (Tuple<Integer, String, String> tpl: listOfTuple){
    // process each tuple
    tpl.First ... etc
}


You can create a wrapper class which holds these three variables and then store that wrapper-object in the list.

For instance

    public class ListWrapperClass {
    private String firstStringValue;
    private String secondStringValue;
    private Integer integerValue;

    public String getFirstStringValue() {
        return firstStringValue;
    }

    public void setFirstStringValue(String firstStringValue) {
        this.firstStringValue = firstStringValue;
    }

    public String getSecondStringValue() {
        return secondStringValue;
    }

    public void setSecondStringValue(String secondStringValue) {
        this.secondStringValue = secondStringValue;
    }

    public Integer getIntegerValue() {
        return integerValue;
    }

    public void setIntegerValue(Integer integerValue) {
        this.integerValue = integerValue;
    }
}

and then use List<ListWrapperClass>.


You can use a List<Object> and then cast whatever you retrieve based on the index, but you may just consider creating a class that holds these three things.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜