简体   繁体   English

查找相同数字并用新数字替换其他数字

[英]Finding same numbers and replacing others with new ones

I have 2 arrays (both containing 2 strings each) one contains serial numbers from a USB. 我有2个数组(每个数组都包含2个字符串),其中一个数组包含USB的序列号。 the other contains serial numbers from a text file. 另一个包含来自文本文件的序列号。 I was able to retrieve them both successfully. 我能够成功检索它们。 So here's my problem: I need to compare them to each other, find exactly one serial number that differs, and replace it. 所以这是我的问题:我需要将它们相互比较,找出一个完全不同的序列号,然后替换它。 like this: 像这样:

Contents (Dummy Serial numbers)
     ________
USB | A | B

TXT | B | C

As you can see, the USB and TXT array both contain one of the same serial number (B). 如您所见,USB和TXT阵列都包含相同序列号(B)之一。 That part is easy; 那部分很简单; I however need to write code to see that C != A and then I need A to replace C. 但是,我需要编写代码以查看C!= A,然后需要A来替换C。

I tried this: 我尝试了这个:

for (int x = 0; x < 2; x++)
{
     for (int y = 0; y < 2; y++)
     {
          //checks for same serial number
          if (m_AttachedUSB[x] == m_Existing_Serial_Numbers[y])
          {
              //found one
              IntOnlyOne++;
              //we want this one to stay beacause it has a serial number 
              //that matches one in the .txt file
              m_ChachedUSB = m_AttachedUSB[x];
          }
     }
}

This however only finds the serial numbers that are similar. 但是,这只会找到相似的序列号。 How do I replace the ones that are different? 如何更换不同的?

If I understood you right: 如果我没看错:

List<int> usb = new List<int> {1,2,4,7,8};
List<int> text = new List<int> {1,2,3,4,5};

usb.Intersect(text).Union(usb);

That will return a list containing {1,2,4,7,8}. 那将返回一个包含{1,2,4,7,8}的列表。

The intersect method gives you all items that are contained by both lists. 相交方法为您提供了两个列表都包含的所有项目。 In this case {1,2,4}. 在这种情况下,{1,2,4}。 The union method will join all items from usb when they are not yet available. 当USB中的所有项目尚不可用时,union方法将加入它们。 In this case {7,8}. 在这种情况下,{7,8}。

The following creates a set of all the serial numbers in the USB list, then loops through the TXT list, either removing a matching item from the USB set or noting the index of an "old" item in the TXT list. 以下内容将创建USB列表中所有序列号的集合,然后循环遍历TXT列表,从USB集合中删除匹配的项,或者注意TXT列表中“旧”项的索引。

It then replaces the "old" items with the remaining items in the USB set, which should be now just be the "new" items. 然后,它将“旧”项目替换为USB集中的其余项目,现在应该只是“新”项目。

This assumes that both lists are the same length, and that the USB list contains no duplicates. 这是假定这两个列表的长度相同,且USB列表中没有重复。

HashSet<string> usbSNs = new HashSet<string>(m_AttachedUSB); // { "A", "B" }
List<int> txtOldIndices = new List<int>();                   // { }

// Remove existing items from USB set, note indices of old items in TXT list.
for (int i = 0; i < m_CachedUSB.Length; i++)
{                                         // First iteration  Second iteration
    if (!usbSNs.Remove(m_CachedUSB[i]))   // Now { "A" }      Still { "A" }
        txtOldIndices.Add(i);             // Still {}         Now { 1 }
}
// At this point you may want to check usbSNs and txtOldIndices
// have the same number of elements.

// Overwrite old items in TXT list with remaining, new items in USB set.    
foreach(var newSN in usbSNs)
{
    m_CachedUSB[txtOldIndices[0]] = newSN;    // Now [ "B", "A" ]
    txtOldIndices.RemoveAt(0);                // Now { }
}

Essentially, this is a way of copying m_AttachedUSB over m_CachedUSB , while preserving the locations of items which are common to both, which is what I assumed you were going for. 从本质上讲,这是一种在m_CachedUSB上复制m_AttachedUSBm_CachedUSB ,同时保留了两者都通用的项目的位置,这就是我所想的。

With only 4 items in your lists you can keep it simple: 列表中只有4个项目,您可以保持简单:

if(usbArray[0] == textArray[0])
  usbArray[1] = textArray[1];
else if(usbArray[0] == textArray[1])
  usbArray[1] = textArray[0];
else if(usbArray[1] == textArray[0])
  usbArray[0] = textArray[1];
else if(usbArray[1] == textArray[1])
  usbArray[0] = textArray[0];

Basically, change the two that are different. 基本上,更改两个不同的地方。

And a second solution: 第二种解决方案:

for(int i=0; i<=1; i++)
  for(int j=0; j<=1; j++)
     if(usbArray[i] == textArray[j])
       usbArray[(i+1)%2] = textArray[(j+1)%2];

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

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