I need to create 2D jagged array. Think of a matrix. The number of rows is known, the number of columns is not known. For example I need to create array of 10 elements, where type of each element string[]. Why do I need that? The number of columns is not known - this function must simply do the allocation and pass array to some other function.
string[][] CreateMatrix(int numRows)
{
// this function must create string[][] where numRows is the first dimension.
}
UPDATE
I have C++ background. In C++ I would write the following (nevermind the syntax)
double ** CreateArray()
{
double **pArray = new *double[10]() // create 10 rows first
}
UPDATE 2
I was considering using List, but I need to have indexed-access to both rows and columns.
return new string[numRows][];
Cannot be done. However you could do something like this:
List<List<string>> createMatrix(int numRows)
{
return new List<List<string>>(numRows);
}
That allows you to be able to have a flexible amount of objects in the second dimesion.
You can write:
string[][] matrix = new string[numRows][];
and that will produce a 2D array of null elements. If you want to fill the array with non-null items, you need to write:
for (int row = 0; row < numRows; row++)
{
matrix[row] = new string[/* col count for this row */];
}
You need to specify the column count for each row of the matrix. If you don't know it in advance, you can either.
Leave the matrix items unintialised, and populate them as you know their size.
Use a fixed number that will give you room enough for the maximum possible size.
Avoid arrays and use List<> as suggested by others here.
Hope this helps.
Related
I've ran into an issue with lists as I need to create a 2-dimensional list where I can read data by giving the columns and rows, so I could read from my list by using my_List[col][row] so is it possible making a 2D list that way?
How much performance impact can this have and anything I should be aware that could have a performance impact on the code? I might need to read a few hundred times per second from my 2D list
Is it possible to have a more grid type 2D list so if i have data in 3, 4 and 5 but i dont have anything in 0, 1, and 2 think of it like coordinates. so can I read from the list using myList[3][5] and get the data from there with 0, 1 and 2 having nothing? or do i need to loop it through and add something there like null?
thanks in advance!
Yes, you can indeed use multidimensional arrays or jagged arrays for storing "2D data".
As for creating a data structure that doesn't use any memory space for unused indexes, an option could be to use a dictionary where the keys are tuples of two numbers, like this (assuming that your data are strings):
var items = new Dictionary<(int, int), string>();
items.Add((0,1), "0-1"); //this throws an error if the key already exists
items[(2,3)] = "2-3"; //this silently replaces the value if the key already exists
Console.WriteLine(items.Keys.Contains((0,1))); //true
Console.WriteLine(items.Keys.Contains((0,2))); //false
Console.WriteLine(items[(2,3)]); //"2-3"
Of course you probably want to encapsulate this functionality in its own class, but you get the idea.
Note however that this dictionary approach will probably be worse than a plain array in terms of performance, but it's up to you to experiment and collect some metrics.
You can create 2D Arrays like this :
string[,] twoDArray = new string[2,2];
Then you can loop through it like :
for (int i = 0; i < twoDArray.Length; i++)
{
foreach (int j in twoDArray[i,0])
{
}
}
You can also create 2D Lists like this:
List<List<string>> grid = new List<List<string>>();
and iterate through them using an Enumerator and for example a for loop:
var enu = grid.GetEnumerator();
while (enu.MoveNext())
{
for(int i = 0; i < enu.Current.Count; i++)
{
enu.Current.RemoveAt(i);
}
}
You are basically iterating over all lists and then through each list as long as its size is. Inside the for loop you can alter the encapsuled lists in whatever way you like.
I am trying to create 2-dimensional array of ints with two columns, but an unknown amount of rows. I know that in order to create the 2D array itself I do the following:
List<List<int>> myList = new List<List<int>>();
But how do I modify this to specify the number of columns? And how would I add a row to this array?
You could use a List of int arrays. The List would represent the rows, and each int array in the List would be one row. The length of each array would equal the amount of columns.
List<int[]> rows = new List<int[]>();
int[] row = new int[2];
row[0] = 100;
row[1] = 200;
rows.Add(row);
As long as each int array is length two (int[] row = new int[2]), all rows will have two columns. The List can have any number of int arrays added to it in this way.
There is no way to create 2D array (or any other sort of array) with unknown number of elements. Once you initialize it you have to provide number of elements.
The syntax for multidimensional array is the following:
var arr = new int[k, l, n,...]
You can create so called jagged array, i.e. array of arrays and initialize it in the cycle. You will still need to initialize it with a number of subarrays and then fill with those subarrays of given lengths:
var arr = new int[][n];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = new int[subArrayLength];
}
What you actually do is List of Lists which can have any number of "rows" of any length. You can add new list of specific length to an outer list by List method Add() and the same way you can add an element to any of inner List.
So basically to add a "row" you would need the following:
List<List<int>> table = new List<List<int>>();
table.Add(Enumerable.Repeat(defaultValue, numberOfColumns).ToList());
To add a column you would need something like this:
foreach (var row in table)
{
row.Add(defaultValue);
}
It seems that you want to simulate table structure - for that I would suggest to create a class to incapsulate table logic from above inside so that any addition of row will cause addition of outer List of current numberOfColumns size and addition of column will cause addition of an element to all outer lists.
However if you need a fixed number of columns the best alternative you can use is to declare a new class with your columns mapped to the properties of the class and then simply declare a List like it is described in the following answer as #shash678 pointed
Original answer: Consider using a List<Tuple<int, int>>.
Explanation: To begin with, an int[] is not a List<>. From the spirit of your question you seem to have the need to group several units of data together with a finite size, like a records in a table. You could decide to create a POCO with a more descriptive name (which would increase code readability and help with semantics), use an array of fixed size, or use a Tuple. Since there is no mention as to the need for mutability, and the size of the inner "array" will be fixed, I would suggest a Tuple. Through type safety you will ensure that the size and shape of each new object added to the list of Tuples is correct.
Your fist and second questions would be taken care, as far as the 3rd, see: Tuple Pair
e.g. list.Add(Tuple.Create<int,int>(1,1));
I feel like these answers are leading you down a bad path. When learning how to program, you should also consider best practices. In this case your code is what you should use. If you can avoid setting explicit array size, you should(In my opinion). List were create for this purpose. This sample app explains how to use the list correctly in this scenario. My personal opinion is to avoid using arrays if you can.
int someNumberOfRow = 10;//This is just for testing purposes.
Random random = new Random();//This is just for testing purposes.
List<List<int>> myList = new List<List<int>>();
//add two elements to the an arraylist<int> then add this arraylist to myList arraylist
for(int i = 0; i < someNumberOfRow; i++)
{
//Create inner list and add two ints to it.
List<int> innerList = new List<int>();
innerList.Add(random.Next());
innerList.Add(random.Next());
//Add the inner list to myList;
myList.Add(innerList);
}
//This prints myList
for(int i = 0; i < myList.Count; i++)
{
Console.WriteLine((i + 1) + ": " + myList[i][0] + " - " + myList[i][1]);
}
Console.WriteLine("\n\n");
//If the app may scale in the future, I suggest you use an approach similar to this
foreach(List<int> sublist in myList)
{
foreach(int columns in sublist)
{
Console.Write(columns + " ");
}
Console.WriteLine();
}
Console.ReadLine();
I am getting a matrix (from file.txt) of known size into the same size of 2D array. This code is fine for that.
But now I am really looking for extending this, like getting a matrix of unknown size into a 2D array, i.e. using a dynamically sized 2D array.
Here is the C# code I am modifying.
int[,] matrix = new int[3,3];
int i = 0, j = 0, k = 0;
#region Reading Matrices From Files
// Matrix 1 Manipulation...............
// Read the file as one string.
string text = System.IO.File.ReadAllText("file.txt");
//reads all the text of file of given path in a string
foreach (var row in text.Split('\n'))
{
//outter foreach loop is for setting number of rows equal to number lines by splittin over \n
j = 0;
foreach (var col in row.Trim().Split(' '))
{
//inner foreach loop is for setting number of columns equal to number chracters by splittin over space
matrix[i, j] = int.Parse(col.Trim());
j++;
}
i++;
}
Any help is appreciated....
An array is a fixed-sized data structure. It is well used in many languages. Unfortunately once created you may not modify its contents.
That is, how do Lists resize?
List is implemented with arrays, actually. So initially a list holds a fixed number of objects. When you want to add an object that exceeds this capacity, we copy the current array into an array of bigger size, thus increasing the capacity dynamically.
Using this idea, we can actually create your matrix class that automatically resizes from array, rather than List.
I hope the above set your mind thinking about how to create a matrix from arrays! If not, here are more hints:
keep two integers tracking the capacity of your current arrays. If you want to add an element that exceeds one (say you add an entry to a row but the row is already full), then you want to do a resize operation
resize operation consists of initialising a new array with bigger (usually double) size, then copy all the values
don't forget to check that all arrays are of the same size after resize! You might need a loop there
I have an array of tiles that are of the type Texture2D and want to set each one in the array to something different.
for (int i = 1; i <= columns * rows; i++)
{
m_tiles[i] = new Texture2D(m_graphicsDevice, tilewidth, tileheight);
}
It points to the error being the closing }
I don't understand how it being null when I'm trying to set it to not be null effects it. If I can never set the variable to anything then they'll always be null.
I have tried:
Texture2D[] m_tiles = new Texture2D(m_graphicsDevice, tilewidth, tileheight)[500];
But the compiler says "Cannot apply indexing with [] to and expression of type 'Microsoft.Xna.Framework.Graphics.Texture2D'"
You'll first need to initialize an array instance in order to assign values to its elements:
Preceed the for-loop with following statement:
Texture2D[] m_tiles = new Texture2D[columns * rows + 1];
Arrays indices are 0-based in C#, and afaik most .NET languages. So when using the indexer, you might want to loop from 0 .. n-1, instead of 1 .. n.
But honestly, I rarely ever still use arrays in .NET. If you have no specific reason to use an array, I would recommend to use a List<T> :
List<Texture2D> m_tiles = new List<Texture2D>();
for(;;)
{
m_tiles.Add(new Texture2D(foo, bar));
}
You need to instantiate the array first, like:
m_tiles = new Texture2D[10];
Like most other types, arrays need to be created, more specifically it needs to know how many elements you want it to have (in this case, it has 10 "slots").
You need to initialize the array with a proper dimension.
m_tiles[] may not be initialized to receive (columns * rows) elements.
So, before your loop, you should initialize the m_titles array with this size.
Texture2D[] m_tiles = new Texture2D[columns * rows];
So, if you have 1 columns and 1 rows, it will give you 1 slot (m_tiles[0]). If you have 2 columns and 2 rows you will have 4 slots (m_tiles[0],m_tiles[1],m_tiles[2],m_tiles[3]);
You should start you loop with i = 0 otherwise, the [0] won't be assigned and an exception of index out of bound will be trigged. If you really do not want to start with 0, you can increase by 1 the size of the array using (columns * rows +1).
I have a two dimensional array that I need to load data into. I know the width of the data (22 values) but I do not know the height (estimated around 4000 records, but variable).
I have it declared as follows:
float[,] _calibrationSet;
....
int calibrationRow = 0;
While (recordsToRead)
{
for (int i = 0; i < SensorCount; i++)
{
_calibrationSet[calibrationRow, i] = calibrationArrayView.ReadFloat();
}
calibrationRow++;
}
This causes a NullReferenceException, so when I try to initialize it like this:
_calibrationSet = new float[,];
I get an "Array creation must have array size or array initializer."
Thank you,
Keith
You can't use an array.
Or rather, you would need to pick a size, and if you ended up needing more then you would have to allocate a new, larger, array, copy the data from the old one into the new one, and continue on as before (until you exceed the size of the new one...)
Generally, you would go with one of the collection classes - ArrayList, List<>, LinkedList<>, etc. - which one depends a lot on what you're looking for; List will give you the closest thing to what i described initially, while LinkedList<> will avoid the problem of frequent re-allocations (at the cost of slower access and greater memory usage).
Example:
List<float[]> _calibrationSet = new List<float[]>();
// ...
while (recordsToRead)
{
float[] record = new float[SensorCount];
for (int i = 0; i < SensorCount; i++)
{
record[i] = calibrationArrayView.ReadFloat();
}
_calibrationSet.Add(record);
}
// access later: _calibrationSet[record][sensor]
Oh, and it's worth noting (as Grauenwolf did), that what i'm doing here doesn't give you the same memory structure as a single, multi-dimensional array would - under the hood, it's an array of references to other arrays that actually hold the data. This speeds up building the array a good deal by making reallocation cheaper, but can have an impact on access speed (and, of course, memory usage). Whether this is an issue for you depends a lot on what you'll be doing with the data after it's loaded... and whether there are two hundred records or two million records.
You can't create an array in .NET (as opposed to declaring a reference to it, which is what you did in your example) without specifying its dimensions, either explicitly, or implicitly by specifying a set of literal values when you initialize it. (e.g. int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };)
You need to use a variable-size data structure first (a generic list of 22-element 1-d arrays would be the simplest) and then allocate your array and copy your data into it after your read is finished and you know how many rows you need.
I would just use a list, then convert that list into an array.
You will notice here that I used a jagged array (float[][]) instead of a square array (float [,]). Besides being the "standard" way of doing things, it should be much faster. When converting the data from a list to an array you only have to copy [calibrationRow] pointers. Using a square array, you would have to copy [calibrationRow] x [SensorCount] floats.
var tempCalibrationSet = new List<float[]>();
const int SensorCount = 22;
int calibrationRow = 0;
while (recordsToRead())
{
tempCalibrationSet[calibrationRow] = new float[SensorCount];
for (int i = 0; i < SensorCount; i++)
{
tempCalibrationSet[calibrationRow][i] = calibrationArrayView.ReadFloat();
} calibrationRow++;
}
float[][] _calibrationSet = tempCalibrationSet.ToArray();
I generally use the nicer collections for this sort of work (List, ArrayList etc.) and then (if really necessary) cast to T[,] when I'm done.
you would either need to preallocate the array to a Maximum size (float[999,22] ) , or use a different data structure.
i guess you could copy/resize on the fly.. (but i don't think you'd want to)
i think the List sounds reasonable.
You could also use a two-dimensional ArrayList (from System.Collections) -- you create an ArrayList, then put another ArrayList inside it. This will give you the dynamic resizing you need, but at the expense of a bit of overhead.