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
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.
I have a bunch of DataTables that need to be converted to object[,] arrays (not object[][] arrays). What's the most efficient way to do this in terms of performance?
I know I can do this by building my object[dt.Rows.Count, dt.Columns.Count] initially, then looping through rows and parsing each row into a place in the array, but I'm fairly sure there's other methods such as using Linq or System.Data-specific features such as dataRow.ToItemArray() which may be more efficient.
My DataTables are of variable sizes, and contain both Dates and Numbers which need to be formatted appropriately, in addition to strings.
For example, if one of my data tables contained
Id Name Date Value
1 Rachel 1/1/2013 00:00:00 100.0000
2 Joseph 3/31/2012 00:00:00 50.0000
3 Sarah 2/28/2013 00:00:00 75.5000
then I would want an object[,] array containing the exact same data (ideally with headers), but with formatted Dates and Values
arr[x,0] = row[x].Field<int>("Id");
arr[x,1] = row[x].Field<string>("Name");
arr[x,2] = row[x].Field<DateTime>("Date").ToString("M/d/yy");
arr[x,3] = row[x].Field<decimal>("Value").ToString("C2"); // Currency format
Basically we need:
Allocate memory for object[,]
We cannot do much here.. we need to ensure we allocate memory once and not re-allocate it again. So it's obvious we need create array at once, without using operations like List.Add(...) which internally re-allocate memory blocks.
Then, we need to copy objects from row items into multidimensional array. We could not use Buffer.BlockCopy here as we work with objects. Naturally we can not rely on any memcpy-like behavior, as CLR for each object needs either copy its reference, or do unbox->copy in heap->box for value types. So, the simplest way will be just for.. for.. style.
So, looks like most performance solution here is the intuitive one:
public static object[,] Convert(DataTable dt)
{
var rows = dt.Rows;
int rowCount = rows.Count;
int colCount = dt.Columns.Count;
var result = new object[rowCount, colCount];
for (int i = 0; i < rowCount; i++)
{
var row = rows[i];
for (int j = 0; j < colCount; j++)
{
result[i, j] = row[j];
}
}
return result;
}
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.
I would like to use a row and column removing method in my array. Therefore I wanted to convert my array to ArrayList to use the RemoveAt(int index) method, but in Windows 8 Apps using .NET 4.5 there is no ArrayList. Could you give me a suggestion how to cast my simple int[,] array to another type, which has a row and column removing method?
You could just use a list of lists:
List<List<int>> array = new List<List<int>>();
And initialize it as follows
for (int i = 0; i < orig_array.Length, i++)
{
array.Add(new List<int>(orig_array[i]));
}
Using this approach, array.RemoveAt(row) would remove an entire row, whereas array[row].RemoveAt(col) would remove an element.
EDIT: As phoog indicated, the above initialization would need to be modified for an array declared as int[,], as follows:
for (int row = 0; row < orig_array.GetLength(0), row++)
{
array.Add(new List<int>());
for (int col = 0; col < orig_array.GetLength(1); col++)
{
array[row].Add(orig_array[row, col]);
}
}
The advantage to using a jagged array (as opposed to a rectangular array) in this case is being able to access entire rows, without the need to explicitly loop through the values.
The ArrayList and all non-generic collections for been removed from WinRT. This link here will help you with replacing the the arraylist.