【问题标题】:Java array splitting and reconnecting. Lost elementsJava 数组拆分和重新连接。丢失的元素
【发布时间】:2011-08-19 09:39:12
【问题描述】:

我正在尝试实现 TSP 双桥移动,其工作原理如下: 给定城市的排列(游览),它将排列分成 4 个部分,并以不同的顺序重新连接这些部分。 例如perm=[a,b,c,d] ---> [a,d,c,b]。

在以下方法中,我有 temp[] 数组,其中包含排列的城市。我选择 3 个随机数并将数组分成 4 部分

 public void doubleBridge() {
    City[] temp = this.permArray; // it's a tour so the first element equals the last temp[0]=temp[temp.length-1]
    Random  random = new Random();      
    int pos1  = 1+ random.nextInt(temp.length/4);
    int pos2 = pos1 +  1 + random.nextInt(temp.length/4);
    int pos3 = pos2 + 1 + random.nextInt(temp.length/4);
    System.out.println("\nPositions chosen : "+pos1+" "+pos2+" "+pos3);

    City[] part1 = new City[pos1+1];
    part1 = Arrays.copyOfRange(temp, 0, pos1);

    City[] part2 = new City[pos2-pos1-1];
    part2= Arrays.copyOfRange(temp, pos1+1, pos2);

    City[] part3 = new City[pos3-pos2-1];
    part3= Arrays.copyOfRange(temp, pos2+1, pos3);

    City[] part4 = new City[temp.length-1-pos3-1];
    part4= Arrays.copyOfRange(temp, pos3+1, temp.length);

    //City[] newTemp = new City[temp.length];

    System.out.println("\npart1");
    for (City c: part1) {
        System.out.print(c.getId()+" ");
    }


    System.out.println("\npart2");
    for (City c: part2) {
        System.out.print(c.getId()+" ");
    }

    System.out.println("\npart3");
    for (City c: part3) {
        System.out.print(c.getId()+" ");
    }

    System.out.println("\npart4");
    for (City c: part4) {

        System.out.print(c.getId()+" ");
    }
    /*newTemp = concatAll(part1, part2, part3, part4);
    this.permArray = newTemp;
    this.computePermutationLength();*/
}

运行程序后打印我得到的零件。

{38, 18, 27, 2, 20, 35, 1, 42, 50, 22, 52, 36, 44, 31, 19, 33, 3, 25, 29, 49, 12, 4, 7, 30, 43, 24, 48, 45, 26, 39, 11, 15, 21, 34, 28, 8, 13, 51, 41, 17, 10, 37, 46, 32, 16, 23, 14, 5, 9、6、47、40、38,}长度:23511950

选择的职位:3 12 24

第 1 部分 38 18 27

第 2 部分 20 35 1 42 50 22 52 36

第 3 部分 31 19 33 3 25 29 49 12 4 7 30

第 4 部分 24 48 45 26 39 11 15 21 34 28 8 13 51 41 17 10 37 46 32 16 23 14 5 9 6 47 40 38

问题是丢失了 4 个元素。例如:初始排列中的元素“2”在 part1 或 part2 中都不存在。

那么问题出在哪里?

【问题讨论】:

    标签: java arrays split permutation


    【解决方案1】:

    不清楚你是想让“2”成为part1还是part2的一部分,但是看看代码:

    City[] part1 = new City[pos1+1];
    part1 = Arrays.copyOfRange(temp, 0, pos1);
    
    City[] part2 = new City[pos2-pos1-1];
    part2= Arrays.copyOfRange(temp, pos1+1, pos2);
    

    这里,pos1 的元素不是part1 的一部分,因为copyOfRange 的最后一个参数是独占。这是典型的 Java API - 例如 "012345".substring(0, 3) 会给你“012”,而不是“0123”。

    它不是part2 的一部分,因为您开始pos1+1

    基本上,这两个参数应该匹配。目前尚不清楚它们是否应该都是pos1 + 1pos1,但它们应该匹配。

    【讨论】:

      【解决方案2】:

      来自Arrays.copyOfRange docs,参数为:

      • original - 要从中复制范围的数组
      • from - 要复制的范围的初始索引,包括
      • to - 要复制的范围的最终索引,独占。 (此索引可能位于数组之外。)

      所以你应该有以下四行:(开始/从索引不应该增加)

      City[] part1 = new City[pos1+1];
      part1 = Arrays.copyOfRange(temp, 0, pos1);
      
      City[] part2 = new City[pos2-pos1-1];
      part2= Arrays.copyOfRange(temp, pos1, pos2);
      
      City[] part3 = new City[pos3-pos2-1];
      part3= Arrays.copyOfRange(temp, pos2, pos3);
      
      City[] part4 = new City[temp.length-1-pos3-1];
      part4= Arrays.copyOfRange(temp, pos3, temp.length);
      

      问候, 斯蒂芬

      【讨论】:

      • 非常感谢。这就是问题所在。现在可以正常使用了。
      • 我的荣幸。请接受答案。
      • 很抱歉,您必须等一下:P。但我会
      【解决方案3】:

      试试Arrays.copyOfRange(temp, pos1, pos2);等,不要在开始参数上加1。

      【讨论】:

        【解决方案4】:

        在上面的代码中

        City[] part1 = new City[pos1+1];
        part1 = Arrays.copyOfRange(temp, 0, pos1);
        
        City[] part2 = new City[pos2-pos1-1];
        part2= Arrays.copyOfRange(temp, pos1+1, pos2);
        

        在 part1= Arrays.copyOfRange(temp, 0, pos1); 中,数组被复制到 pos-1 而在 part2= Arrays.copyOfRange(temp, pos1+1, pos2); , pos1 处的元素被忽略,因为部分复制的数组是从 pos1+1 开始的

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-18
          • 2017-09-08
          • 2017-12-06
          • 1970-01-01
          • 1970-01-01
          • 2020-09-21
          • 1970-01-01
          • 2022-08-11
          相关资源
          最近更新 更多