简体   繁体   中英

comparing two 2d arrays line by line

I'm trying to write a code for comparing two 2d arrays.for each line of the first array if program can find a simple line in the second array lines should return 0 else it should return 1.but i don't know why it does not work.please guide me. `

int test = 0;

for (int i = 0, q = 0; i < arr.m && q < arr.m; i++, q++)
{
    for (int j = 0, w = 0; j < arr.m && w < arr.m; j++, w++)
    {
        if (a[i, j] == b[q, w])
        { test = 0; }
        else
        {
            q++;
            w = 0;
            if (a[i, j] == b[q, w])
            { test = 0; }
            else 
                test = 1;

        }


    }
}
if (test == 1)
{
    MessageBox.Show("two graphs are different ");
} `

arr.m is number of the elements of arrays and I should remember that each line of both graphs are sorted before . I mean:

{ { 1 },{ 7, 8 ,3}, { 3, 4 }, { 5, 6 } }

{ { 3, 4 },{ 1 }, { 5, 6 }, { 7, 8,3 } } how can i show that these two arrays have same elements although they don't have the same position.

Just an addition: you know that you can simply compare Arrays (and more) with SequenceEqual.. That would be much more readable and easier to implement if you really just want to compare lines of arrays. Just put that in a loop and you`re ready to go...

Samplearray:

int[] arr1 = new int[] { 1,2,3};
int[] arr2 = new int[] { 3,2,1 };

Check:

Console.WriteLine(arr1.SequenceEqual(arr2)); // false
Console.WriteLine(arr1.Reverse().SequenceEqual(arr2)); // true

.Net 4 even has an easier way: StructuralComparisons type:

bool isStructureEqual = arr1.Equals (arr2, StructuralComparisons.StructuralEqualityComparer)); 

Don't use Multi-Dimensional Arrays, whilst conceptually okay, they suck in .Net for multiple reasons.

using System.Collections.Generic;
using System.Linq;

var bRows = Enumerable.Range(0, arr.m)
    .Select(i => b.YieldRow(i).ToList())
    .ToList();

var test = Enumerable.Range(0, arr.m)
    .Select(i => a.YieldRow(i))
    .All(aRow => bRows.Any(bRow => aRow.SequenceEqual(bRow)); 

if (test)
{
    MessageBox.Show("two graphs are different ");
}

// ...

public static class Ext
{
    // This is fine for your special case but remember,
    // MD Arrays don't necessarily have a lower bound of 0.
    private IEnumerable<T> YieldRow(this T[,] arr, int row)
    {
        for (var i = 0; i < arr.GetLength(1); i++)
        {
            yield return arr[row, i];
        }
    }
}

Following your comment

Your arrays, as declared in the question are jagged, not multi-dimensional. This answer continues on the assumption that you actually want jagged arrays and not sparsely populated multi-dimensional arrays. eg

    var a = new int[4][];
    a[0] = new[] { 1 }; 
    a[1] = new[] { 7, 8 ,3 };
    a[2] = new[] { 3, 4 };
    a[3] = new[] { 5, 6 };

    var b = new int[4][];
    b[0] = new[] { 3, 4 }; 
    b[1] = new[] { 1 };
    b[2] = new[] { 5, 6 };
    b[3] = new[] { 7, 8, 3 };

    var arr = new Arr { m = a.Length };

This algorithm does what you define. Working example here .

    var match = false;
    for (var i = 0; i < arr.m; i++)
    {
        for (var q = 0; q < arr.m; q++)
        {
            if (a[i].Length != b[q].Length)
            {
                continue;
            }

            match = true;
            for (var j = 0; j < a[i].Length; j++)
            {
                if (a[i][j] != b[q][j])
                {
                    match = false;
                    break;
                }
            }

            if (match)
            {
                break;
            }
        }

        if (!match)
        {
            break;
        }
    }

    if (!match)
    {
        Console.WriteLine("two graphs are different.");
    }

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