简体   繁体   English

HashMap的get(key)方法仅返回最后一个条目

[英]HashMap get(key) method only returns the last entry

So I'm 100% at a loss as to what's causing something like this. 因此,我对造成这种情况的原因不知所措。 Here's the entire section of code that I'm working with 这是我正在使用的整个代码部分

public static void getBestHeuristic(int[] nextParent) {

    int bestHeuristic = 0;

    Random rndm = new Random();
    int randomIndex = 0;
    HashMap<Integer, int[]> childHuristics = new HashMap<Integer, int[]>();
    int k = 0;
    while (k < nextParent.length - 1) {
        for (int i = 0; i < (nextParent.length) - 2; i++) {
            randomIndex = rndm.nextInt(nextParent.length / 5) + i;
            int temp = nextParent[i];
            nextParent[i] = nextParent[randomIndex];
            nextParent[randomIndex] = temp;
        }
        // Assign this random set to the child
        int[] childArray = nextParent;

        int childDistance = 0;

        // Calculate the heuristic of the new current child
        for (int j = 0; j < childArray.length - 1; j++) {
            childDistance += cities[childArray[j]][childArray[j + 1]];
        }

        childHuristics.put(childDistance, childArray);

        // Display the current values added to the HashMap
        System.out.println("Distance: " + childDistance);
        System.out.println(Arrays.toString(childArray));
        System.out.println();

        // Keep track of the lowest value
        if (bestHeuristic == 0) {
            bestHeuristic = childDistance;
        } else if (childDistance < bestHeuristic) {
            bestHeuristic = childDistance;
        }

        k++;
    }
    // Display the lowest distance
    System.out.println();
    System.out.println("The best child was: ");
    System.out.println("Distance: " + bestHeuristic);
    System.out.println(Arrays.toString(childHuristics.get(bestHeuristic)));

Here is the output that I get from this: 这是我从中得到的输出:

Distance: 670
[12, 5, 6, 10, 2, 3, 4, 8, 0, 9, 7, 1, 14, 11, 13]

Distance: 680
[6, 10, 2, 12, 4, 8, 5, 0, 9, 7, 3, 14, 13, 11, 1]

Distance: 611
[2, 12, 6, 4, 5, 8, 9, 7, 0, 10, 3, 13, 14, 11, 1]

Distance: 668
[2, 4, 5, 12, 9, 7, 6, 0, 3, 13, 10, 8, 14, 11, 1]

Distance: 684
[2, 5, 9, 7, 6, 12, 4, 13, 3, 8, 0, 14, 10, 11, 1]

Distance: 634
[2, 9, 5, 12, 6, 7, 4, 8, 3, 14, 0, 11, 13, 10, 1]

Distance: 736
[9, 12, 6, 7, 2, 8, 5, 4, 3, 11, 14, 13, 0, 10, 1]

Distance: 622
[9, 7, 6, 12, 5, 4, 3, 11, 8, 14, 0, 10, 1, 13, 2]

Distance: 585
[9, 7, 5, 6, 4, 11, 3, 12, 8, 0, 10, 13, 14, 1, 2]

Distance: 554
[9, 5, 7, 4, 6, 11, 8, 12, 3, 10, 0, 14, 13, 1, 2]

Distance: 587
[9, 7, 4, 6, 11, 5, 8, 3, 12, 0, 10, 1, 13, 14, 2]

Distance: 575
[7, 4, 6, 11, 8, 9, 5, 12, 3, 1, 0, 10, 14, 13, 2]

Distance: 642
[4, 6, 7, 8, 5, 12, 9, 3, 1, 0, 11, 14, 10, 13, 2]

Distance: 634
[4, 6, 7, 5, 9, 12, 1, 8, 0, 11, 10, 13, 14, 3, 2]


The best child was: 
Distance: 554
[4, 6, 7, 5, 9, 12, 1, 8, 0, 11, 10, 13, 14, 3, 2]

To give some context to all this, basically each number in each array has a distance value associated with them. 为了给所有这些提供上下文,基本上每个数组中的每个数字都有一个与之关联的距离值。 That's what the distance value of each one represents. 这就是每个人代表的距离值。 The 'cities' array is the 2D array that holds all of those distance values. “城市”数组是保存所有这些距离值的2D数组。 It looks something like this: 看起来像这样:

0  29  82  46  68  52  72  42  51  55  29  74  23  72  46  
29   0  55  46  42  43  43  23  23  31  41  51  11  52  21  
82  55   0  68  46  55  23  43  41  29  79  21  64  31  51  
46  46  68   0  82  15  72  31  62  42  21  51  51  43  64  
68  42  46  82   0  74  23  52  21  46  82  58  46  65  23  
52  43  55  15  74   0  61  23  55  31  33  37  51  29  59  
72  43  23  72  23  61   0  42  23  31  77  37  51  46  33  
42  23  43  31  52  23  42  0  33   15  37  33  33  31  37  
51  23  41  62  21  55  23  33   0  29  62  46  29  51  11  
55  31  29  42  46  31  31  15  29   0  51  21  41  23  37  
29  41  79  21  82  33  77  37  62  51   0  65  42  59  61  
74  51  21  51  58  37  37  33  46  21  65   0  61  11  55  
23  11  64  51  46  51  51  33  29  41  42  61   0  62  23  
72  52  31  43  65  29  46  31  51  23  59  11  62   0  59  
46  21  51  64  23  59  33  37  11  37  61  55  23  59   0

The issue I'm running into, is that the 'get(key)' method of the HashMap that I have is not returning the Array associated with the key that I give it. 我遇到的问题是,我拥有的HashMap的'get(key)'方法没有返回与我提供的键关联的Array。

It says Distance: 554 [4, 6, 7, 5, 9, 12, 1, 8, 0, 11, 10, 13, 14, 3, 2] 它说距离:554 [4、6、7、5、9、12、1、8、0、11、10、13、14、3、2]

When the distance 554 is actually lined to this array: [9, 5, 7, 4, 6, 11, 8, 12, 3, 10, 0, 14, 13, 1, 2] 当距离554实际上与该数组对齐时:[9、5、7、4、6、11、8、12、3、10、0、14、13、1、2]

I don't know if there's something wrong with how I'm putting them into the HashMap or not. 我不知道如何将它们放入HashMap中是否存在问题。 I tried using a TreeMap to just force it to be sorted and get the first value, but the result was the same. 我尝试使用TreeMap强制将其排序并获取第一个值,但结果是相同的。

Thanks for your time. 谢谢你的时间。

It's because you are putting the same reference into the map multiple times. 这是因为您多次将相同的参照物放入地图中。 If you do 如果你这样做

int[] childArray = nextParent.clone();

or 要么

int[] childArray = Arrays.copyOf(nextParent, nextParent.length)

instead, you will use a fresh array each time. 相反,您每次都会使用一个新的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM