How to delete a row from a 2d array in c#? [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Delete row of 2D string array in C#
i have a 2d string array, I want to delete a specified row from the array.

string[] a = new string[] { "a", "b" }; //dummy string array
int deleteIndex = 1; //we want to "delete" element in position 1 of string
a = a.ToList().Where(i => !a.ElementAt(deleteIndex).Equals(i)).ToArray();
dirty but gives the expected result (foreach through the array to test it)
EDIT missed the "2d array" detail, here is the right code for the job
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; //we want to get rid of row {"c","d"}
//a = a.ToList().Where(i => !i.Equals(a.ElementAt(rowToRemove))).ToArray(); //a now has 2 rows, 1st and 3rd only.
a = a.Where((el, i) => i != rowToRemove).ToArray(); // even better way to do it maybe
code updated

As has been said above you cant remove from an array.
If you are going to need to remove rows quite often maybe change from using a 2d array to a list containing an array of string. This way you can make use of the remove methods that list implements.

Ok so I said you can't "delete" them. That's still true. You'll have to create a new array instance with enough space for the items you want to keep and copy them over.
If this is a jagged array, using LINQ here could simplify this.
string[][] arr2d =
{
new[] { "foo" },
new[] { "bar", "baz" },
new[] { "qux" },
};
// to remove the second row (index 1)
int rowToRemove = 1;
string[][] newArr2d = arr2d
.Where((arr, index) => index != rowToRemove)
.ToArray();
// to remove multiple rows (by index)
HashSet<int> rowsToRemove = new HashSet<int> { 0, 2 };
string[][] newArr2d = arr2d
.Where((arr, index) => !rowsToRemove.Contains(index))
.ToArray();
You could use other LINQ methods to remove ranges of rows easier (e.g., Skip(), Take(), TakeWhile(), etc.).
If this is a true two-dimensional (or other multi-dimensional) array, you won't be able to use LINQ here and will have to do it by hand and it gets more involved. This still applies to the jagged array as well.
string[,] arr2d =
{
{ "foo", null },
{ "bar", "baz" },
{ "qux", null },
};
// to remove the second row (index 1)
int rowToRemove = 1;
int rowsToKeep = arr2d.GetLength(0) - 1;
string[,] newArr2d = new string[rowsToKeep, arr2d.GetLength(1)];
int currentRow = 0;
for (int i = 0; i < arr2d.GetLength(0); i++)
{
if (i != rowToRemove)
{
for (int j = 0; j < arr2d.GetLength(1); j++)
{
newArr2d[currentRow, j] = arr2d[i, j];
}
currentRow++;
}
}
// to remove multiple rows (by index)
HashSet<int> rowsToRemove = new HashSet<int> { 0, 2 };
int rowsToKeep = arr2d.GetLength(0) - rowsToRemove.Count;
string[,] newArr2d = new string[rowsToKeep, arr2d.GetLength(1)];
int currentRow = 0;
for (int i = 0; i < arr2d.GetLength(0); i++)
{
if (!rowsToRemove.Contains(i))
{
for (int j = 0; j < arr2d.GetLength(1); j++)
{
newArr2d[currentRow, j] = arr2d[i, j];
}
currentRow++;
}
}

Instead of array you can use List or ArrayList class. Using it you can dynamically add element and remove based on your requirement. Array is fixed in size, which can not be manipulated dynamically.

The best way is to work with a List<Type>! The items are ordered in the way the are added to the list and each of them can be deleted.
Like this:
var items = new List<string>;
items.Add("One");
items.Add("Two");
items.RemoveAt(1);

Related

Sorting 2D array by 2 columns

I am looking for an efficient way to sort the data in a 2D array. The array can have many rows and columns but, in this example, I will just limit it to 6 rows and 5 columns. The data is strings as some are words. I only include one word below but in the real data there are a few columns of words. I realise if we sort, we should treat the data as numbers?
string[,] WeatherDataArray = new string[6,5];
The data is a set of weather data that is read every day and logged. This data goes through many parts of their system which I cannot change and it arrives to me in a way that it needs sorting. An example layout could be:
Day number, temperature, rainfall, wind, cloud
The matrix of data could look like this
3,20,0,12,cumulus
1,20,0,11,none
23,15,0,8,none
4,12,0,1,cirrus
12,20,0,12,cumulus
9,15,2,11,none
They now want the data sorted so it will have temperature in descending order and day number in ascending order. The result would be
1,20,0,11,none
3,20,0,12,cumulus
12,20,0,12,cumulus
9,15,2,11,none
23,15,0,0,none
4,12,0,1,cirrus
The array is stored and later they can extract it to a table and do lots of analysis on it. The extraction side is not changing so I cannot sort the data in the table, I have to create the data in the correct format to match the existing rules they have.
I could parse each row of the array and sort them but this seems a very long-handed method. There must be a quicker more efficient way to sort this 2D array by two columns? I think I could send it to a function and get returned the sorted array like:
private string[,] SortData(string[,] Data)
{
//In here we do the sorting
}
Any ideas please?
I agree with the other answer that it's probably best to parse each row of the data into an instance of a class that encapsulates the data, creating a new 1D array or list from that data. Then you'd sort that 1D collection and convert it back into a 2D array.
However another approach is to write an IComparer class that you can use to compare two rows in a 2D array like so:
public sealed class WeatherComparer: IComparer
{
readonly string[,] _data;
public WeatherComparer(string[,] data)
{
_data = data;
}
public int Compare(object? x, object? y)
{
int row1 = (int)x;
int row2 = (int)y;
double temperature1 = double.Parse(_data[row1, 1]);
double temperature2 = double.Parse(_data[row2, 1]);
if (temperature1 < temperature2)
return 1;
if (temperature2 < temperature1)
return -1;
int day1 = int.Parse(_data[row1,0]);
int day2 = int.Parse(_data[row2,0]);
return day1.CompareTo(day2);
}
}
Note that this includes a reference to the 2D array to be sorted, and parses the elements for sorting as necessary.
Then you need to create a 1D array of indices, which is what you are actually going to sort. (You can't sort a 2D array, but you CAN sort a 1D array of indices that reference the rows of the 2D array.)
public static string[,] SortData(string[,] data)
{
int[] indexer = Enumerable.Range(0, data.GetLength(0)).ToArray();
var comparer = new WeatherComparer(data);
Array.Sort(indexer, comparer);
string[,] result = new string[data.GetLength(0), data.GetLength(1)];
for (int row = 0; row < indexer.Length; ++row)
{
int dest = indexer[row];
for (int col = 0; col < data.GetLength(1); ++col)
result[dest, col] = data[row, col];
}
return result;
}
Then you can call SortData to sort the data:
public static void Main()
{
string[,] weatherDataArray = new string[6, 5]
{
{ "3", "20", "0", "12", "cumulus" },
{ "1", "20", "0", "11", "none" },
{ "23", "15", "0", "8", "none" },
{ "4", "12", "0", "1", "cirrus" },
{ "12", "20", "0", "12", "cumulus" },
{ "9", "15", "2", "11", "none" }
};
var sortedWeatherData = SortData(weatherDataArray);
for (int i = 0; i < sortedWeatherData.GetLength(0); ++i)
{
for (int j = 0; j < sortedWeatherData.GetLength(1); ++j)
Console.Write(sortedWeatherData[i,j] + ", ");
Console.WriteLine();
}
}
Output:
1, 20, 0, 11, none,
3, 20, 0, 12, cumulus,
12, 20, 0, 12, cumulus,
9, 15, 2, 11, none,
23, 15, 0, 8, none,
4, 12, 0, 1, cirrus,
Note that this code does not contain any error checking - it assumes there are no nulls in the data, and that all the parsed data is in fact parsable. You might want to add appropriate error handling.
Try it on .NET Fiddle: https://dotnetfiddle.net/mwXyMs
I would suggest parsing the data into objects that can be sorted by conventional methods. Like using LINQ:
myObjects.OrderBy(obj => obj.Property1)
.ThenBy(obj=> obj.Property2);
Treating data as a table of strings will just make processing more difficult, since at every step you would need to parse values, handle potential errors since a string may be empty or contain an invalid value etc. It is a much better design to do all this parsing and error handling once when the data is read, and convert it to text-form again when writing it to disk or handing it over to the next system.
If this is a legacy system with lots of parts that handle the data in text-form I would still argue to parse the data first, and do it in a separate module so it can be reused. This should allow the other parts to be rewritten part by part to use the object format.
If this is completely infeasible you either need to convert the data to a jagged array, i.e. string[][]. Or write your own sorting that can swap rows in a multidimensional array.
I had fun trying to make something better than the accepted answer, and I think I did.
Reasons it is better:
Which columns it uses to sort and whether in an ascending or descending order is not hardcoded, but passed in as parameters. In the post, I understood that they might change their mind in the future as to how to sort the data.
It supports sorting by columns that do not contain numbers, for if they ever want to sort by the name columns.
In my testing, for large data, it is much faster and allocates less memory.
Reasons it is faster:
It never parses the same index of Data twice. It caches the numbers.
When copying, it uses Span.CopyTo instead of indeces.
It doesn't create a new Data array, it sorts the rows in place. This also means it won't copy the rows that are already in the correct spots.
Here's the usage:
DataSorter.SortDataWithSortAguments(array, (1, false), (0, true));
And here's the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
namespace YourNamespace;
public static class DataSorter
{
public static void SortDataWithSortAguments(string[,] Data, params (int columnIndex, bool ascending)[] sortingParams)
{
if (sortingParams.Length == 0)
{
return;
// maybe throw an exception instead? depends on what you want
}
if (sortingParams.Length > 1)
{
var duplicateColumns =
from sortingParam in sortingParams
group false by sortingParam.columnIndex
into sortingGroup
where sortingGroup.Skip(1).Any()
select sortingGroup.Key;
var duplicateColumnsArray = duplicateColumns.ToArray();
if (duplicateColumnsArray.Length > 0)
{
throw new ArgumentException($"Cannot sort by the same column twice. Duplicate columns are: {string.Join(", ", duplicateColumnsArray)}");
}
}
for (int i = 0; i < sortingParams.Length; i++)
{
int col = sortingParams[i].columnIndex;
if (col < 0 || col >= Data.GetLength(1))
{
throw new ArgumentOutOfRangeException($"Column index {col} is not within range 0 to {Data.GetLength(1)}");
}
}
int[] linearRowIndeces = new int[Data.GetLength(0)];
for (int i = 0; i < linearRowIndeces.Length; i++)
{
linearRowIndeces[i] = i;
}
Span<int> sortedRows = SortIndecesByParams(Data, sortingParams, linearRowIndeces);
SortDataRowsByIndecesInPlace(Data, sortedRows);
}
private static float[]? GetColumnAsNumbersOrNull(string[,] Data, int columnIndex)
{
if (!float.TryParse(Data[0, columnIndex], out float firstNumber))
{
return null;
}
// if the first row of the given column is a number, assume all rows of the column should be numbers as well
float[] column = new float[Data.GetLength(0)];
column[0] = firstNumber;
for (int row = 1; row < column.Length; row++)
{
if (!float.TryParse(Data[row, columnIndex], out column[row]))
{
throw new ArgumentException(
$"Rows 0 to {row - 1} of column {columnIndex} contained numbers, but row {row} doesn't");
}
}
return column;
}
private static Span<int> SortIndecesByParams(
string[,] Data,
ReadOnlySpan<(int columnIndex, bool ascending)> sortingParams,
IEnumerable<int> linearRowIndeces)
{
var (firstColumnIndex, firstAscending) = sortingParams[0];
var firstColumn = GetColumnAsNumbersOrNull(Data, firstColumnIndex);
IOrderedEnumerable<int> sortedRowIndeces = (firstColumn, firstAscending) switch
{
(null, true) => linearRowIndeces.OrderBy(row => Data[row, firstColumnIndex]),
(null, false) => linearRowIndeces.OrderByDescending(row => Data[row, firstColumnIndex]),
(not null, true) => linearRowIndeces.OrderBy(row => firstColumn[row]),
(not null, false) => linearRowIndeces.OrderByDescending(row => firstColumn[row])
};
for (int i = 1; i < sortingParams.Length; i++)
{
var (columnIndex, ascending) = sortingParams[i];
var column = GetColumnAsNumbersOrNull(Data, columnIndex);
sortedRowIndeces = (column, ascending) switch
{
(null, true) => sortedRowIndeces.ThenBy(row => Data[row, columnIndex]),
(null, false) => sortedRowIndeces.ThenByDescending(row => Data[row, columnIndex]),
(not null, true) => sortedRowIndeces.ThenBy(row => column[row]),
(not null, false) => sortedRowIndeces.ThenByDescending(row => column[row])
};
}
return sortedRowIndeces.ToArray();
}
private static void SortDataRowsByIndecesInPlace(string[,] Data, Span<int> sortedRows)
{
Span<string> tempRow = new string[Data.GetLength(1)];
for (int i = 0; i < sortedRows.Length; i++)
{
while (i != sortedRows[i])
{
Span<string> firstRow = MemoryMarshal.CreateSpan(ref Data[i, 0], tempRow.Length);
Span<string> secondRow = MemoryMarshal.CreateSpan(ref Data[sortedRows[i], 0], tempRow.Length);
firstRow.CopyTo(tempRow);
secondRow.CopyTo(firstRow);
tempRow.CopyTo(secondRow);
(sortedRows[i], sortedRows[sortedRows[i]]) = (sortedRows[sortedRows[i]], sortedRows[i]);
}
}
}
}
PS: I should not have spent so much time working on this considering my responsibilities, but it was fun.

Is there a way to expand a one-dimensional Array into a 2D Array with the original values in the [0] index of the 2D Array in C#?

I have an Array with Category Names, but now i Need to assign a few Counters to each Category.
Is there a way to expand my 1D-Array to a 2D-Array in C#?
Thanks for helping!
Edit:
PerformanceCounterCategory[] categories;
categories = PerformanceCounterCategory.GetCategories();
string[] categoryNames = new string[categories.Length];
string[] categoryNames_en = new string[categories.Length];
for (int objX = 0; objX < categories.Length; objX++)
{
categoryNames[objX] = categories[objX].CategoryName;
}
Array.Sort(categoryNames);
for (int objX = 0; objX < categories.Length; objX++)
{
Console.WriteLine("{0,4} - {1}", objX + 1, categoryNames[objX]);
}
I have the Array categoryNames with all the Names of the Categories, but in every Category there are a few Counters which i want to assign to their Category somehow...
Unfortunately you can't use Array.Copy since the source and destination array do not have the same dimensions.
Furthermore, you can't expand arrays in C#, since they are initialized with a fixed size.
What you can do is create a new array with a second dimonesion and copy the values over and set the second dimension to a default value.
void Main()
{
int[] sourceCollection = new [] {1,2,3,4,5,6,7} ;
var result = CopyArrayValues(sourceCollection, 2);
result.Dump();
}
//create a new 2d array
T[,] CopyArrayValues<T>(T[] DataSource, int SecondLength)
{
//Initialize the new array
var Target = new T[DataSource.Length, SecondLength];
//Copy values over
for (int i = 0; i < DataSource.Length; i++)
{
Target[i, 0] = DataSource[i];
}
return Target;
}
Output:
If it's ok for you to have array of arrays, you can do something along this pattern:
int[] sourceCollection = new[] { 1, 2, 3, 4, 5, 6, 7 };
int[][] arr = sourceCollection.Select(i => Enumerable.Range(i, 4).ToArray()).ToArray();

C# How do I parse a text file of data of unknown row size to a 2D array?

I can easily pass a data file to a 2D array if I know the size of the data file, but thats pretty pointless when you want to add and remove data.
How can I instantiate a 2D array of that Data rows length that is global and not out of scope to the rest of the program?
Here is the usual code, for an array of 4 rows and 6 columns, but I want to add/remove data to the data file making it of unknown length in rows.
string[,] allStudentData = new string[4, 6];
string[] allStudents = File.ReadAllLines("data.txt");
for (int i = 0; i < allStudents.Count(); i++)
{
for (int j = 0; j < 6; j++)
{
string[] DataIn = allStudents[i].Split(',');
allStudentData[i, j] = DataIn[j];
}
}
Thanks for your ideas :-)
Why not use something mutable, like a List ?
You can add columns using the .Add() command, , and you can collapse it into an array when you're done with it (.ToArray())
you can do as below
var result = File.ReadAllLines("data.txt").Select(x=>x.Split(',')).ToArray();
You can use a List that holds a custom object (it could be an array) instead of Array and then at the end convert the list to an array with the ToArray extension:
var list = new List<string[]>();
var data1 = new string[2] {"1", "2" };
var data2 = new string[3] {"1", "2", "3" };
list.Add(data1);
list.Add(data2);
list.ToArray(); // converts the list to string[][]
After you are done reading the file you could add easily more items to the list and then at the end write all modifications to the file. If you think, that in future you will need more than the two dimensional array, than it's worth it to create a custom object like:
public class Student
{
public string Name { get; set; }
public int Grade { get; set; }
}
and use this instead of the string[]:
var studentData = new List<Student>();
The maintenance later will be much more easier (adding address, classes, and so on). I admit, you will have a little more work to do when reading and writing, but if the project grows it will pay off.
You can still use LINQ to read the data on one line:
// creates a List<Student>
var studentData = File.ReadAllLines("data.txt")
.Select(row => row.Split(','))
.Select(elements =>
new Student
{
Name = elements[0],
Grade = int.Parse(elements[1])
}).ToList();
If the number of columns is always 6 and only the number of rows is unknown. Just change the first 2 rows of your code to:
string[] allStudents = File.ReadAllLines("data.txt");
string[,] allStudentData = new string[allStudents.Count(), 6];
If the number of columns is unknown you can do this to get a 2D result:
var not2DResult = File.ReadAllLines("data.txt").Select(x => x.Split(',')).ToArray();
var maxRow = not2DResult.Count()-1;
int maxColumn = Enumerable.Select(not2DResult, c => c.Count()).Max()-1;
var result2D = new string[maxRow, maxColumn];
for (int rowNumber = 0; rowNumber < maxRow; rowNumber++)
{
var row = not2DResult[rowNumber];
for (int columnNumber = 0; columnNumber < row.Count(); columnNumber++)
{
result2D[rowNumber, columnNumber] = row[columnNumber];
}
}
Try using a Lists to hold the file information.
var myList = new List<string>();
myList = File.ReadLines("data.txt");
var my2DList = new List<List<string>>();
foreach(string line in myList)
my2DList.Add(line.Split(','));
If you want to know the number of lines, just use:
int numberOfLines = my2DList.Count;
For the number of items in an individual line:
int lengthOfLine3 = my2DList[3].Length;

