How to delete an element from an array[,]? - c#

I want to remove an element of this array
int[,] numbers = { {1,0} , {3,4} , {9,2} , {4,0} };
int[,] Remove = {{4,0}};

You cannot remove items from an Array because they are a fixed size I would use:
List<Tuple<int,int>>
this way you still have a list of two dimensional objects with the ability to remove them as well using
List.Remove()

You can't - arrays are fixed size. You can set the value to null. Best is to use generic collections such as List<int[]> which will allow you to add, insert and remove values.

Related

How to make the array size a ReadLine?

I'm trying to make a sorting algorithm and asking the user to input the array size is a must. I am a beginner in C# so I don't have any idea how to do that.
This is the idea that came to my mind, but I'm having an error.
Console.WriteLine("Enter how many elements you want to be sorted:");
a = Convert.ToInt32(Console.ReadLine());
int[] MyArray= new int[a] {""};
Visual Studio says that 'a constant value is expected'. How could I make the array length a ReadLine? My goal is for the user to decide which array length they want the program to show and that the elements inside the array would be system generated based on the array length that the user chose.
You can initialise an array like this:
int[] MyArray= new int[a];
But, I would also point out, that you could use a dynamic collection (such as a list), then you don't need to ask up front how many items, you just keep adding items until the user decides to stop.
ICollection<int> myCollection = new List<int>();
myCollection.Add(1);
myCollection.Add(1);
The type of your array is int but you are trying to initialize it with an empty string. In C# you can declare an array in a few ways. You can declare it by providing a size as in:
int[] myArray = new int[size];
And initialize the values later.
An alternative is to instantly intialize it with values like this:
int[] myArray = new int[] { 1, 2, 3 };
Note that when using the second option, you shouldn't provied the size as the compiler will infere it.

C# find matching pairs of arrays (when items inside are mixed)

I would like to ask if there is a data structure or some sort of dictionary that would help me to solve this problem.
First I create array of arrays of int.
11,32,21,10;
455,476,465,454;
11,32,476,455;
...
10,32,21,11;
Besides that I am adding points to separate array of arrays
Pt11,Pt32,Pt21,Pt10;
Pt455,Pt476,Pt465,Pt454;
Pt11,Pt32,Pt476,Pt455;
...
Pt10,Pt32,Pt21,Pt11;
Is there a way to create a dictionary to add array of points by name, and the name is array of integers - 10,32,21,11 - .
But the problem I have, is that I want to add to the same dictionary point array if name is mixed - 11,32,21,10.
So dictionary would point to the same collection - if I call 10,32,21,11 or 11,32,21,10. In other words 10,32,21,11 and 11,32,21,10 is the same name because elements are just ordered differently.
I do not know if it clear as I mixing several things:
1. Is it possible to create at least a dictionary whose name is an array?
2. Then if yes it probably would not point to the same points array if I add elements dictionary.Add(10,32,21,11, pt[]); dictionary.Add(11,32,21,10, pt[]);

Storing lots of Arrays into a List on every Loop

