java - Salesforce 中的列表
问题描述
当我执行以下代码时,我收到一条错误消息,提示myList
找不到变量。
public class ListExample {
List<Integer> myList=new List<Integer>{1, 2, 3, 4, 5};
public static void main() {
System.debug(myList);
}
}
解决方案
您的代码存在一些问题:
语法
List<Integer> myList=new List<Integer>{1, 2, 3, 4, 5};
不正确。_ 您不能创建这样的列表。您应该使用实现List
接口的类之一,例如ArrayList
,LinkedList
等。例如,正确的语法应该是List<Integer> myList=new ArrayList<Integer>();
.变量
myList
不是静态的,并且不能在静态方法中加入非静态字段。
请参阅如何使用静态初始化块Arrays#asList
内部修复它的示例:
public class ListExample {
static List<Integer> myList;
static {
myList= Arrays.asList(1, 2, 3, 4, 5);
// this would work too
// myList = new ArrayList<>();
// for (int i = 1; i < 6; i++) {
// myList.add(i);
// }
}
public static void main(String[] args) {
System.out.println(myList); // [1, 2, 3, 4, 5]
}
}
推荐阅读
- azure - 在 Azure 上,如何重定向同一 IP 上的所有出站流量?
- r - 在R中收集多个虚拟变量作为一个分类变量
- python - 通过 Flask 应用程序访问容器中的 MongoDB 失败
- sql-server - SSRS 报告管理器未在列表视图中显示完整描述
- arrays - 速度模板:从数组的第 n 个位置开始显示
- scheme - 如何在方案中生成 lambda 列表?
- shopify - 如何在 Liquid 模板中输出当前行号?
- javascript - 如果另一个数组中不存在,如何删除数组中的对象
- java - 表单提交thymleaf和Spring boot后如何保持在同一页面上?
- sql - SQL SP - Where 子句中的 Case 语句