C# Object reference not set to an instance of an object - c#

I am getting this NullReferenceException in the second execution of the while loop of the changeColors function.
public class myClass {
Tuple<int,int>[] theArray = new Tuple<int, int>[100];
}
public myClass() {
theArray = null;
}
public Tuple<int,int>[] findRedPixels(){
Tuple<int,int>[] myArray = new Tuple<int, int>[100];
int k = 0;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if (graph.pixelMap [i, j].color == "red") {
myArray[k]= new Tuple<int,int>(i,j);
k++;
}
}
}
return myArray;
}
public void changeColors(){
while (true) {
theArray = findRedPixels();
foreach (var item in theArray) {
//error appears here in the second time it executes the while loop
Console.WriteLine (item.Item1 );
}
}
}

You should not return array from function findRedPixels as you have done because that array will already be initialized with 100 elements, try using List as it provide you flexibility and you can increase decrease size on fly may be something like this
public Tuple<int,int>[] findRedPixels(){
List<Tuple<int,int>> myArray = new List<Tuple<int, int>>();
int k = 0;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if( graph.pixelMap [i, j].color=="red"){
myArray.Add( new Tuple<int,int>(i,j));
k++;
}
}
}
return myArray.ToArray();
}

Related

How to replace duplicates in an array with 0

I have two arrays that have 20 randomly assigned elements and I need to create a method that replaces the duplicate numbers in an array with zeros but when I display the output nothing is changed.
This is the method for removing the repeating numbers. Is there a problem with the way the for loop is set up?
public static void RemoveDuplicates (int [] xArray){
for (int i = 0; i > xArray.Length-1; i++){
if (xArray[i] == xArray[i+1])
xArray[i] = 0;
}
}
and this is the whole thing
using System;
class MainClass {
public static void Main (string[] args) {
int [] student1 = new int [20];
int [] student2 = new int [20];
//int [] both = new int [40];
FillArray(student1);
FillArray(student2);
//Console.WriteLine("---------- Unsorted ----------");
//DisplayOutput(student1,student2);
Sort2Arrays(student1,student2);
//Console.WriteLine("---------- Sorted ----------");
//DisplayOutput(student1,student2);
RemoveDuplicates(student1);
RemoveDuplicates(student2);
DisplayOutput(student1, student2);
Sort2Arrays(student1,student2);
Console.WriteLine("---------- 1 and 2 no duplicates ----------");
DisplayOutput(student1, student2);
Console.WriteLine("done");
}//end main
public static void FillArray (int [] xArray){
Random rnd = new Random();
for(int i = 0; i< xArray.Length; i++){
xArray.SetValue (rnd.Next(80, 101),i);
}//end for
}//end FillArray
public static void Sort2Arrays (int [] xArray, int [] yArray){
Array.Sort(xArray);
Array.Sort(yArray);
}//end Sort2Arrays
public static void DisplayOutput (int [] xArray, int [] yArray){
for(int i = 0; i< 20; i++){
Console.WriteLine("{0}-{1}",xArray.GetValue(i),yArray.GetValue(i));
}//end for
}//end DisplayOutput
public static void RemoveDuplicates (int [] xArray){
for (int i = 0; i > xArray.Length-1; i++){
if (xArray[i] == xArray[i+1])
xArray[i] = 0;
}
}
}
Your for-loop condition is wrong, not >:
for (int i = 0; i > xArray.Length - 1; i++){
but <
for (int i = 0; i < xArray.Length - 1; i++)
with your version you will never enter the loop.
May i suggest you a different way to replace the duplicates(what you actually do), which is more efficient, more readable and more reusable:
public static void ReplaceDuplicates<T>(IList<T> xArray, T replaceWith)
{
HashSet<T> set = new HashSet<T>();
for (int i = 0; i < xArray.Count; i++)
{
if(!set.Add(xArray[i]))
{
xArray[i] = replaceWith;
}
}
}
It also doesn't need to sort the collection.
You use it in this way:
ReplaceDuplicates(student1, 0);
ReplaceDuplicates(student2, 0);
the > shouldve been <
public static void RemoveDuplicates (int [] xArray){
for (int i = 0; i < xArray.Length - 2; i++){
if (xArray[i] == xArray[i+1])
xArray[i] = 0;
}//end for
}//end RemoveDuplicates
public static void RemoveDuplicates(int[] array)
{
var foundValues = new HashSet<int>();
for (var index = 0; index < array.Length; index++)
{
var currentValue = array[index];
if (foundValues.Contains(currentValue))
{
array[index] = 0;
}
else
{
foundValues.Add(currentValue);
}
}
}

C# Sort 2D array based on key array

I'm trying to sort 2D array of chars based on 1D array as key. I need to find a char that wasn't used in key so far and then sort it based on it.
Fe:
AEITY
EDNTB
to
TEAIY
TDENB
based on key:
TEAIY
on row 0
My code struggle with an multiple chars and provide bad results:
Fe:
AAABN
TEAIY
TDENB
to
ABANA
TIEYA
TNDBE
but instead of that i get
ABANA
AIEYT
ENDBT
based on key:
ABANA
Code i figure out:
static char[][] SortSimiliarity(char[][] arr, char [] key, int arrRow)
{
Dictionary<char, int> dcKeyList = new Dictionary<char, int>();
for (var i = 0; i < key.Length; i++)
{
var counterCurrentKey = 0;
for (var j = 0; j < key.Length; j++)
{
if(key[i] == arr[arrRow][j])
{
//Console.WriteLine("{0} == {1}", key[i], arr[arrRow][j]);
if (!dcKeyList.ContainsKey(key[i]))
{
for (var k = 0; k < key.Length; k++)
{
//Console.WriteLine("x");
var temp = arr[k][j];
arr[k][j] = arr[k][i];
arr[k][i] = temp;
}
dcKeyList.Add(key[i], 1);
}
else
{
if (dcKeyList[key[i]] == counterCurrentKey)
{
Console.WriteLine("key is {0}", dcKeyList[key[i]]);
for (var k = 0; k < key.Length; k++)
{
//Console.WriteLine("x");
var temp = arr[k][j];
arr[k][j] = arr[k][i];
arr[k][i] = temp;
}
dcKeyList[key[i]]++;
}
counterCurrentKey++;
}
}
}
}
return arr;
}
I know there is something wrong with that ELSE statement, where i compare current count of char in key loop.
Thank you for your ideas.
My problem was simple. I tried to sort 2d array based on array key which i does, but i also save same result into same 2d array, without keeping tracks on changes. That can be solved with pointer logic or with new array where you copy only your sorted results without changing the base 2D array.
static char[][] SortSimiliarity(char[][] arr, char[] key, int arrRow)
{
// init dict
Dictionary<char, int> dict = new Dictionary<char, int>();
// init new matrix
char[][] tempArray = new char[5][];
for(var i = 0; i < 5; i++)
{
tempArray[i] = new char[5];
}
// copy new sorted keys
for (var i = 0; i < key.Length; i++)
{
for (var j = 0; j < key.Length; j++)
{
if (key[i] == arr[arrRow][j])
{
if (!dict.ContainsKey(key[i]))
{
dict.Add(key[i], j);
for(var k = 0; k < key.Length; k++)
{
tempArray[k][i] = arr[k][j];
}
break;
}
else
{
if(j != dict[key[i]])
{
for (var k = 0; k < key.Length; k++)
{
tempArray[k][i] = arr[k][j];
}
dict[key[i]] = j+1;
}
}
}
}
}
return tempArray;
}

Objects in List<T> reference same value

I have a list of custom objects List<Slot>
Each Object Slot has an array of Gene[]
Object slot
public class Slot
{
private Gene[] _genes;
private int _fitness;
//...
public Slot(int count)
{
_genes = InitializeArray<Gene>(count);
Fitness = 0;
}
public Gene[] getChromosomes()
{
return Genes; //getter method
}
//Helper to init array
static T[] InitializeArray<T>(int length) where T : new()
{
T[] array = new T[length];
for (int i = 0; i < length; ++i)
{
array[i] = new T();
}
return array;
}
}
Object Gene
public class Gene
{
private DateTime _date;
private int _tcode;
private byte _availabe;
private byte _duty;
private int _fitness;
//...
public Gene()
{
_fitness = 0;
}
}
Main
private List<Slot> slotsList = new List<Slot>();
//...
//...
private void init_population(int lchromeSize, int lpopulationSize)
{
slotsList.Clear();
Gene[] lstGene = InitializeArray<Gene>(lchromeSize);
//For all slots
for (int i = 0; i < tempInt; i++)
{
//for all genes
for (int ii = 0; ii < lchromeSize; ii++)
{
//assign values to local variables
// and :
lstGene[ii].Date = ldate;
lstGene[ii].Tcode = lteacherCode;
lstGene[ii].Availabe = lavailable;
lstGene[ii].Duty = tempDuty;
lstGene[ii].Fitness = 0;
}
//End ii For
//Add the genes to slotsList
Slot itemtoadd = new Slot(lchromeSize);
itemtoadd.setChromosomes(lstGene);
slotsList.Add(itemtoadd);
}
}
The problem is that in every single Slot the Genes are identical and they reference the last lstGene[] that has been added to slotsList.
Where did I mess up it again ?
You should create new array for each itemtoadd.
//For all slots
for (int i = 0; i < tempInt; i++)
{
//for all genes
Gene[] lstGene = InitializeArray<Gene>(lchromeSize);
for (int ii = 0; ii < lchromeSize; ii++)
{
//assign values to local variables
// and :
lstGene[ii].Date = ldate;
lstGene[ii].Tcode = lteacherCode;
lstGene[ii].Availabe = lavailable;
lstGene[ii].Duty = tempDuty;
lstGene[ii].Fitness = 0;
}
//End ii For
//Add the genes to slotsList
Slot itemtoadd = new Slot(lchromeSize);
itemtoadd.setChromosomes(lstGene);
slotsList.Add(itemtoadd);
}
You need to move this line:
Gene[] lstGene = InitializeArray<Gene>(lchromeSize);
to be inside the for (int i = .. loop. You are now re-using the same array for every slot - which is what you see.
Why don't you initialize your Gene[] lstGene inside a loop? Otherwise you are still referencing the same array over and over again if I am not mistaken

Best way to convert int[][] to int**?

This is kind of a curiosity question. I do have an existing solution, but I wonder if people know of a better approach.
My callers want to call me with int[][]. I have a routine that needs to process an int**. What's the best way to do this conversion? In other words:
public static void Func1(int[][] data) {
Func2(data); //how to do this?
}
private unsafe static void Func2(int** data) {
//process data
}
Following is the best approach I could come up with. It works fine, but I can't say I'm 100% happy with the recursive approach (and the O(data.Length) stack space it requires)
public static void Main() {
var test=new[] {
new [] {10},
new [] {20,30},
new [] {40,50,60},
new [] {70,80,90,100},
};
MySolution_Func1(test);
}
public unsafe static void MySolution_Func1(int[][] data) {
var items=new int*[data.Length];
Recurse(0, data, items);
}
public unsafe static void Recurse(int index, int[][] data, int*[] build) {
if(index==data.Length) {
fixed(int** finalp=build) {
Func2(finalp);
}
} else {
fixed(int* nextp=data[index]) {
build[index]=nextp;
Recurse(index+1, data, build);
}
}
}
private unsafe static void Func2(int** data) {
for(var j=0; j<4; ++j) {
for(var i=0; i<j+1; ++i) {
Debug.WriteLine("{0},{1}: {2}", j, i, data[j][i]);
}
}
}
There's no need to copy the whole array. You can create an array of pointers (i.e. IntPtr[]), and then cast that to int**. It's not pretty, and I wouldn't suggest doing it. But it can be done. The code below shows how.
int[][] myArray = new int[10][];
for (int i = 0; i < 10; ++i)
{
int[] x = new int[10];
for (int j = 0; j < 10; ++j)
{
x[j] = 10 * i + j;
}
myArray[i] = x;
}
// Output the array
Console.WriteLine("int[][]");
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
Console.Write("{0}, ", myArray[i][j]);
}
Console.WriteLine();
}
// Convert to int*[]
unsafe
{
GCHandle[] handles = new GCHandle[10];
IntPtr[] ArrayOfPointer = new IntPtr[10];
for (int i = 0; i < 10; ++i)
{
handles[i] = GCHandle.Alloc(myArray[i], GCHandleType.Pinned);
ArrayOfPointer[i] = handles[i].AddrOfPinnedObject();
}
// Okay, let's output that
Console.WriteLine("int*[]");
for (int i = 0; i < 10; ++i)
{
int* p = (int*)ArrayOfPointer[i];
for (int j = 0; j < 10; ++j)
{
Console.Write("{0}, ", *p);
++p;
}
Console.WriteLine();
}
// now convert to int**
GCHandle bigHandle = GCHandle.Alloc(ArrayOfPointer, GCHandleType.Pinned);
int** ppInt = (int**)bigHandle.AddrOfPinnedObject();
// and output it
Console.WriteLine("int**");
int** pa = ppInt;
for (int i = 0; i < 10; ++i)
{
int* p = *pa;
for (int j = 0; j < 10; ++j)
{
Console.Write("{0}, ", *p);
++p;
}
Console.WriteLine();
++pa;
}
// Need to free the handles
bigHandle.Free();
for (int i = 0; i < 10; ++i)
{
handles[i].Free();
}
}
public unsafe void ConvertToNative(int[][] jarray, out int** ptr)
{
ptr= (int**)Marshal.AllocHGlobal(jarray.Length*sizeof(int));
for (int i = 0; i < jarray.Length; i++)
{
*(ptr+i) = (int*)Marshal.AllocHGlobal(jarray[i].Length*sizeof(int));
for (int j = 0; j < jarray[i].Length; j++)
{
(*(i + ptr))[j] = jarray[i][j];
}
}
}
This works but uses unmanaged memory, and there is no recursion, is that valid ?
You can do it with O(1) stack space, if you are willing to copy all the data into another buffer:
public unsafe static void AlternateSolution_Func1(int[][] data) {
var buffer=new int[data.Sum(a => a.Length)];
fixed(int* pBuffer=buffer) {
var items=new int*[data.Length];
int count=0;
for(int i=0; i<data.Length; ++i) {
items[i]=pBuffer+count;
var array=data[i];
for(int j=0; j<array.Length; ++j) {
pBuffer[count++]=array[j];
}
}
fixed(int** pItems=items) {
Func2(pItems);
}
}
}

