Get count of elements of two dimensional array by given first index - c#

I have array defined like this :
List<DocumentFields>[,] dummyArray = new List<DocumentFields>[8,8];
It takes records from the database. I need to prepare this array for showing in a asp.net mvc3 view so i iterate it the standard way :
for (int i = 0; i < ViewBag.Header.GetLength(0); i++)
{
for (int j = 0; j < ViewBag.Header.GetLength(1); j++)
{
But I realized that in fact for the second iteration I don't need the Length but the count of the elements that are actually there. So instead of ViewBag.Header.GetLength(1) I need this:
ViewBag.Header.Get_Count_Of_The_Elements_For_The_Second_Index
I couldn't find a property that do this right away. Maybe some way of using Length or something... Dunno...

Your dummyArray is a two-dimensional array of lists. To get list size you can use dummyArray[i,j].Count in your inner loop.

Related

Table index out of bounds allthough the table is bigger than the index

I'm working on a simple matching game and I want to output a leaderboard, but i get an exception array index out of bounds. In the debug though it says that the table's size is 3 and that both i and j are 0 and 1, which is weird to me becouse that is not out of bounds since the array size is 3. This is my code for sorting the array of objects from biggest to smallest by the highscore, thanks for the help.
for(int i = 0; i<tabUporabnikov.Length-1; i++)
{
for(int j = (i+1); j<tabUporabnikov.Length; j++)
{
if (tabUporabnikov[i].highscore < tabUporabnikov[j].highscore)
{
Uporabnik[] zacasna = new Uporabnik[1];
zacasna[1] = tabUporabnikov[j];
tabUporabnikov[j] = tabUporabnikov[i];
tabUporabnikov[i] = zacasna[1];
}
}
}
it should be zacasna[0]. Aray can have only one value in this case and array starts with index 0. so it should be zacasna[0]. better you declare a variable instead of array because you need to store only value. declare it as Uporabnik zacasna

How can I assign different iteration within for loop in C#?

I have list called “images” which is contained of the series of bitmap images.
My question is how can I loop through every single element of my “images” list and do this operation for different elements of my list “images”?
You want to keep collections for each element of images, right?
You'll need to maintain a list of lists, or something similar:
List<List<int>> stuffOfImages = new List<List<int>>();
for (int x = 0; x < images[i].Width; x++) {
var stuffOfImage = new List<int>();
stuffOfImages.Add(stuffOfImage);
for (int y = 0; y < images[i].Height; y++) {
}
}
Note that you lose all reference to the associated image - it might be worthy creating a type so you can keep a reference to the image and the integer collection associated with it in one place and related.
I'm a little unclear as to what you mean, so please elaborate if necessary.

How can I make a 2D array from this one-line, comma delimited file?

I am trying to figure out how to turn the following, one-line CSV file into a 30x30 2D array.
http://pastebin.com/8NP7s7N0
I've tried looking it up myself, but I just can't seem to wrap my brain around the concept of multidimensional arrays, and I don't know how to turn a one-line file like this into an array of a specified size.
I want to be able to make an array that would look like this when printed:
0,0 = 2
0,1 = 2
All the way to 30,30.
Most of the numbers in the CSV are indeed 2's, but some are 1s. The difference is very important though. I am trying to make collision detection for a game, and this CSV file is the map. All I need left is how to create this array - leave the rest to me. :)
Thank you very much to all, have a nice day.
This should be a complete example using a 5 x 5 grid. I've tried it and seems to work as expected:
namespace ConsoleApplication1
{
using System;
class Program
{
const int MapRows = 5;
const int MapColumns = 5;
static void Main(string[] args)
{
// Create map and the raw data (from file)
var map = new int[MapRows, MapColumns];
string rawMapData = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25";
string[] splitData = rawMapData.Split(',');
int index = 0;
// Loop through data
for (int row = 0; row < MapRows; row++)
{
for (int column = 0; column < MapColumns; column++)
{
// Store in map and show some debug
map[row, column] = int.Parse(splitData[index++]);
Console.WriteLine(string.Format("{0},{1} = {2}", row, column, map[row, column]));
}
}
// Wait for user to read
Console.ReadKey();
}
}
}
Assuming your file is 900 elements first you need to read it in..
something along the lines of
line = myStreamReader.readLine().Split(',').. then in John U's example, value would be the next index in this array called line
I'll let you work out whats missing from my example :P
well, first you need to get the numbers...
var numbers = Read_File_As_String().Split(new char[',']).Select(n => int.Parse(n)).ToList();
then, you need to build your array
const int ROWS = 30;
const int COLS = 30;
var result = new int[ROWS, COLS];
for (int row = 0; row < ROWS; row++)
for (int col = 0; col < COLS; col++)
result[row, col] = numbers[(row * COLS) + col];
for(row=0;row<30;row++)
{
for(col=0;col<30;col++)
{
array[row][col] = value;
}
}
Value would need to be moved along to point to the next thing each time, but I'm sure you can figure that out.
Edited to add: If it's a map it might be easier to store it as an array in the first place.
Since you asked about the concept of multi-dimensional arrays, here are some useful ways of thinking about arrays. Please note these are analogies, meant to help you visualize them.
Think of a 1D array as a list of items (not in the programming sense of list!).
Think of a 2D array as a table (again, not in the programming sense!). In a table (like a spreadsheet) you have rows and columns, and each dimension in your array accesses one of these.
For higher dimensional arrays, it may help to think geometrically. For instance, you can think of 3D arrays as 3-dimensional points in space, and 4D arrays as 4-dimensional points in space-time.
So if you have a single CSV file, start off by conceptualizing how this would be re-structured as a table. Once you have that, you have a pretty straight-forward mapping to the array.

Unable to access 2d String Array row wise in C#

I am taking the data into a 2d string in C#.
Defined as ::
string[,] strValueBasic = new string[GridView1.Rows.Count,49];
Now i want the data to be taken into DataRows and add these rows into the DataTable.
for (int i = 0; i < GridView1.Rows.Count; i++)
{
DataRow drRow = dtNew.NewRow();
drRow.ItemArray = strValueBasic[i];
dtNew.Rows.Add(drRow);
}
But i am not able to access this 2d String array in the step
drRow.ItemArray = strValueBasic[i];
It is giving error as Wrong number of indices inside [], expected 2.
I know that it requires 2 indices, but than i want the whole row of that string array to be filled inside that DataRow.
Also the structure of DataRow and String Array is same.
If you want to assign whole row your strValueBasic should be defined as string[][]. Not the 2D array but array of arrays. That changes initialization too, each array can have different size so you have to create each separately. http://msdn.microsoft.com/en-us/library/2s05feca.aspx
Thats because strValueBasic is a 2D array and you're trying to access it as it's a 1D array.
for (int i = 0; i < GridView1.Rows.Count; i++)
{
DataRow drRow = dtNew.NewRow();
drRow.ItemArray = strValueBasic[i, iSomeOtherValue];
dtNew.Rows.Add(drRow);
}
Notice iSomeOtherValue, you're missing something!

C# Lists: initialize with size, why then can't use [] access until after .Add()?

This works fine with an array:
int[] a = new int[10];
for (int i = 0; i < 10; i++)
{
a[i] = i;
}
But this throws an ArgumentOutOfRangeException with a list:
List<int> a = new List<int>(10);
for (int i = 0; i < 10; i++)
{
a[i] = i;
}
Why is that? I thought that lists used arrays internally.
You are initializing the capacity, not the size. The count will still be zero. Initializing the capacity allows an optimization to size the internal data structure (an array) when you know the maximum size when creating the list. This keeps the internal array to a known size and prevents internal array re-sizing as you add the known number of elements.
new List<int>(10) creates a new list with the initial capacity of 10 items. The list is still empty.
You need to add items to it before you can access them by index.
When you don't specify a capacity, your collection will be reallocated several times if it even grows to have 100 elements. This makes populating your list twice as slow.
refer to this test page

Categories