java - 导航 ArrayList
问题描述
我在 java 上实现 QM 方法时遇到问题。
我在导航 2D ArrayList 时遇到问题。我在代码中的目标是让 ArrayList 的第一个元素成为地址,并且每次配对另一个二进制文件时,我都会找到该地址并添加配对二进制文件的地址。
有没有办法使用 indexOf 函数来查找 2D 数组中第一个元素的索引?
例子。我的 2D ArrayList 命名表是 [4][3][7];[7][4];[8][3];[9];
我想将 int 5 添加到 [4] 所在的行。所以我的代码是 table.get(table.indexOf(4)).add(5) 但它一直说索引是-1。请帮忙谢谢
解决方案
所以考虑...
ArrayList<ArrayList<Integer>> myList = new ArrayList<ArrayList<Integer>>();
// populate list
Integer index = myList.indexOf(4);
// index is -1 because 4 is not of type: ArrayList<Integer>
如果您需要基于整数索引向 ArrayList 添加值,我会考虑切换到不同的容器类型,例如:
HashMap<Integer, ArrayList<Integer>>
另一种方法是不使用 indexOf ... 而是编写自己的函数来循环 0 到 myList.size() 寻找 myList.get(loopIndex).get(0) = 要找到的地址,然后返回外部 ArrayList 索引发现的地方
推荐阅读
- angular - Ngx-datatable 不显示数据
- microsoft-graph-api - Microsoft Graph Explorer 中的列表标签需要哪些权限?
- firefox - 我可以忽略 Firefox DevTools SameSite cookie 属性警告吗?
- python - 如何将 input() 发送到用户可以访问和更改的文件?
- python - 如何通过 Python 从 MCCGQ02HL 传感器读取数据?
- android-mediaplayer - 媒体播放器和外部存储
- c - 在 cs50 的过滤器上出现分段错误
- flutter - Flutter:有没有办法将参数“ID”从页面传递给对话框?
- java - 无法通过 RMI 协议连接到 Maximo 服务器
- java - 内部联接和通过对象获取子类(表)实体之间有什么区别