简体   繁体   中英

c# How to compare values with a 2D array

int[] a = new int[] { 1, 2, 3, 4 };
int[] b = new int[] { 5, 6, 1, 2, 7, 8 };
int Count = 0;
for (int i = 0; i < a.Length; i++)
{
     for (int j = 0; j < b.Length; j++)
     {
         if (a[i] == b[j])
         { 
             Count++; 
             break; 
         }
     }        
 } 

Console.WriteLine(Count);
Console.ReadLine();    

Above is a simple program which can search through both arrays and find duplicates between them. However I am having difficulty when making the first array a 2D array as I get an error running the code below 'Index was outside the bounds of the array.' I really can't figure out what to do so I would be grateful for any help.

int[][] a = {new int[] { 1, 2, 3, 4 }, new int[] { 3, 9, 9 }};      
int[] b = new int[] { 5, 6, 1, 2, 7, 8 };
int Count = 0;
for (int i = 0; i < a.Length; i++)
{
     for (int j = 0; j < b.Length; j++)
     {
         if (b[j] == a[i][j])
         { 
             Count++; 
             break; 
         }
     }
} 

Console.WriteLine(Count);
Console.ReadLine();
//initialize stuff here
for(int i = 0; i<a.length; ++i) {//iterate over the rows in a
    for(int j = 0; j<a[i].length; ++j) {//iterate over columns in a
        for(int k = 0; k < b.length; ++k) {//iterate over b
            if(a[i][j] == b[k]) { 
                //increment stuff here
            }//end if
        }//end for k
    }//end for j
//print stuff for each sub array here 
}end for i 
//print stuff here for all sub arrays 

I assume you want to find duplicates between the 2d array and a 1d array.

      int[][] a = { new int[] { 1, 2, 3, 4 }, new int[] { 3, 9, 9 } };
        int[] b = new int[] { 5, 6, 1, 2, 7, 8 };
        int Count = 0;
        for (int h = 0; h < a.Length; h++)
        {
            for (int i = 0; i < a[h].Length; i++)
            {
                for (int j = 0; j < b.Length; j++)
                {
                    if (b[j] == a[h][i])
                    {
                        Count++;
                        break;
                    }
                }
            }
        }

Or in Linq ;)

int Count = (from i in a from j in i from k in b where k == j select j).Count();

= Found 2 duplicates between a2 and b?

Edit for new spec

2nd edit for storing duplicates for each 2d array element.

   var duplicates = new List<int>();
        foreach (var i in a)
        {
            var duplicate = 0;
            foreach (var j in i)
            {
                foreach (var k in b)
                {
                    if (k == j)
                    {
                        duplicate++;
                    }
                }
            }
            duplicates.Add(duplicate);
        }

Or linq again ;)

var duplicates = a.Select(i => (from j in i from k in b where k == j select j).Count()).ToList();

Update 3: For your selected code format:

var duplicates = new List<int>();
        for (int h = 0; h < a.Length; h++)
        {
            var duplicate = 0;
            for (int i = 0; i < a[h].Length; i++)
            {
                for (int j = 0; j < b.Length; j++)
                {
                    if (b[j] == a[h][i])
                    {
                        duplicate++;
                        break;
                    }
                }
            }
            duplicates.Add(duplicate);
        }

Added duplicate output:

     for (int d = 0; d < duplicates.Count; d++)
        {
            Console.WriteLine(duplicates[d]);
        }

You can try with a foreach

            foreach (var val1 in a)
            {
                foreach (var val2 in val1)
                {
                    foreach (var val3 in b)
                    {
                        if (val3 == val2)
                        {
                            Count++;
                        }
                    }
                }
            }

Or with a another for

        for (int i = 0; i < a.Length; i++)
        {
            var innerArray = a[i];
            for (int f = 0; f < innerArray.Length; f++)
            {
                for (int j = 0; j < b.Length; j++)
                {
                    if (b[j] == innerArray[f])
                    {
                        Count++;
                    }
                }
            } 
        } 

您也可以使用Linq表达式,我想它比循环要容易得多

a.SelectMany(x => x).Count(x => b.Contains(x))

Your array a is actually an array with 2 elements, one that is 4 elements long, and the other that is 3 elements long. Your inner loop is iterating over each of those arrays for every element of b , so that when it hits the 5th element of b , b[4] compared against a[0][4] , it's out of bounds, since the last element of a[0] is a[0][3] , but you are trying to compare to a[0][4] . Also, do you really want to break after you've found a match?

If you want to find all duplicates, the simplest bets are probably either @peyman, or you might use the idea of commenter @zerkms, Enumerable.Intersect<TSource> , assuming flattening a into 1d first.

int[] flattened = a.SelectMany(item => item).ToArray();
Count = b.Intersect(flattened).Count();

which is the same as:

b.Intersect(a.SelectMany(item => item).ToArray()).Count();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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