How do I 'foreach' through a two-dimensional array?

I've got a two-dimensional array,
string[,] table = {
{ "aa", "aaa" },
{ "bb", "bbb" }
};
And I'd like to foreach through it like this,
foreach (string[] row in table)
{
Console.WriteLine(row[0] + " " + row[1]);
}
But, I get the error:
Can't convert type string to string[]
Is there a way I can achieve what I want, i.e. iterate through the first dimension of the array with the iterator variable returning me the one-dimensional array for that row?
Multidimensional arrays aren't enumerable. Just iterate the good old-fashioned way:
for (int i = 0; i < table.GetLength(0); i++)
{
Console.WriteLine(table[i, 0] + " " + table[i, 1]);
}
As others have suggested, you could use nested for-loops or redeclare your multidimensional array as a jagged one.
However, I think it's worth pointing out that multidimensional arrays are enumerable, just not in the way that you want. For example:
string[,] table = {
{ "aa", "aaa" },
{ "bb", "bbb" }
};
foreach (string s in table)
{
Console.WriteLine(s);
}
/* Output is:
aa
aaa
bb
bbb
*/
If you define your array like this:
string[][] table = new string[][] {
new string[] { "aa", "aaa" },
new string[]{ "bb", "bbb" }
};
Then you can use a foreach loop on it.
UPDATE: I had some time on my hands, so ... I went ahead and fleshed out this idea. See below for the code.
Here's a bit of a crazy answer:
You could do what you're looking for -- essentially treat a two-dimensional array as a table with rows -- by writing a static method (perhaps an extension method) that takes a T[,] and returns an IEnumerable<T[]>. This would require copying each "row" of the underlying table into a new array, though.
A perhaps better (though more involved) approach would be to actually write a class that implements IList<T> as a wrapper around a single "row" of a two-dimensional array (you would probably set IsReadOnly to true and just implement the getter for the this[int] property and probably Count and GetEnumerator; everything else could throw a NotSupportedException). Then your static/extension method could return an IEnumerable<IList<T>> and provide deferred execution.
That way you could write code pretty much like what you have:
foreach (IList<string> row in table.GetRows()) // or something
{
Console.WriteLine(row[0] + " " + row[1]);
}
Just a thought.
Implementation suggestion:
public static class ArrayTableHelper {
public static IEnumerable<IList<T>> GetRows<T>(this T[,] table) {
for (int i = 0; i < table.GetLength(0); ++i)
yield return new ArrayTableRow<T>(table, i);
}
private class ArrayTableRow<T> : IList<T> {
private readonly T[,] _table;
private readonly int _count;
private readonly int _rowIndex;
public ArrayTableRow(T[,] table, int rowIndex) {
if (table == null)
throw new ArgumentNullException("table");
if (rowIndex < 0 || rowIndex >= table.GetLength(0))
throw new ArgumentOutOfRangeException("rowIndex");
_table = table;
_count = _table.GetLength(1);
_rowIndex = rowIndex;
}
// I didn't implement the setter below,
// but you easily COULD (and then set IsReadOnly to false?)
public T this[int index] {
get { return _table[_rowIndex, index]; }
set { throw new NotImplementedException(); }
}
public int Count {
get { return _count; }
}
bool ICollection<T>.IsReadOnly {
get { return true; }
}
public IEnumerator<T> GetEnumerator() {
for (int i = 0; i < _count; ++i)
yield return this[i];
}
// omitted remaining IList<T> members for brevity;
// you actually could implement IndexOf, Contains, etc.
// quite easily, though
}
}
...now I think I should give StackOverflow a break for the rest of the day ;)
It depends on how you define your multi-dimensional array. Here are two options:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
// First
string[,] arr1 = {
{ "aa", "aaa" },
{ "bb", "bbb" }
};
// Second
string[][] arr2 = new[] {
new[] { "aa", "aaa" },
new[] { "bb", "bbb" }
};
// Iterate through first
for (int x = 0; x <= arr1.GetUpperBound(0); x++)
for (int y = 0; y <= arr1.GetUpperBound(1); y++)
Console.Write(arr1[x, y] + "; ");
Console.WriteLine(Environment.NewLine);
// Iterate through second second
foreach (string[] entry in arr2)
foreach (string element in entry)
Console.Write(element + "; ");
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Press any key to finish");
Console.ReadKey();
}
}
}
Here's a simple extension method that returns each row as an IEnumerable<T>. This has the advantage of not using any extra memory:
public static class Array2dExt
{
public static IEnumerable<IEnumerable<T>> Rows<T>(this T[,] array)
{
for (int r = array.GetLowerBound(0); r <= array.GetUpperBound(0); ++r)
yield return row(array, r);
}
static IEnumerable<T> row<T>(T[,] array, int r)
{
for (int c = array.GetLowerBound(1); c <= array.GetUpperBound(1); ++c)
yield return array[r, c];
}
}
Sample usage:
static void Main()
{
string[,] siblings = { { "Mike", "Amy" }, { "Mary", "Albert" }, {"Fred", "Harry"} };
foreach (var row in siblings.Rows())
Console.WriteLine("{" + string.Join(", ", row) + "}");
}
string[][] table = { ... };
string[][] languages = new string[2][];
languages[0] = new string[2];
languages[1] = new string[3];
// inserting data into double dimensional arrays.
for (int i = 0; i < 2; i++)
{
languages[0][i] = "Jagged"+i.ToString();
}
for (int j = 0; j < 3; j++)
{
languages[1][j] = "Jag"+j.ToString();
}
// doing foreach through 2 dimensional arrays.
foreach (string[] s in languages)
{
foreach (string a in s)
{
Console.WriteLine(a);
}
}
Using LINQ you can do it like this:
var table_enum = table
// Convert to IEnumerable<string>
.OfType<string>()
// Create anonymous type where Index1 and Index2
// reflect the indices of the 2-dim. array
.Select((_string, _index) => new {
Index1 = (_index / 2),
Index2 = (_index % 2), // ← I added this only for completeness
Value = _string
})
// Group by Index1, which generates IEnmurable<string> for all Index1 values
.GroupBy(v => v.Index1)
// Convert all Groups of anonymous type to String-Arrays
.Select(group => group.Select(v => v.Value).ToArray());
// Now you can use the foreach-Loop as you planned
foreach(string[] str_arr in table_enum) {
// …
}
This way it is also possible to use the foreach for looping through the columns instead of the rows by using Index2 in the GroupBy instead of Index 1. If you don't know the dimension of your array then you have to use the GetLength() method to determine the dimension and use that value in the quotient.
I'm not a big fan of this method because of the memory usage involved, but if you use the arrays it produces, it isn't such a waste.
public static void ForEachRow<T>(this T[,] list, Action<int, T[]> action)
{
var len = list.GetLength(0);
var sub = list.GetLength(1);
T[] e;
int i, j;
for (i = 0; i < len; i++)
{
e = new T[sub];
for (j = 0; j < sub; j++)
{
e[j] = list[i, j];
}
action(i, e);
}
}
Implementation:
var list = new[,]{0x0, 0x1, 0x2, 0x4, 0x8};
list.ForEachRow((i, row) =>
{
for (var j = 0; j < row.Length; j++)
{
Console.WriteLine("[{0},{1}]: {2}", i, j, row[j]);
}
});
The other solution I found is less memory intensive, but will use more CPU, especially when the dimensions of the arrays' entries are larger.
public static void ForEachRow<T>(this T[,] list, Action<int, IEnumerable<T>> action)
{
var len = list.GetLength(0);
var sub = list.GetLength(1);
int i, j;
IEnumerable<T> e;
for (i = 0; i < len; i++)
{
e = Enumerable.Empty<T>();
for (j = 0; j < sub; j++)
{
e = e.Concat(AsEnumerable(list[i, j]));
}
action(i, e);
}
}
private static IEnumerable<T> AsEnumerable<T>(T add)
{
yield return add;
}
Implementation:
var list = new[,]{0x0, 0x1, 0x2, 0x4, 0x8};
list.ForEachRow((i, row) =>
{
var j = 0;
forrach (var o in row)
{
Console.WriteLine("[{0},{1}]: {2}", i, j, o);
++j;
}
});
As a whole, I find the first option to be more intuitive, especially if you want to access the produced array by its indexer.
At the end of the day, this is all just eye candy, neither methods should really be used in favour of directly accessing the source array;
for (var i = 0; i < list.GetLength(0); i++)
{
foreach (var j = 0; j < list.GetLength(1); j++)
{
Console.WriteLine("[{0},{1}]: {2}", i, j, list[i, j]);
}
}
Remember that a multi-dimensional array is like a table. You don't have an x element and a y element for each entry; you have a string at (for instance) table[1,2].
So, each entry is still only one string (in your example), it's just an entry at a specific x/y value. So, to get both entries at table[1, x], you'd do a nested for loop. Something like the following (not tested, but should be close)
for (int x = 0; x < table.Length; x++)
{
for (int y = 0; y < table.Length; y += 2)
{
Console.WriteLine("{0} {1}", table[x, y], table[x, y + 1]);
}
}
I try this. I hope to help. It work with
static void Main()
{
string[,] matrix = {
{ "aa", "aaa" },
{ "bb", "bbb" }
};
int index = 0;
foreach (string element in matrix)
{
if (index < matrix.GetLength(1))
{
Console.Write(element);
if (index < (matrix.GetLength(1) - 1))
{
Console.Write(" ");
}
index++;
}
if (index == matrix.GetLength(1))
{
Console.Write("\n");
index = 0;
}
}

Categories

Resources