I have a question about storing lots of arrays into a list.
First, I initialize the array and list:
int[] arr = new int[9];
List<int[]> forkarr = new List<int[]>();
then I run through a lot of for loops and modify the array each time in order to produce all possible tic tac toe variations. While looping through all those variations, if the array is a possible board then I print it, and additionally if the array meets certain criteria for being a 'fork', then I will add it to the list like this:
if (ForkCheck.fork(arr)) { forkarr.Add(arr); Console.WriteLine("It's a Fork!");}
which also prints that message letting you know that particular array is a fork.
Now, when I am printing all of the arrays, the ones that are forks are printed properly and labeled as such.
However, when I go to print out all of the int[] elements of my list forkarr like so:
foreach (int[] arry in forkarr)
{
PrintGame.print(arry);//this is my method that prints the array
Console.WriteLine();
}
for some reason each array becomes an identical:
222
222
222
(I'm using 2 as 'X' and 1 as 'O' and 0 as 'empty' btw)
And it just prints that out over and over for all the forks.
So, something is going wrong when I am adding each modification of the array as a new element in the list I think, but I'm not sure why.
Thanks for any help.
Because you are modifying the same array and adding it to the list. Each time you done with one array array you need to create a new instance like this:
arr = new int[9];
This will create a new reference which will be independent from other arrays. And modifying it's elements wont affect the others.
For more information about value vs reference types you can refer to the question below:
What is the difference between a reference type and value type in c#?

Why changing one array changes another array in C#?

I have a two dimensional array namely States in C#. I build a one dimensional array, namely SubState, from States. When I change SubState, States changes too. I want States be unchanged. Thanks
int[] SubState = State [0];
SubState[0]-=1; //State[0][0] is also changed here
In my mind your State definition is:
int[][] State;
Array is a reference type and when you copy an element from the State array you get a reference to the array from the first list and the both references map to the same int[] array. So when you change array stored at SubArray you use a link to the same array.
The simple fix is a copy of the source array
var SubState = State[0].ToArray();
That is because when you assign the array you pass its reference you are not making a copy of it. Look at the msdn link on the array copy method https://msdn.microsoft.com/en-us/library/System.Array.Copy(v=vs.110).aspx
int[] SubState = State [0]; is just another reference to the state array and so can be changed via it as well.
What you probably want to do, is create a separate array from the state array like
int[] substate = new int[state.GetLength(0)];
state.CopyTo(substate, 0);
You are not building a new one-dimensional array. You are simply creating a new reference to the first row of your two-dimensional array. If you actually want to build a new one-dimensional array, you have to iteratively copy the first row of your two-dimensional array.
Try:
int[] SubState = new int[States[0].length];
States[0].CopyTo(SubState, 0);
instead of
int[] SubState = State [0];
try
int[] SubState = new int[State[0].Length];
Array.Copy(State[0],Substate, Substate.Length)
So you are not simply assigning a new reference, but are actually copying the array correctly
Obviously your element at position 0 of State is an array of int which is a reference-type. Thus both SubState and State reference the same array which is why changes to any of their elements are reflected by both. To overcome this problem you may create a copy of your State-array and copy its values to SubState:
States.CopyTo(SubStates, 0);
EDIT: Thius assumes that SubStates was already initialized with the same size as States. e.g:
int[] SubStates = new int[States[0].Length];

refresh array element

i am using array control in which i am saving value one by one.
now i have to delet one of the element and refresh it simultaneuosly.
for example....
string[] arr= new string(25);
arr[0]="A";
arr[1]="B";
arr[2]="C"; and so on....
now after deleting second element via arr[1]=null;
i want refreshed array like mentioned below...
arr[0]="A";
arr[1]="C"; and so on....
please help...
thanks in advance,,,
It sounds like you should be using a List<string> rather than an array, this would give exactly the functionality you are describing.
Although arrays can be resized (thanks #Austin Brunkhorst), this is not "cheap" and you would you would need to move everything around yourself.
It should be noted, that with lots of inserts and removes Lists can get very inefficient, so you'd be better off with a LinkedList<string>. These have advantages and disadvantages. Google linked list for more info.
When you have a static data amount you should use Array, BUT when you have dinamic data amount you should use List<>.
If you want to resize arrays, you have to create a new and copy all elements from the old to the new one.
arr = arr.Where(s => s != null).ToArray();
If you would use a List<string> you could use methods like List.Remove or List.RemoveAt.
If you'll be adding/deleting entries at arbitrary positions in your collection a lot, you'd be better off using a LinkedList<string> instead
Instead of Array you can go with List
List<int> list = new List<int>();
list.Add(2);
list.Add(3);
list.Add(5);
list.Add(7);
you will get more options like
Contains
Exists
IndexOf
For Removing the items you will get the functions like
Remove
ex: dogs.Remove("bulldog"); // Remove bulldog
RemoveAt
ex: list.RemoveAt(1);
RemoveAll

Categories