【问题标题】:merge 2 ArrayLists based on a common variable基于公共变量合并 2 个 ArrayList
【发布时间】:2016-08-02 18:16:32
【问题描述】:

我有两个单独的数组列表。例如:

array1 = [[1 2 3 4 5 6]]
array2 = [[3 7 8 9 10 11 16]]

我想要一个如下所示的数组列表:

arrayResult = [[1 2 3 7 8 9 10 11 16]]

两个数组列表上有一个互变量,我想将数组 1 的第一部分 + 数组 2 的第二部分放在一个新的数组列表中。

【问题讨论】:

  • 不允许收藏,是吗??
  • 收藏是什么意思?!没有区别! @ΦXocę웃Пepeúpaツ
  • 您可以为此使用TreeSet。避免重复并以排序形式保存数据
  • 我的意思是问题的标题使用了ArrayList这个词,它在java中具有特定的含义......
  • 但是你可以把它转换成 TreeSet

标签: java join


【解决方案1】:

只需遍历第一个List 并检查第一个List 的每个索引处的值是否存在于第二个List 中。如果该值不存在,则添加第一个 List 值,如果存在则从第二个 List 复制剩余的值。

List<Integer> l1 = Arrays.asList(1,2,3,4,5,6);
List<Integer> l2 = Arrays.asList(3,7,8,9,10,11,16);
List<Integer> l3 = new ArrayList<Integer>();//joined List
for ( int i = 0; i < l1.size(); i++ ){
    int index = l2.indexOf(l1.get(i));
    if ( index == -1 ){
        l3.add(l1.get(i));
    }else{
        for ( int j = index; j < l2.size(); j++ ){
            l3.add(l2.get(j));
        }
        break;
    }
}

【讨论】:

  • 您也可以在没有 for 循环的情况下执行 l3.addAll(l2.subList(index, l2.size())),并在 l1 上使用增强的 for 循环。
【解决方案2】:

你可以这样做:

List<Integer> a1 = Arrays.asList(1, 2, 3, 4, 5, 6);
List<Integer> a2 = Arrays.asList(3, 7, 8, 9, 10, 11, 16);
ArrayList<Integer> merged = new ArrayList<>();
for(Integer val: a1){
    if(val == a2.get(0)){
        for(Integer val2: a2){
            merged.add(val2);
        }
        break;
    }
    merged.add(val);
}
System.out.println(merged);

您必须确保第二个列表至少包含 1 个值,如果列表尚未排序,则必须对其进行排序。

【讨论】:

    【解决方案3】:

    遍历array1的元素,每次检查它们是否在array2中。如果没有,添加它们并继续 array1 的循环。如果它存在于array2中,则停止循环array1并在该索引处开始循环array2。

    这应该会有所帮助..

    Boolean found = false;
    integer foundInd = 0;
    Boolean useArray1 = false;
    if (array1.length >= array2.length)
        useArray1 = true
    
    For (integer cnt1 = 0; cnt1 < array1.length; cnt1 ++)
    {
        For (integer cnt2 = 0; cnt2 < array2.length; cnt2 ++)
        {
            if (array1[cnt1] == array2[cnt2])
            {
                found = true;
                foundInd = cnt2;
            }
        }
        if (useArray1 == false)
            Array2[cnt] = Array1[cnt];
        if (found == true)
            break;
    }
    
    For (integer cnt2 = foundInd; cnt2 < array2.length; cnt2 ++)
    {
        if (useArray1 == true)
            useArray1[foundInd + cnt2] = array2[cnt2];
    }
    

    旁注-我没有创建一个新数组来存储值,而是使用了array1 或array2,具体取决于哪个更长。它已经有正确的长度,并且至少有一半的数据在正确的位置已经是正确的数字。

    【讨论】:

      【解决方案4】:

      如果你使用集合,那么你可以将两个列表放在一起,并且更容易找到共同的元素......

      看看这个例子:

      public static void main(String[] args) {
      List<Integer> a1 = Arrays.asList(1, 2, 3, 4, 5, 6);
      List<Integer> a2 = Arrays.asList(3, 7, 8, 9, 10, 11, 16);
      List<Integer> common = new ArrayList<>();
      int commonw;
      // merge both and find common Element.class..
      // IllegalMonitorStateException must be one
      common.addAll(a1);
      common.retainAll(a2);
      commonw = common.get(0);
      common.clear();
      
      // fill the common elements
      for (int i = 0; i < a1.indexOf(commonw); i++) {
          common.add(a1.get(i));
      }
      for (int i = a2.indexOf(commonw) + 1; i < a2.size(); i++) {
          common.add(a2.get(i));
      }
      System.out.println(common);
      }
      

      【讨论】:

        【解决方案5】:

        如果你有java8你可以试试

        public List<Integer> merge(List<Integer> first, List<Integer> second) {
            Integer integerToMergeAt = first.stream().filter(i -> second.indexOf(i) >= 0).findFirst().get();
            return Stream.concat(first.stream().limit(first.indexOf(integerToMergeAt)),
                    second.stream().skip(second.indexOf(integerToMergeAt))).collect(Collectors.toList());
        }
        

        【讨论】:

          【解决方案6】:

          这是合并排序问题。尝试应用归并排序算法。检查这个以获得更清晰和代码快照:Merge Sort

          试试这个:

          List<Integer> list1 = Arrays.asList(array1); 
          
          List<Integer> list2 = Arrays.asList(array2); 
          
          set.addAll(list1);
          set.addAll(list2);
          
          yourResult = set.toArray();
          

          【讨论】:

          • 问题不在于排序 - 它涉及使用每个列表的部分,在由相等值的索引定义的交叉点处连接它们。
          猜你喜欢
          • 2017-06-02
          • 2016-10-29
          • 2015-08-27
          • 1970-01-01
          • 2022-06-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-13
          相关资源
          最近更新 更多