Remove zeros from string [,]

I have a string array defined in C # as

string[,] options = new string[100,3];

Throughout the code, it is populated with data, but not always populated.

So, if I have 80 parts of it, and 20 parts of it are not filled. 20 parts have zeros in them or 60 zeros at the end. Is there an easy way to resize the array so that after filling it, the array is the same as

String[,] options = new string[80,3];

It must be changed depending on the position of the first set of three zeros that it found.

If it were a jagged array, I would do

options = options.Where(x => x != null).ToArray();
+4
source share
4 answers

The method is quite long because it has to check every line twice ...

public static string[,] RemoveEmptyRows(string[,] strs)
{
    int length1 = strs.GetLength(0);
    int length2 = strs.GetLength(1);

    // First we count the non-emtpy rows
    int nonEmpty = 0;

    for (int i = 0; i < length1; i++)
    {
        for (int j = 0; j < length2; j++)
        {
            if (strs[i, j] != null)
            {
                nonEmpty++;
                break;
            }
        }
    }

    // Then we create an array of the right size
    string[,] strs2 = new string[nonEmpty, length2];

    for (int i1 = 0, i2 = 0; i2 < nonEmpty; i1++)
    {
        for (int j = 0; j < length2; j++)
        {
            if (strs[i1, j] != null)
            {
                // If the i1 row is not empty, we copy it
                for (int k = 0; k < length2; k++)
                {
                    strs2[i2, k] = strs[i1, k];
                }

                i2++;
                break;
            }
        }
    }

    return strs2;
}

Use it as:

string[,] options = new string[100, 3];
options[1, 0] = "Foo";
options[3, 1] = "Bar";
options[90, 2] = "fiz";
options = RemoveEmptyRows(options);

As suggested by Alexei, there is another way:

public static string[,] RemoveEmptyRows2(string[,] strs)
{
    int length1 = strs.GetLength(0);
    int length2 = strs.GetLength(1);

    // First we put somewhere a list of the indexes of the non-emtpy rows
    var nonEmpty = new List<int>();

    for (int i = 0; i < length1; i++)
    {
        for (int j = 0; j < length2; j++)
        {
            if (strs[i, j] != null)
            {
                nonEmpty.Add(i);
                break;
            }
        }
    }

    // Then we create an array of the right size
    string[,] strs2 = new string[nonEmpty.Count, length2];

    // And we copy the rows from strs to strs2, using the nonEmpty
    // list of indexes
    for (int i1 = 0; i1 < nonEmpty.Count; i1++)
    {
        int i2 = nonEmpty[i1];

        for (int j = 0; j < length2; j++)
        {
            strs2[i1, j] = strs[i2, j];
        }
    }

    return strs2;
}

, , . , , , , .

+7

, :

, , , . .

: , - , downvotes, ( ):)

void Main()
{
    string[,] options = new string[100,3];

    options[0,0] = "bleb";
    options[1,1] = "bleb";
    options[2,0] = "bleb";
    options[2,1] = "bleb";
    options[3,2] = "bleb";
    options[4,1] = "bleb";

    string[,] trimmed = TrimNullRows(options);

    Console.WriteLine(trimmed);
}

public string[,] TrimNullRows(string[,] options) 
{
    IList<string[]> nonNullRows = new List<string[]>();
    for (int x = 0; x < options.GetLength(0); x++) 
    {
        bool allNull = true;

        var row = new string[options.GetLength(1)];

        for (int y = 0; y < options.GetLength(1); y++) 
        {
            row[y] = options[x,y];
            allNull &= options[x,y] == null;
        }


        if (!allNull) 
        {
            nonNullRows.Add(row);
        }
    }

    var optionsTrimmed = new string[nonNullRows.Count, options.GetLength(1)];

    for (int i=0;i<nonNullRows.Count;i++)
    {
        for (int j=0;j<options.GetLength(1);j++)
        {
            optionsTrimmed[i, j] = nonNullRows[i][j];
        }
    }


    return optionsTrimmed;
}
0

linq

static string[,] RemoveNotNullRow(string[,] o)
{
    var rowLen = o.GetLength(1);
    var notNullRowIndex = (from oo in o.Cast<string>().Select((x, idx) => new { idx, x })
                group oo.x by oo.idx / rowLen into g
                where g.Any(f => f != null)
                select g.Key).ToArray();

    var res = new string[notNullRowIndex.Length, rowLen];

    for (int i = 0; i < notNullRowIndex.Length; i++)
    {
        Array.Copy(o, notNullRowIndex[i] * rowLen, res, i * rowLen, rowLen);
    }
    return res;
}
0

. , , , , , ( ), .

void Main()
{
    string[,] options = new string[100,3];

    options[3, 1] = "Hi";
    options[5, 0] = "Dan";

    var results = 
        options
            .JagIt()
            .Where(i => i.Any(j => j != null))
            .UnjagIt();

    results.Dump();
}

static class Extensions
{
    public static IEnumerable<IEnumerable<T>> JagIt<T>(this T[,] array)
    {
        for (var i = 0; i < array.GetLength(0); i++)
            yield return GetRow(array, i);
    }

    public static IEnumerable<T> GetRow<T>(this T[,] array, int rowIndex)
    {
        for (var j = 0; j < array.GetLength(1); j++)
            yield return array[rowIndex, j];
    }

    public static T[,] UnjagIt<T>(this IEnumerable<IEnumerable<T>> jagged)
    {
        var rows = jagged.Count();
        if (rows == 0) return new T[0, 0];

        var columns = jagged.Max(i => i.Count());

        var array = new T[rows, columns];

        var row = 0;
        var column = 0;

        foreach (var r in jagged)
        {
          column = 0;

          foreach (var c in r)
          {
            array[row, column++] = c;
          }

          row++;
        }

        return array;
    }
}

JagIt , , yield . , LINQ. , , (, Select(i => i.ToArray()).ToArray()).

UnjagIt , . unyield, : D

This is pretty inefficient, of course, but this is not necessarily a problem. You could save some iterations by saving, for example, an internal enumeration array- this will save us from having to iterate over all the internal elements.

I basically save this as an inexpensive, processor-rich alternative to the heavy use of memory @xanatos, CPU-cheap (relatively).

Of course, the main bonus is that it can be used to process any multidimensional arrays in the form of jagged arrays and convert them back. General solutions are usually not the most effective: D

0
source

All Articles