java - 在构造函数中创建具有数组特征的数据结构
问题描述
我想创建一个像数组一样工作的特殊列表数据结构,因为它就像一个具有值 x[0]、x[1] 的列表......任何建议将不胜感激。
我知道我的所有代码都不完美,我只想弄清楚如何解决我在下面概述的一个问题。这是我拥有的一些代码:
public class SpecialList {
int[] specialList;
int lengthList;
public SpecialList(int x[]) {
this.lengthList = x.length;
this.specialList = new int[lengthList];
this.specialList = x;
for (int i=0; i<lengthList; i++) {
this.specialList[i] = x[i];
}
}
public SpecialList(SpecialList w) {
this.specialList = w.specialList;
}
public SpecialList doSomething(SpecialList y) {
int len = y.lengthList;
//The line below is an example to show the error I get
System.out.println(y[0]);
//Do some other stuff to the list y
return y;
}
//I test the code with this
public static void main(String[] args) {
SpecialList y = new SpecialList(new int[] {14, 17, 30});
SpecialList z = x.doSomething(y);
}
但是,当我尝试使用y[i]
类似System.out.println(y[0]);
代码行的东西时,我收到错误“需要数组,但找到了 SpecialList”。
' lengthList
' 有效,但获取 的单个值y[i]
,列表无效。我无法弄清楚我的构造函数出了什么问题,因为它不能按我想要的方式工作。
解决方案
推荐阅读
- java - 使用 JDBC 和 MySQL 插入 1000 条具有关系的记录并忽略重复项
- r - 在 R 中逐行累积
- python - 根据另一个 df python pandas 更新 df 列值
- python - 在 python 中遍历 SQL 表
- apache - 如果存在具有特定值的标头,则使用 .htaccess 仅允许请求
- java - 在 Servlet 中生成线程
- maven - artifactId Selenium-java.jar 从 3.3.1 更改为 3.4 导致我的 maven 项目中出现大量“无法访问”错误
- performance - 如何分解scala.js JS输出的大小
- entity-framework - 实体框架使用参数执行存储过程
- javascript - 一年后,仍然在单元测试、集成测试和 E2E 测试中苦苦挣扎