【问题标题】:Set Type of ArrayList Dynamically动态设置 ArrayList 的类型
【发布时间】:2015-05-08 07:45:31
【问题描述】:

目前我有这组代码:

package com.sdqn.shared.property;

import java.util.ArrayList;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Misc_ReturnValue {
    public String message;
    public int total;
    public boolean success;
    public ArrayList results;

    public Misc_ReturnValue(){
        this.success = false;
        this.total = 0;
    }
}

问题是,我需要results 来接受任何类型。我尝试遵循 here 的答案,但似乎我不明白如何在我的代码中使用它。任何人都可以向我解释如何实现这一目标吗?

【问题讨论】:

  • 我看不懂你的代码,你能解释一下你的返回类型应该在哪里吗?
  • @IvanLing,这只是一个属性类——这些信息有用吗?
  • ArrayList<T> results= new ArrayList<T>();你还想要什么?
  • @prashantthakre 我已经按照你的建议做了,但它根本不起作用。 T 未被识别为泛型类型。你可以试试看。
  • 我发布了我的答案,请检查它是否符合您的要求。

标签: java arraylist


【解决方案1】:

试试这个

public class Misc_ReturnValue<T> {
    public String message;
    public int total;
    public boolean success;
    public ArrayList<T> results = new ArrayList<T>();

    public Misc_ReturnValue(){
        this.success = false;
        this.total = 0;
    }
}

【讨论】:

  • 我如何使用这个属性?我尝试使用它们,但返回 500 错误。
猜你喜欢
  • 2018-12-03
  • 1970-01-01
  • 2022-07-05
  • 2011-02-28
  • 1970-01-01
  • 2013-02-19
  • 2015-03-04
  • 2018-02-04
  • 2014-01-13
相关资源
最近更新 更多