java 列表中的多个参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3979748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:14:59  来源:igfitidea点击:

Multiple parameters in List

java

提问by y2p

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 about it? Thanks

我希望能够迭代地检索这三个参数中的任何一个。我该怎么办?谢谢

采纳答案by jjnguy

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
}

回答by Yrlec

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>.

然后使用List<ListWrapperClass>.

回答by Daniel DiPaolo

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.

您可以使用 aList<Object>然后根据索引转换您检索到的任何内容,但您可能只考虑创建一个包含这三个内容的类。