ListIterator.PreviousIndex 返回一个单元素整数数组而不是 Int?

问题描述

我正在编写一个程序,其中有一次,我正在遍历“存储桶”列表(嵌套的字符串列表)。这样做时,我需要跟踪存储桶中特定元素的索引。

为此,我求助于创建第二个 2D 列表对象,其中每个内部列表存储需要跟踪的元素的 int 索引值。

因为第二个列表稍后会在我的代码中进行排序,所以我想将每个内部列表的第一个元素专用于存储它负责跟踪其中元素的存储桶的索引。

我想我可以通过使用列表迭代器的“prevIoUsIndex()”方法来做到这一点,因为它说它返回一个 int 值。但相反,它返回一个包含我想要的 int 的 1 元素 int 数组,而不仅仅是 int 本身。这完全弄乱了我的代码,当它以这种方式存储时,我似乎无法访问该元素。

A debugging output showing the enemies list with 3 levels instead of 2 for the first element of the nested list

A debugging output from later in the execution showing that elements added later do not get entered in list form

A compiler error in a different section of code caused by the remove method thinking it is being given an object rather than an integer index

public static ArrayList<List<Integer>> isItPossible(String word,LinkedList<List<String>> buckets) {
        ListIterator<List<String>> bucketIterator;
        ListIterator<String> wordIterator;
        List<String> bucket;
        ArrayList<List<Integer>> enemies;
        
        enemies = new ArrayList<List<Integer>>();
        bucketIterator = buckets.listIterator();
        
        while(bucketIterator.hasNext()) {
            bucket = bucketIterator.next();
            wordIterator = bucket.listIterator();
            enemies.add(new ArrayList<Integer>());
            enemies.get(enemies.size() - 1).add(bucketIterator.prevIoUsIndex()); //adds a list with the element I need,but I just need the element,not the list????

        while(wordIterator.hasNext()) {
            if(!isCompatible(word,wordIterator.next())) {
                enemies.get(bucketIterator.prevIoUsIndex()).add(wordIterator.prevIoUsIndex());
            }
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)