我有兩個位元組陣列,它們可能非常大,甚至可能有 700500 個值。
array2總是大于array1,并且它基本上包含與 中相同的資料array1,但隨機添加,例如:
int[] array1 = {1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 0, 0, 0};
int[] array2 = {1, 1, 1, 2, 7, 7, 2, 2, 2, 2, 1, 2, 3, 2, 2, 3, 3, 4, 7, 2, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 8, 4, 1, 1, 7, 7, 8, 8, 9, 9, 0, 0};
我需要一個array3,它需要與 arrays2 具有相同的大小。它將顯示添加位置的確切索引,因此對于此示例,它將是:
int[] array3 = {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
(0 = 與 array1 相同,1 = 與 arrays1 不同)
我希望獲得與在“Beyond Compare”應用程式中獲得的結果相同的結果:
https://i.ibb.co/yX6YCsp/Diff.jpg
但要獲取您在右側窗格中的圖片中看到的紅色標記的索引。
我需要用 C# 撰寫它。
非常感謝您對此的所有幫助!
uj5u.com熱心網友回復:
您正在尋找的是diff algorithm,這不是那么容易做好。我建議使用Google 的 DiffMatchPatch 庫而不是自己撰寫它,但如果你想走那條路,維基百科文章應該是一個很好的起點,可以了解更多關于那個特定兔子洞的資訊。
uj5u.com熱心網友回復:
您可以比較兩個陣列之間的每個元素。如果有匹配項,則添加一個0toarray3并查看兩個陣列中的下一個元素。如果沒有匹配項,則添加一個1toarray3并查看 中的下一個元素array2。如果array1沒有更多元素,則繼續添加1直到array2沒有更多元素。
int[] array1 = {1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 0, 0, 0};
int[] array2 = {1, 1, 1, 2, 7, 7, 2, 2, 2, 2, 1, 2, 3, 2, 2, 3, 3, 4, 7, 2, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 8, 4, 1, 1, 7, 7, 8, 8, 9, 9, 0, 0};
int index1 = 0;
int index2 = 0;
int[] array3 = new int[array2.Length];
while (index2 < array2.Length)
{
if (index1 >= array1.Length)
{
array3[index2] = 1;
index2 = 1;
}
else if (array1[index1] == array2[index2])
{
array3[index2] = 0;
index1 = 1;
index2 = 1;
}
else
{
array3[index2] = 1;
index2 = 1;
}
}
foreach (int i in array3)
{
Console.Write(i.ToString() " ");
}
輸出:
0 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/367586.html
下一篇:如何將帶有陣列的物件轉換為陣列
