如何在Eclipse中的Java中将字符串添加到字符串列表

时间:2018-12-26 11:33:01

标签: java

今天我正在制作Minecraft Java mod 我使用

创建了一个自定义命令列表

public String Commands[];

但是后来我尝试使用添加一个字符串

Commands.add("");

为什么

2 个答案:

答案 0 :(得分:0)

主数组与实现List的对象ArrayList / LinkedList不同。

方法add()属于工具列表;

List<String> commands = new ArrayLidt<>();
commands.add("whatever");

String[] commands = new String[SIZE];
commands[0] = "";

答案 1 :(得分:0)

字符串数组列表实现的示例用法:

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

public class Main {

  public static void main(String[] args) {
    List<String> supplierNames = new ArrayList<>();
    supplierNames.add("sup1");
    supplierNames.add("sup2");
    supplierNames.add("sup3");
    for (String supply : supplierNames) {
      System.out.println(supply);
    }
  }
}