【问题标题】:Returning a linked list containing keys from another Linked List返回包含来自另一个链接列表的键的链接列表
【发布时间】:2016-11-10 07:27:43
【问题描述】:

我有以下问题。我有一个链接列表或位置列表,其中包含名为 Entry 的对象。每个条目都存储一个键和一个值对。我想制作另一个链接列表,以便从该列表中仅获取键。我想出了一种方法来做到这一点,但是由于某种原因,当我打印密钥列表时,它们的添加顺序并没有表示出来。我有以下两种方法:

public PositionalList1<K> keySet() //I prnted position by position while adding and it worked. I also tested addAfter with positionalList1 class and it worked
{
    PositionInterface<Entry> iterator = map.first(); //gets first position in the Entry List
    PositionInterface<K> first = keyList.addFirst((K)iterator.getData().getKey()); //adds to list containing just keys the key from the Entry list(getData is just a method that returns whatever object is stored at the node, in this case a Entry Object)
    iterator = map.after(iterator); //go to next node in Entry list

    for(int i=0;i<size-1;i++) //get rest of keys
    {
        PositionInterface<K> p = keyList.addAfter(first,(K)iterator.getData().getKey());
        iterator = map.after(iterator);

    }
    return keyList;
}

public void printKeySet(PositionalList1 list) //print key list
{
    PositionInterface p = list.first();
    for(int i=0; i<list.size();i++)
    {
        System.out.println("Key : " + p.getData());
        p = list.after(p);
    }
}

keySet() 方法返回仅包含键的列表,而 printKeySet 获取 KeySet() 的结果并打印整个键列表。我已经使用以下主程序对此进行了测试:

OrderedMapL<Integer,String> map2 = new OrderedMapL<Integer,String>();
    map2.put(2,"A");//adds (2,A)
    map2.put(5,"B");//adds(5,B)
    map2.put(1,"C");//adds(1,C)
    map2.put(4,"D");//adds(4,D)
    map2.put(3,"E");//adds(3,E)
    map2.remove(2); //removes (2,A)

这导致了 (1,C) (2,A) .. 等的有序列表,并且条目本身按此顺序打印良好。调用以下内容时会出现问题:

PositionalList1<Integer> keyList = map2.keySet();
map2.printKeySet(keyList);

由于某种原因,键按顺序打印:1,5,4,3 而不是 1,3,4,5,我不知道为什么。任何帮助将不胜感激。

【问题讨论】:

  • 您使用的所有类看起来都像专有的自定义类。我们不知道他们做什么以及他们是如何工作的。为什么不使用标准的 Java 集合?
  • 这是一个数据结构类,我希望在其中编写自己的抽象数据类型。与节点列表本身相关的所有辅助功能都可以工作并且已经过测试。 addAfter() 只是在参数化位置之后添加一个位置。这是非常奇怪的,因为当我在 keySet() 中一一打印位置时,在添加位置时正确打印了位置。然而在 printKeySet() 中没有表示这个顺序。
  • after() 也是另一个节点列表函数,它返回参数化后的位置。该函数的名称几乎可以告诉您它的作用。

标签: java linked-list hashmap key-value nodelist


【解决方案1】:

问题出在这一行:

PositionInterface<K> p = keyList.addAfter(first,(K)iterator.getData().getKey());

您不应该在first 之后添加。这就是为什么你会得到错误的顺序。

如果键是1, 3, 4, 5,那么您将像这样添加它:

  1. 添加 1 作为第一个元素 1
  2. 在第一个元素后添加 3 1 3
  3. 在第一个元素后添加 4 1 4 3
  4. 在第一个元素后添加 5 1 5 4 3

这是因为您在第一个元素之后添加它。

如果我正确理解了您的代码,您应该像这样更改它:

// I suspect that #addFirst method returns added list element
PositionInterface<K> last = keyList.addFirst((K) iterator.getData().getKey());
iterator = map.after(iterator);

for(int i = 0; i < size - 1; i++)
{
    // I suspect that #addAfter method returns added list element
    PositionInterface<K> last = keyList.addAfter(last, (K) iterator.getData().getKey());
    iterator = map.after(iterator);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 2021-08-01
    • 2019-11-21
    • 2013-02-01
    • 1970-01-01
    相关资源
    最近更新 更多