0

I meet a problem that I don't know how to solve it. I created an 2-D array which contains Date and price.I want to delete the row whose datetime is between two date.The example below, I want to delete the third row.

Date    Price 
01/07   10
02/07   20 
Empty   30
03/07   40

Here is my code:(I dont know why it does work)

for (int i=0;i<row.length;i++)
{
    for (int j=0;j<col.length;j++)
    {
        if (Array[row,0]=" ")
        {
            Array[row,j]=Array[row+1,j];
            i++
        }
    }
}
Kroll DU
  • 79
  • 1
  • 10
  • If you need a collection you can dynamically size, use a `List` instead of an array. – Matt Burland Jul 16 '15 at 20:33
  • 1
    Also `=` isn't how you do comparisions and `" "` isn't the same as an empty string, or the string `Empty`. – Matt Burland Jul 16 '15 at 20:35
  • 1
    `Empty != " "`, your inner "if" statement should be `if (string.IsNullOrWhitespace(Array[row,0]))`... Really you aren't deleting anything, you are just moving your data up and leaving blanks at the end. This code will also throw an "out of bounds" error because you are using [row + 1], and when you are processing the last row, there is no [row + 1]. – Ron Beyer Jul 16 '15 at 20:36
  • 1
    Arrays in C# are static by nature. That means that you would have to assign it to a new 2d array with one less row. Then loop through and reassign values, skipping the row you want to remove. Or, if that is functionality you need, you could use an `IEnumerable` (or subset of it) of type array and remove what you need as you need it. The code you currently have is just moving your rows up, so your array is going to be the same size, with a duplicate row at the end. – David Jul 16 '15 at 20:37
  • You code as you have it will only copy the row below the row with a "blank" (however you actually define it) date to the one above. Leaving you with duplicates. – Matt Burland Jul 16 '15 at 20:37

4 Answers4

2

If I were you I would create an object and store Date and Price as a properties.

For example:

public class DateAndPrice //let this name be whatever you want
{
    public DateTime Date { get; set; }
    public int Price { get; set; }
}

Then, store them in a List so you can easily remove them with the Remove method.

List<DateAndPrice> list = new List<DateAndPrice>();
LaneL
  • 737
  • 1
  • 6
  • 25
1

If you are hell-bound on using arrays, you can use a Linq query to filter out the results and return a new array:

var data = new[] {
                new { Date = "01/07", Price = 10 },
                new { Date = "02/07", Price = 20 },
                new { Date = "", Price = 30 },
                new { Date = "03/07", Price = 40 }
            };

var noBlanks = (from d in data 
                where !string.IsNullOrWhiteSpace(d.Date) 
                select d).ToArray();

Which will select the data that does not have empty, null, or whitespace date items and place them in a new array.

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
  • Your example is using a 1D array, the OP is using a 2D array. – Matt Burland Jul 17 '15 at 14:10
  • The OP's code would never compile, so I don't know what he's really using, it looks like he's using `row` as the array in one spot, `col` in another, `Array` in another, then using `row` as an index, etc. This answer really can't be that much worse than the most upvoted one on this question... – Ron Beyer Jul 17 '15 at 14:13
1

If you're dead set on staying with a non-List like 2D array, you can try the following:

string[,] array = 
{
    { "01/07", "10" },
    { "02/07", "20" },
    { String.Empty, "30" },
    { "03/07", "40" },
};

array = RemoveEmptyDates(array);

for (int i = 0; i <= array.GetUpperBound(0); i++)
{
    for (int j = 0; j <= array.GetUpperBound(1); j++)
    {
        Console.Write("{0} \t", array[i, j]);
    }
    Console.WriteLine();
}

RemoveEmptyDates looks like:

public static string[,] RemoveEmptyDates(string[,] array)
{
    // Find how many rows have an empty date
    int rowsToRemove = 0;
    for (int i = 0; i <= array.GetUpperBound(0); i++)
    {
        if (string.IsNullOrEmpty(array[i, 0]))
        {
            rowsToRemove++;
        }
    }

    // Reinitialize an array minus the number of empty date rows
    string[,] results = new string[array.GetUpperBound(0) + 1 - rowsToRemove, array.GetUpperBound(1) + 1];

    int row = 0;
    for (int i = 0; i <= array.GetUpperBound(0); i++)
    {
        int col = 0;
        if (!string.IsNullOrEmpty(array[i, 0]))
        {
            for (int j = 0; j <= array.GetUpperBound(1); j++)
            {
                results[row, col] = array[i, j];
                col++;
            }
            row++;
        }
    }

    return results;
}

Results:

01/07   10
02/07   20
03/07   40
Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0

Your approach to this is not the best. You should be creating a helper class:

public class DatePrice
{
    public DateTime Date { get; set; }
    public decimal Price { get; set; }
}

Then creating a collection class:

var prices = new List<DatePrice>();

Then you can add data like this:

prices.Add(new DatePrice() { Date = DateTime.Now, Price = 10m });

And you can easily remove an item based on an index like this:

prices.RemoveAt(2);

If you really must use an array, you'll need an extension method, such as this to remove an item (copied from here):

public static T[] RemoveAt<T>(this T[] source, int index)
{
    T[] dest = new T[source.Length - 1];
    if( index > 0 )
        Array.Copy(source, 0, dest, 0, index);

    if( index < source.Length - 1 )
        Array.Copy(source, index + 1, dest, index, source.Length - index - 1);

    return dest;
}

For 2-dimensional arrays, use this:

string[][] a = new string[][] { 
    new string[] { "a", "b" } /*1st row*/, 
    new string[] { "c", "d" } /*2nd row*/, 
    new string[] { "e", "f" } /*3rd row*/
};
int rowToRemove = 1; // 2nd row
a = a.Where((el, i) => i != rowToRemove).ToArray();
Community
  • 1
  • 1
Icemanind
  • 47,519
  • 50
  • 171
  • 296