C# 如何在两个数组之间跳过相同的值

C# 如何在两个数组之间跳过相同的值,c#,arrays,if-statement,while-loop,C#,Arrays,If Statement,While Loop,我一直在“排序”2个不同的数组。 我的目标是去掉array1和array2中包含的数字 以下是一个例子: int [] arr1 = {1,2,3,4,5,6 } ; int [] arr2 = {3,4} ; 数组arr1中的值应如下所示:1,2,5,6(不带3和4) 到目前为止,我的代码是: static int[] test(int[]a,int[]b) { int i = 0; int g = 0; int d = 0;

我一直在“排序”2个不同的数组。 我的目标是去掉array1和array2中包含的数字

以下是一个例子:

int [] arr1 = {1,2,3,4,5,6 } ;
int [] arr2 = {3,4} ;
数组arr1中的值应如下所示:1,2,5,6(不带3和4)

到目前为止,我的代码是:

static int[] test(int[]a,int[]b)
    {
        int i = 0;
        int g = 0;
        int d = 0;

        int indexB = 0;

        while( i < a.Length)
        {

            bool dvojnost = false;

            int j = 0;
            while (j<b.Length)
            {
                if (a[i] == b[j])
                {
                    dvojnost = true;
                    indexB = j;
                    break;
                }
                else
                j++;
            }

                    int trenutniElementB = 0;
                if(dvojnost==true)
                {
                    while (trenutniElementB < b.Length)
                    {
                        if (trenutniElementB != indexB)
                        {
                            b[g] = b[trenutniElementB];
                            g++;
                            trenutniElementB++;
                        }
                        else
                        {
                            trenutniElementB++;
                        }
                    }
                }




            int h = 0;
            if (dvojnost == true)
            {
                while (h < a.Length)
                {
                    if (h != i)
                    {
                        a[d] = a[h];
                        d++;
                        h++;
                    }
                    else
                    {
                        h++;
                    }
                }
            }


            i++;
        }
        return a;

    }
静态int[]测试(int[]a,int[]b)
{
int i=0;
int g=0;
int d=0;
int indexB=0;
while(i
使用LINQ:-)


如果将第二个数组视为结果的排除列表,则使用泛型列表类可能足以创建如下简单代码:

    static int[] test(int[] a, int[] b)
    {
        List<int> result = new List<int>();
        List<int> exclusion = new List<int>(b);
        for (int i = 0; i < a.Length; i++)
        {
            if (exclusion.IndexOf(a[i]) >= 0)
                continue;
            result.Add(a[i]);
        }
        return result.ToArray();
    }
静态int[]测试(int[]a,int[]b)
{
列表结果=新列表();
列表排除=新列表(b);
for(int i=0;i=0)
继续;
结果。添加(a[i]);
}
返回result.ToArray();
}

如果您将第二个数组作为结果的排除列表,那么使用泛型列表类可能足以创建如下简单代码:

    static int[] test(int[] a, int[] b)
    {
        List<int> result = new List<int>();
        List<int> exclusion = new List<int>(b);
        for (int i = 0; i < a.Length; i++)
        {
            if (exclusion.IndexOf(a[i]) >= 0)
                continue;
            result.Add(a[i]);
        }
        return result.ToArray();
    }
静态int[]测试(int[]a,int[]b)
{
列表结果=新列表();
列表排除=新列表(b);
for(int i=0;i=0)
继续;
结果。添加(a[i]);
}
返回result.ToArray();
}

将方法分为两部分。 首先清除重复项,然后对数组进行排序:

public int[] RemoveAndSort(int[] a, int[] b){
  List<int> temp = new List<int>();
  for(int i = 0; i < a.Length; i++){
    bool found = false;
    for(int j = 0; j < b.Length; j++){
      if(a[i] == b[j]){
        found = true;
      }
    }
    if(!found) temp.Add(a[i]);
  }
  temp.Sort();
  return temp.ToArray();
}
public int[]RemoveAndSort(int[]a,int[]b){
列表温度=新列表();
for(int i=0;i

我在解决方案中使用了一个列表,但您也可以在方法开始时使用与
a
长度相同的新空数组。

将方法拆分为两部分。 首先清除重复项,然后对数组进行排序:

public int[] RemoveAndSort(int[] a, int[] b){
  List<int> temp = new List<int>();
  for(int i = 0; i < a.Length; i++){
    bool found = false;
    for(int j = 0; j < b.Length; j++){
      if(a[i] == b[j]){
        found = true;
      }
    }
    if(!found) temp.Add(a[i]);
  }
  temp.Sort();
  return temp.ToArray();
}
public int[]RemoveAndSort(int[]a,int[]b){
列表温度=新列表();
for(int i=0;i

我在解决方案中使用了一个列表,但您也可以在方法开始时使用与
a
长度相同的新空数组。

您可以在不使用任何泛型集合的情况下尝试此操作:)仅数组:

public class Program
{
    static void Main(string[] args)
    {
        int resultLength = 0;
        int[] arr1 = { 1, 2, 3, 4, 5, 6 };
        int[] arr2 = { 3, 4 };
        int[] result = new int[resultLength];

        for(int i = 0; i < arr1.Length; i++)
        {
            if(!arr2.Exists(arr1[i]))
            {
                resultLength++;
                Array.Resize(ref result, resultLength);
                result[resultLength- 1] = arr1[i];
            }
        }            
    }
}

public static class MyExtensions
{
    public static bool Exists(this int[] array, int value)
    {
        for(int i = 0; i < array.Length; i++)
        {
            if (array[i] == value)
                return true;
        }
        return false;
    }
}
公共类程序
{
静态void Main(字符串[]参数)
{
int resultLength=0;
int[]arr1={1,2,3,4,5,6};
int[]arr2={3,4};
int[]结果=新的int[resultLength];
for(int i=0;i
您可以在不使用任何泛型集合的情况下尝试此操作:)仅限数组:

public class Program
{
    static void Main(string[] args)
    {
        int resultLength = 0;
        int[] arr1 = { 1, 2, 3, 4, 5, 6 };
        int[] arr2 = { 3, 4 };
        int[] result = new int[resultLength];

        for(int i = 0; i < arr1.Length; i++)
        {
            if(!arr2.Exists(arr1[i]))
            {
                resultLength++;
                Array.Resize(ref result, resultLength);
                result[resultLength- 1] = arr1[i];
            }
        }            
    }
}

public static class MyExtensions
{
    public static bool Exists(this int[] array, int value)
    {
        for(int i = 0; i < array.Length; i++)
        {
            if (array[i] == value)
                return true;
        }
        return false;
    }
}
公共类程序
{
静态void Main(字符串[]参数)
{
int resultLength=0;
int[]arr1={1,2,3,4,5,6};
int[]arr2={3,4};
int[]结果=新的int[resultLength];
for(int i=0;i
不要重新发明方向盘,使用LINQ。无论如何,如果您将此作为理解数组的练习,下面是一个避免LINQ替代的实施方案:

public static T[] Except<T>(this T[] first, T[] second)
{
    if (first == null)
        throw new ArgumentNullException(nameof(first));

    if (second == null)
        throw new ArgumentNullException(nameof(second));

    if (second.Length == 0)
        return first;

    var counter = 0;
    var newArray = new T[first.Length];

    foreach (var f in first)
    {
        var found = false;

        foreach (var s in second)
        {
            if (f.Equals(s))
            {
                found = true;
                break;
            }
        }

        if (!found)
        {
            newArray[counter] = f;
            counter++;
        }
    }

    Array.Resize(ref newArray, counter);
    return newArray;
}
公共静态T[]除外(此T[]第一,T[]第二)
{
if(first==null)
抛出新ArgumentNullException(nameof(first));
if(秒==null)
抛出新ArgumentNullException(nameof(second));
if(second.Length==0)
先返回;
var计数器=0;
var newArray=newt[first.Length];
foreach(第一个变量为f)
{
var=false;
foreach(第二个变量为s)
{
如果(f等于(s))
{
发现=真;
打破
}
}
如果(!找到)
{
newArray[计数器]=f;
计数器++;
}
}
Array.Resize(ref newArray,counter);
返回新数组;
}

不要重新发明方向盘,使用LINQ。无论如何,如果您将此作为理解数组的练习,下面是一个避免LINQ替代的实施方案:

public static T[] Except<T>(this T[] first, T[] second)
{
    if (first == null)
        throw new ArgumentNullException(nameof(first));

    if (second == null)
        throw new ArgumentNullException(nameof(second));

    if (second.Length == 0)
        return first;

    var counter = 0;
    var newArray = new T[first.Length];

    foreach (var f in first)
    {
        var found = false;

        foreach (var s in second)
        {
            if (f.Equals(s))
            {
                found = true;
                break;
            }
        }

        if (!found)
        {
            newArray[counter] = f;
            counter++;
        }
    }

    Array.Resize(ref newArray, counter);
    return newArray;
}
公共静态T[]除外(此T[]第一,T[]第二)
{
if(first==null)
抛出新ArgumentNullException(nameof(first));
if(秒==null)
抛出新ArgumentNullException(nameof(second));
if(second.Length==0)
先返回;
var计数器=0;
var newArray=newt[first.Length];
foreach(第一个变量为f)
{
var=false;
foreach(第二个变量为s)
{
如果(f等于(s))
{
发现=真;
打破