当前位置: 首页 > 知识库问答 >
问题:

使用迭代器获取列表值Java

翟浩穰
2023-03-14

我正在尝试使用列表迭代器来遍历一个链表,并根据存储在那里的整数值对下一个节点进行一些操作/检查,但我的代码中出现了一些错误。我想我不明白iterator.next()正在返回什么(一些E对象,但我不知道如何从它中访问我想要的值)编辑器希望我做一些如下所述的转换。它摆脱了错误,但我不知道这是否是处理问题的安全方法,或者它是否具有我正在寻找的行为。请解释为什么我会得到错误,以及是否有一个好的方法来处理这个问题。

        LinkedList<Integer>[] hash = new LinkedList[list.size()];
        hash = remo.createHash(hash, list.size());
        ListIterator iterator = list.listIterator(0);

        // use the value of the integer stored at the next Node as its hash
        // and add the same value to the linked list at that bucket
        int i = 0;
        while(iterator.hasNext()){
            hash[iterator.next()].add(iterator.next());
            i++;
        }

        // reset iterator to beginning of list
        iterator = list.listIterator(0);

        // if the hash bucket corresponding to the value at that node has more than
        // one item in its list, remove that node from the list.
        while(iterator.hasNext()){
            if(hash[iterator.next()].size()>1){
                iterator.remove(iterator.next());
            }
        }

createHash初始化数组中的每个链表,remo是我的类的一个实例。

编辑器希望我将iterator.next()转换为int hash[iterator.next()],并希望我将其转换为in. add(iterator.next())。

示例:hash[(int)iterator.next()]hash[(int)iterator.next()]. add((整数)iterator.next());

共有2个答案

邢骏
2023-03-14

数组和泛型不能混合。只需使用列表

List<List<Integer>> hash = new LinkedList<List<Integer>>(list.size());

华安民
2023-03-14

链接列表

此行有问题,因为http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays

You cannot create arrays of parameterized types. For example, the following code does not compile:

List<Integer>[] arrayOfLists = new List<Integer>[2];  // compile-time error

Because:

Object[] stringLists = new List<String>[];  // compiler error, but pretend it's allowed
stringLists[0] = new ArrayList<String>();   // OK
stringLists[1] = new ArrayList<Integer>();  // An ArrayStoreException should be thrown,
                                            // but the runtime can't detect it.
If arrays of parameterized lists were allowed, the previous code would fail to throw the desired ArrayStoreException.

因此,您创建的列表数组不使用泛型(因为您无法创建参数化类型的数组),因此它存储对象(它不知道您实际计划存储的类型)。您可能应该使用数组列表而不是数组来解决此问题,如下所示:

List<List<Integer>> listOfLists = new ArrayList<List<Integer>>(list.size());

//example usage
listOfLists.add(new LinkedList<Integer>()); 
for(List<Integer> currList : listOfLists)
{
     ...
}
 类似资料:
  • 我将“User class”类型的变量转换为JsonNode,如下所示: 我需要迭代地循环该节点的每个元素,并获取其键和值,例如:“name”:John,“lastname”:H等等 但是当我做iterator.next()时,我直接得到约翰值,没有键。我需要同时得到键和值。我如何在JsonNode类型的迭代器中访问键?我做错了什么,或者有没有更好的方法来迭代这些属性?

  • 问题内容: List rateList = guestList.stream() .map(guest -> buildRate(ageRate, guestRate, guest)) .collect(Collectors.toList()); 在上面的代码中,可以通过内部方法的索引。我在构建时还需要传递索引,但无法通过获取索引。 问题答案: 您尚未提供的签名,但是我假设您希望首先传递元素的索引

  • 在上面的代码中,是否可以在方法内部传递的索引。在构建时,我还需要传递索引,但无法使用获取索引。

  • 问题内容: 我有一个清单: 要从此列表中获取,有两种方法: 1。 2。 我的问题是,哪一个内存效率高且迭代速度快? 问题答案: 他们做同样的事情- 增强的循环对于长版本来说只是语法糖(对于可迭代对象;对于数组,它稍有不同)。除非您明确需要迭代器(例如,调用),否则我将使用第一个版本。 有关编译器执行的准确转换的更多详细信息,请参见Java语言规范的14.14.2节。

  • 我有以下LinkedHashMap声明。 我的观点是我如何迭代这个哈希图。我想在下面执行此操作,对于每个键获取相应的数组列表,并根据该键逐个打印数组列表的值。 我试过了,但只得到返回字符串,

  • 我对RxJava/RxAndroid还不熟悉,但我一直坚持使用我的用例。 我尝试迭代一个