【问题标题】:What is List<?> in Java (Android)? [duplicate]Java (Android) 中的 List<?> 是什么? [复制]
【发布时间】:2011-12-29 07:07:29
【问题描述】:

可能重复:
What is Type<Type> called?
What does List<?> mean in java generics?

package com.xyz.pckgeName;

import java.util.ArrayList;
import java.util.List;

public class Statement {

// public String
public String status;
public String user_id;
public String name;
public String available_balance;
public String current_balance;
public String credit_card_type;
public String bank_id;
public List<Statements> statements = new ArrayList<Statement.Statements>();

public class Statements {
    public String month;
    public String account_id;
    public String user_id;
    public String id;
    public List<Transaction> transactions = new ArrayList<Transaction>();
}
}

谁能解释一下这两个语句的意思

public List<Statements> statements = new ArrayList<Statement.Statements>();

public List<Transaction> transactions = new ArrayList<Transaction>();

【问题讨论】:

标签: java android generics


【解决方案1】:

这是java中的Generics

List&lt;?&gt;本质上翻译为“未知数列表”,即未知类型的列表。 ? 被称为Wildcard(本质上意味着未知)。


public List<Statements> statements = new ArrayList<Statement.Statements>();

这实际上创建了一个只接受Statement.StatementsList。您想要添加到statementsStatement.Statements 之外的任何内容都会产生编译错误。这同样适用于public List&lt;Transaction&gt; transactions = new ArrayList&lt;Transaction&gt;();。这意味着Listbounded 类型Statement.Statements(在statements 变量上)。

【讨论】:

  • 我们可以简单地说它存储了Statement中嵌套类中的Statements对象吗?'
  • 感谢您的回复,让我阅读文档并举一些例子,我赞成并接受答案:-)
【解决方案2】:

这是泛型的使用。您正在声明一个 Statement 对象或 Transaction 对象的列表。

查看维基百科了解更多信息

http://en.wikipedia.org/wiki/Generics_in_Java

【讨论】:

    【解决方案3】:

    您应该阅读Generics 以了解这一点。 List 是一个原始类型,可以是对象的列表,例如字符串、包装器和用户定义的对象。

    public List<Statements> statements = new ArrayList<Statement.Statements>();
    

    上面的代码说 statements 是对 Statement Objects 的 ArrayList 的引用。它确实指定列表将包含 Statements 对象,这些对象是 Statement 类的内部类。

    List&lt;?&gt;用来表示未知类型的列表。

    【讨论】:

      【解决方案4】:

      statements 列表只能包含 Statements 对象。它被称为泛型。检查此以获取示例程序。

      http://www.java2s.com/Code/Java/Language-Basics/Asimplegenericclass.htm

      【讨论】:

        猜你喜欢
        • 2011-10-15
        • 2016-12-24
        • 2011-04-07
        • 1970-01-01
        • 1970-01-01
        • 2012-10-03
        • 2010-12-19
        • 2010-10-08
        • 2012-04-12
        相关资源
        最近更新 更多