Remove Element From Array by String (text) instead of Index

I want to remove the element form the array. Actually I don't know the the index of the element and want to remove through it's value. I have tried a lot but fail. This is the function which i used to add element in the Array
string [] Arr;
int i = 0;
public void AddTOList(string ItemName)
{
Arr[i] = ItemName;
i++;
}
And I want to remove the element by the value. I know the below function is wrong but I want to explain what I want:
public void RemoveFromList(string ItemName)
{
A["Some_String"] = null;
}
Thanks
If you want to remove items by a string key then use a Dictionary
var d = new Dictionary<string, int>();
d.Add("Key1", 3);
int t = d["Key1"];
Or something like that.
Array has a fixed size, which is not suitable for your requirement. Instead you can use List<string>.
List<string> myList = new List<string>();
//add an item
myList.Add("hi");
//remove an item by its value
myList.Remove("hi");
List<string> list = new List<string>(A);
list.Remove(ItemName);
A = list.ToArray();
and #see Array.Resize
and #see Array.IndexOf
You can iterate through every value in array and if found then remove it. Something like this
string[] arr = new string[] { "apple", "ball", "cat", "dog", "elephant", "fan", "goat", "hat" };
string itemToRemove = "fan";
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == itemToRemove)
{
arr[i]=null;
break;
}
}

