Please guide me, are there any out of box data structures in C# that support dynamic length and two or more dimensions?
I tried to use a string array but length is an issue. I want some structure that can be built and support 2 dimensions, like some text and its value.
Please suggest solutions.
How about a List<List<T>>?
First of all, "two or more dimensions" isn't all that specific.
Do you need 2? Or 3? Or more? What is "more" anyway?
Also, do you need a rectangular data set like a 2 dimensional array? Or do you need one that has different widths for different rows?
Also, your "some text and its value" suggests you're not really looking at a 2 dimensional data structure, but a 1 dimensional data structure, containing elements that have multiple values.
The difference here is that a 2 dimensional data structure would have the same type of elements in both dimensions, whereas the other can have different types of elements, like an int and a string.
So perhaps you need to tell us a bit more about what you need to store in that structure.
Otherwise, List<List<T>> it is.
If you want key-value pairs you can use a Hashtable or NameValueCollection.
It's not clear what you're asking. There are many ways to use the generic collections to build 2-dimensional data structures. What is it that you really want?
If you're talking about text data that you want to split into lines, and those lines can be modified, then how about List<StringBuilder>? That will allow you to add and delete lines, and you can also modify the individual lines.
Related
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[]);
I have the following DataTable
ItemNumber Quantity Order
1 2 3
4 3 8
7 7 9
I would like to load first two columns into 2 dimensional array. If it is not possible, at least I would like to get first column into 1 dimension array.
Of course, I can write a loop, but I wonder if it is possible to avoid loops.
Regards,
You won't necessarily be able to avoid loops in the technical sense, but you could write something like this:
DataTable table = ...;
return table.Rows.Cast<DataRow>().Select(c => new[] { c[0], c[1] });
That's an array of arrays, rather than a multidimensional array. But from what I've seen multidimensional arrays seem to be phased out as standard practice in most applications. You might also want to parse each row in that Select to being some actual type.
If you leave this as an IEnumerable<>, it will not need to loop more than the once that you use it, but if you're reading at random points a lot (using the indexer, or what have you), you might want to append .ToArray(). That decision depends on how you're using it. As a general rule, it's nice to write code that doesn't depend on random access like that, since then you don't have to read from the DataTable more than once per record.
I have an array of type Cell. This is a 2D array.
Depending on certain conditions, I might have to resize this array and shift the Cell objects one row or column down/right/left.
I was thinking of using the prototype pattern to copy the original array into another larger array.
Does this offer any more benefits over simply doing an array resize?
Edit: I realized that I have not mentioned my intent. I don't really need another object. I just want a larger array based on certain conditions.
The only reason this could be of help is if you are making a shallow copy: copying would save you the costs of creating new objects. The flip side of it is that all Cell objects inside the array would be shared among multiple 2D arrays; if this presents a problem, you should go back to creating arrays from scratch.
Short answer No.
There is an alternative solution that will solve your problem without needing a prototype.
Don't store the cells in an array.
The cells don't know their row and column numbers but instead ask the row/column for it.
This allows the columns to be rearranged without having to update every item.
Have a container object that represents the array and can return the cell at a given (x,y) coordinate. If the array is sparce (or if it has a sensible default) then treat a missing cell as the default. This will massively reduce storage costs.
The columns and rows belong to the container. Adding an empty row or column now becomes a case of inserting a row or column object.
I have a project where I need to split data from a text file into an array or an "array of structure" as my professor said, and I'm pretty confused as to how to go about doing it.
Basically I have data arranged like this in a text file (doesn't have to be like this.. can be one thing per line):
PR214;MR43T;RBL8;14K22
PR223;R43;RJ6;14K24
PR224;R43N;RN4;14K30
PR246;R46N;RN8;14K32
PR247;R46TS;RBL17Y;14K33
PR248;R46TX;RBL12-6;14K35
PR324;S46;J11;14K38
PR326;SR46E;XEJ8;14K40
PR444;47L;H12;14K44
The numbers in the first column (PR...) represent a CCC number. The 2nd, 3rd, and 4th columns represent part numbers for three different companies (column 2 is one company, column 3 another, etc). The user will tell me the company, and the part number under the company by selecting a radio button and then typing in the part (ie company 3, and "RJ6") and then I'm supposed to give the customer the CCC number ("PR223").
I'm not asking you guys to do my homework for me but I have a hard time grasping arrays. Could you point me in the right as to how to go about doing this?
The other answers are correct. You read in each line from the text file and insert the values into a structure defined by your data. For each new line you need to use an instance of the structure, so you do that be making an array of structures which just a group of structures.
If you are struggling understanding arrays, you really need to work on that. Think of arrays like group of boxes. Let's start with 3 boxes in row. Each is the same size and can only hold what fits inside them. You can assigned type of item (data) to the boxes but all the boxes can only hold that type of item. The boxes are numbered from 0 to 2. In some languages, you can name the boxes (an associative array) but not in C# AFAIK. So that gives you a one dimensional array.
For two dimensonal array think of a Chess board. Do you know how to record a chess match? Chess notation uses A to H for the horizontal position and 1 to 8 for the vertical position. So the white queen starts in position D1. Now if I was programming a chess game I would use and 8x8 array just like the chess board. Of course the numbering on my array will be 0-7 across the top and 0-7 up the side. The Position of the white queen in my array would then be [3][0]. So placing data in a two dimensional array is like placing piece on a chess board. The game battleship is another example of a 2 dimensional array.
Adding dimensions to your array is like adding a new coordinate to your graph. You start with X, then you add Y and they add Z. So on and so forth.
You can use the ReadLine method of a StreamReader to read your file one line at a time. Then you can use the String.Split(char[]) method to get a string array of the values in each line. If you already know how to make structures you should be set.
If you look at each line of data there's a common delimiter between the 'columns', so you want to get each line split into four separate entities. As you work line by
line and get those four discrete elements should can assign each to the appropriate part of the structure. You'll of course have one 'structure' for each line, so an array of structure here at the end.
You may feel weird about what I'm asking, but it's true, convert 1 two-dimensional array into 2 one-dimensional arrays.
That was what my teacher asked, and he said he would give a perfect point for whoever answers this (I think, tricky) question. I would happy to convert any 2 or n-dimensional array into one one-dimensional array. But he said 2, so I think there must be something to do with the second array. And, he didn't tell what type of array (int, String or object), so I assume it must be done with any kind of 2-dimensional array.
This is what I will answer him if no one here figure out what he wants: Convert into a 1-dimensional array, and leave the second null (or let it have no element). But I don't think it's a good answer for such a tricky question.
EDIT: Here is my teacher question, word-by-word (he just ask at the end of the session in voice, not in the textbook, as a bonus question(with... a nice bonus reward)): Given a 2-dimensional array, convert it into two 1-dimensional arrays.
I don't know if [][] in Java and C# considered 2-dimensional array, but C# does have [, ], which is 2-dimensional array. We are studying computer algorithm, with no target IDE or language.
EDIT2: I emailed him, and he refused to give additional information (he said it was unfair for others if I have more information than them), and he didn't give any comments about jagged array idea. The only useful thing in his reply: Let [][] be considered 2-dimensional array.
I'll bite. It's possible to flatten the entire two-dimensional array into the first of the two one-dimensional arrays by simply reading and writing consistently. I.e. store row 1 then row 2, etc. sequentially in that first array. Whenever you move to the next row, store the index of that next cell (of the first one dimensional array) in the second one-dimensional array, which would essentially become a row index table.
As Jon Skeet said above, this isn't a very well-specified question; perhaps with clarified information, we could better help you.
If I understand your question properly
it's easy m8...
it's only an algorithm question.. not a programming language specific...
you can do it like this:
one array holds the values
second array holds the keys
try to find a workaround in the second array to know what keys you've got..
For example:
array_1: v0 v1 v2 null v3 v4 v5 null v6 v7 v8 null
array_2: 0 1 2 newR 0 1 2 newR 0 1 2 newR
You can represent it in one array as well... but you need a specific algorithm to figure out when you are located on Y of the matrix.
The problem is that you won't access the data instantly from memory.. this is why there are bi-dimensional arrays
Another way:
keep in array 1 the values
keep in the second array the keys as string like in the following example:
array1: value1 value2 value3 value4 value5
array2: 0,0 0,1 1,0 1,1 2,0
there are a lot of algorithms but I don't think you will find better than bi-dimensional arrays...
When you look after them you will have less performance.. ofc.. unless you keep them in hashtables.. hashing 0,0 and added as key in a hashtable and add the specified value to that key. then you will look for key "0,0"...
Flatten the 2-d array in either row-major or column-major order, storing it in one of the 1-d arrays. Store the shape {n, m} of the array in the other 1-d integer array. Given the indices for an element in the 2-d array of values, you can use the shape to calculate the index in the 1-d array of values.
The two representations are isomorphic, and both allow looking up the values in constant time. It's also similar to how a 2-d array is represented in memory.
I guess you want to transform you 2-demension array (typed RelevantType[,]) into 2 arrays (typed SomeTypeA[] and SomeTypeB[]) without losing any information?
It's not very difficult:
Have the first array be of type RelevantType[], the second one of type int[], copy the content of your 2-dimensionnal array into the first one and its first indices into the second one, and you're done.