首页 > 解决方案 > 导航 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。请帮忙谢谢

标签: javaarraylist

解决方案


所以考虑...

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 索引发现的地方


推荐阅读