Initializing jagged arrays

I want to create array 10 * 10 * 10 in C# like int[][][] (not int[,,]).
I can write code:
int[][][] count = new int[10][][];
for (int i = 0; i < 10; i++)
{
count[i] = new int[10][];
for (int j = 0; j < 10; j++)
count[i][j] = new int[10];
}
but I am looking for a more beautiful way for it. May be something like that:
int[][][] count = new int[10][10][10];
int[][][] my3DArray = CreateJaggedArray<int[][][]>(1, 2, 3);
using
static T CreateJaggedArray<T>(params int[] lengths)
{
return (T)InitializeJaggedArray(typeof(T).GetElementType(), 0, lengths);
}
static object InitializeJaggedArray(Type type, int index, int[] lengths)
{
Array array = Array.CreateInstance(type, lengths[index]);
Type elementType = type.GetElementType();
if (elementType != null)
{
for (int i = 0; i < lengths[index]; i++)
{
array.SetValue(
InitializeJaggedArray(elementType, index + 1, lengths), i);
}
}
return array;
}
You could try this:
int[][][] data =
{
new[]
{
new[] {1,2,3}
},
new[]
{
new[] {1,2,3}
}
};
Or with no explicit values:
int[][][] data =
{
new[]
{
Enumerable.Range(1, 100).ToArray()
},
new[]
{
Enumerable.Range(2, 100).ToArray()
}
};
There is no built in way to create an array and create all elements in it, so it's not going to be even close to how simple you would want it to be. It's going to be as much work as it really is.
You can make a method for creating an array and all objects in it:
public static T[] CreateArray<T>(int cnt, Func<T> itemCreator) {
T[] result = new T[cnt];
for (int i = 0; i < result.Length; i++) {
result[i] = itemCreator();
}
return result;
}
Then you can use that to create a three level jagged array:
int[][][] count = CreateArray<int[][]>(10, () => CreateArray<int[]>(10, () => new int[10]));
With a little help from Linq
int[][][] count = new int[10].Select(x => new int[10].Select(x => new int[10]).ToArray()).ToArray();
It sure isn't pretty and probably not fast but it's a one-liner.
There is no 'more elegant' way than writing the 2 for-loops. That is why they are called 'jagged', the sizes of each sub-array can vary.
But that leaves the question: why not use the [,,] version?
int[][][] count = Array.ConvertAll(new bool[10], x =>
Array.ConvertAll(new bool[10], y => new int[10]));
A three dimensional array sounds like a good case for creating your own Class. Being object oriented can be beautiful.
You could use a dataset with identical datatables. That could behave like a 3D object (xyz = row, column, table)... But you're going to end up with something big no matter what you do; you still have to account for 1000 items.
Why don't you try this?
int[,,] count = new int[10, 10, 10]; // Multi-dimentional array.
Any problem you see with this kind of representation??

Categories

Resources