本文翻译自:How to initialize List object in Java?

I can not initialize a List as in the following code: 我无法初始化List,如下面的代码所示:

List<String> supplierNames = new List<String>();
supplierNames.add("sup1");
supplierNames.add("sup2");
supplierNames.add("sup3");
System.out.println(supplierNames.get(1));

I face the following error: 我面临以下错误:

Cannot instantiate the type List<String> 无法实例化类型List<String>

How can I instantiate List<String> ? 如何实例化List<String>


#1楼

参考:https://stackoom.com/question/uCgE/如何在Java中初始化List-String-对象


#2楼

You will need to use ArrayList<String> or such. 您将需要使用ArrayList<String>等。

List<String> is an interface. List<String>是一个接口。

Use this: 用这个:

import java.util.ArrayList;...List<String> supplierNames = new ArrayList<String>();

#3楼

List is an interface, and you can not initialize an interface. List是一个接口,您无法初始化接口。 Instantiate an implementing class instead. 实际上实例化一个实现类。

Like: 喜欢:

List<String> abc = new ArrayList<String>();
List<String> xyz = new LinkedList<String>();

#4楼

List is an Interface , you cannot instantiate an Interface, because interface is a convention, what methods should have your classes. List是一个接口 ,你不能实例化一个接口,因为接口是一个约定,你的类应该有哪些方法。 In order to instantiate, you need some realizations(implementations) of that interface. 为了实例化,您需要该接口的一些实现(实现)。 Try the below code with very popular implementations of List interface: 使用非常流行的List接口实现尝试以下代码:

List<String> supplierNames = new ArrayList<String>();

or 要么

List<String> supplierNames = new LinkedList<String>();

#5楼

Depending on what kind of List you want to use, something like 取决于您要使用的List类型,类似于

List<String> supplierNames = new ArrayList<String>();

should get you going. 应该让你去。

List is the interface, ArrayList is one implementation of the List interface. List是接口,ArrayList是List接口的一个实现。 More implementations that may better suit your needs can be found by reading the JavaDocs of the List interface . 通过阅读List接口的JavaDocs,可以找到更适合您需求的更多实现。


#6楼

List is an Interface . 列表是一个接口。 You cant use List to initialize it. 你不能使用List来初始化它。

  List<String> supplierNames = new ArrayList<String>();

These are the some of List impelemented classes, 这些是列中的一些实例化的类,

ArrayList, LinkedList, Vector

You could use any of this as per your requirement. 您可以根据自己的要求使用其中任何一项。 These each classes have its own features. 这些每个类都有自己的功能。

如何在Java中初始化List <String>对象?相关推荐

  1. java如何实例化集合_如何在java中实例化一个Queue对象?

    Queue是一个接口,这意味着你不能直接构造一个Queue . 最好的select是构造一个已经实现Queue接口的类,如下所示: AbstractQueue , ArrayBlockingQueue ...

  2. 初始化list java_如何在Java中初始化List 对象?

    如果你检查List http://docs.oracle.com/javase/6/docs/api/java/util/List.html的API,你会注意到: Interface List 作为一 ...

  3. queue初始化java,如何在java中实例化一个Queue对象?

    When I try: Queue q = new Queue(); the compiler is giving me an error. Any help? Also, if I want to ...

  4. java获取语言_如何在java中获取语言环境对象?

    我在框架Spring和Liferay中使用Java. 使用liferay我知道如何获得一个语言环境(对象有一些信息:语言,国家......),但现在我在一个没有与liferay连接的java类,我不知 ...

  5. 如何在Java中比较日期? [重复]

    本文翻译自:How to compare dates in Java? [duplicate] This question already has answers here : 这个问题已经在这里有了 ...

  6. 如何在Java中创建一个新的List

    本文翻译自:How to make a new List in Java We create a Set as: 我们创建一个Set为: Set myset = new HashSet() How d ...

  7. 如何在java中调用js方法

    [java] view plain copy/* * 加载脚本引擎,并在java中调用js方法 */ public void test2() { ScriptEngineManager manager ...

  8. java 中覆 写tostring_如何在Java中正确覆盖toString()?

    如何在Java中正确覆盖toString()? 听起来有点愚蠢,但我需要帮助我的toString()方法,这是非常irking. 我尝试在网上查找,因为toString是搞砸了,"没有找到K ...

  9. java 正则表达式 开头_如何在Java中修复表达式的非法开头

    java 正则表达式 开头 您是否遇到过这个令人难以置信的错误,想知道如何解决它? 让我们仔细阅读一下,研究如何解决表达式Java非法开头错误. 这是一个动态错误,这意味着编译器会发现某些不符合Jav ...

最新文章

  1. 用C语言实现扫雷小游戏(附上思路+项目展示+源代码)
  2. Github连击200天(1)——ShowCase
  3. Java-集合的简单介绍
  4. python docx 合并文档 图片_不再为处理PDF烦恼,python处理操作PDF全攻略
  5. Teams 的逻辑架构与语音解决方案 - Official Posters Download
  6. Docker 入门尝鲜
  7. 【API进阶之路】做OCR文字识别,谁说必须要有AI工程师?
  8. ubuntu设置apt-get永久使用http代理
  9. 剑指offer 面试题63. 股票的最大利润
  10. maven Could not resolve dependencies
  11. 如何打开SAR 文件
  12. Android中矢量图形的相关知识
  13. python 读取txt文件并在txt每行文件后面增加内容
  14. XINS Web服务框架
  15. 友盟集成微信授权登录,切换微信账号,登录无法切换微信信息问题
  16. AD19滴泪添加与删除
  17. teamspeak3快速安装
  18. 华为畅享20 pro 和华为畅享Z 的区别 哪个好
  19. 亿发软件:智能中医药信息管理系统,加速智慧药房信息化建设
  20. 联想小新使用Alt+insert快捷键

热门文章

  1. 收集小型机snap日志文件步骤
  2. SpringMVC -- 梗概--源码--贰--RestFul收参(了解) @PathVariable
  3. 【Python之旅】第四篇(二):Python异常处理与异常捕捉
  4. zookeeper源码分析之leader选举
  5. 百万数据查询优化技巧三十则
  6. javascript比较日期
  7. Ubuntu移除mysql后重新安装
  8. Jeesite 自定义api接口 404 访问不到页面
  9. Centos7安装maven过程
  10. 面向对象设计思想_重要_2