日期:2014-05-19  浏览次数:20531 次

如何从数组中减掉数组
如下:
数组A:
int[]   A=new   int[5];
A[0]=1;
A[1]=2;
A[2]=3;
A[3]=4;
A[4]=5;

数组B:
int[]   B=new   int[3];
B[0]=1;
B[1]=3;
B[2]=4;

得到新的数组C
C[0]=2;
C[1]=5;

------解决方案--------------------
修正一下...
for(i=n;i <A.length();i++)
{A[i]=A[i+1];}
A[i]=null;
------解决方案--------------------
比较笨的方法
string c= " ";
foreach(int a in A)
{
int flat = 0;
foreach(int b in B)
{
if(a==b)
{
flat = 1;
}
}
if(flat == 0)
{
c += a + ", ";
}
}

string[] C = c.Substring(0,c.Length-1).Split( ', ');//要int[] 的就转换下
//显示结果
foreach(string cc in C)
{
Response.Write(cc+ " <br> ");
}
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Collections;

public class SubtrationArray <T> {
public T[] Subtration(T[] a,T[] b){
ArrayList al=new ArrayList();
foreach(object obj in a){
if(Array.BinarySearch(b,obj) <0){
al.Add(obj);
}
}
return (T[])al.ToArray(typeof(T));
}
}
public class MyClass
{
public static void Main()
{
try{
int[] A=new int[5]{1,2,3,4,5};
int[] B=new int[3]{1,3,4};
SubtrationArray <int> sa=new SubtrationArray <int> ();
int[] c=sa.Subtration(A,B);
foreach(int i in c){
Console.WriteLine(i);
}

string[] AA=new string[]{ "one ", "two ", "three "};
string[] BB=new string[]{ "one ", "three "};
SubtrationArray <string> SS=new SubtrationArray <string> ();
string[] CC=SS.Subtration(AA,BB);
foreach(string i in CC){
Console.WriteLine(i);
}

}
catch(Exception e){
Console.WriteLine(e.ToString());
}
finally{
Console.WriteLine( "END ");
Console.Read();
}
}




}
------解决方案--------------------
笨办法..实现你要的结果就行了
int[] A = new int[5];
A[0] = 1;
A[1] = 2;
A[2] = 3;
A[3] = 4;
A[4] = 5;
int[] B = new int[3];
B[0] = 1;
B[1] = 3;
B[2] = 4;
System.Collections.ArrayList a = new System.Collections.ArrayList(A.Length + B.Length);
foreach (int aa in A)
{
a.Add(aa);
}
foreach (int bb in B)
{
a.Add(bb);
}
for (int i = 0; i < a.Count; i++)
{
for (int j = i + 1; j < a.Count; j++)
{
if (i != a.Count - 1)
{
if (a[i].ToString() == a[j].ToString())
{
a.RemoveAt(i);
a.RemoveAt(j - 1);
i--;
}
}
}
}
int[] C=new int[a.Count];
for (int k = 0; k < a.Count; k++)