Home > Net >  How to use linear index, take multidimensional an element in the list?
How to use linear index, take multidimensional an element in the list?

Time:04-28

Multidimensional list:
1, one dimension: five elements
2, 2 d: eight element
3, 3 d: three elements

If through the linear index, how to collect the 11 elements? How to collect the 43rd elements? And how to pick up the 82th element?

Assuming that dimension sorting is growing up, namely: 1 d and 2 d, 3 d,

CodePudding user response:

I don't want to, you can also issued a problem, just want to back, the results of q deleted,
See demo:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

The namespace ArryDemo
{
Class Program
{
[STAThread]
The static void Main (string [] args)
{
//two dimensional array
Int [and] a=new int [3, 4]//3 line 4 column array
{
,1,2,3 {0},
{4, 7},
,9,10,11 {8}
};

//array subscript access: the row index and column index
//the Console. Write (a) [1, 2];

//traversal
for(int i=0; i <3; I++)
{
for(int j=0; j <4. J++)
{
Console. WriteLine (" a/{0}, {1} ", I, j, a [I, j));
}
}


//three dimensional array
Int [and] arr3=new int [and]
{
{{1, 2, 3}},
{{4 and 6}},
7,8,9 {{}}
};

Foreach (int I in arr3)
{
Console. WriteLine (I);
}

The Console. ReadKey ();
}
}
}


More than three dimensions, basic is 3 layers of nested array, or layer 3 braces, in short, do not add a comma, a layer, with two commas, is three layers,
The value of how to take? That is three layers for or foreach nested, directly through the subscript access, line not line, also can, in fact is the subscript (x, y, z) (index),

CodePudding user response:

This is not a multidimensional in.net, but staggered array
Traditionally useful memory copy into a weft, but with.net
Tend to use linq selectmany mapped to a weft (because many objects in.net, so direct copy your bad memory size calculation)

If at ease, you list in the list, and so is another problem, traditional hierarchical data query, generally is recursive, this you can baidu "sp1234 heavyweight query component" can be seen, although sp1234 that year to check the control of the control, but the general point, he is really a hierarchical data query

CodePudding user response:

This is a general solution, if you must and blogs to learn what the optimal, special solution, lazy

I can only write the

If the index in the list [0]. Take list0 directly in length. The index
If in match between 0, 1, 1. The length of the index 0
Repeat doing

Bad knock code, phone, but only to describe the process of the

CodePudding user response:

In favor of the thinking of "morliz xuan son",

CodePudding user response:

If you still want to blog garden, not nouns don't write code, also line

Give you a big nouns, consistency hash,
Draw a circle, circle superscript 3 points, points 1 to 5, 2 to 5 + 8, 3 to 5 + 8 + 3

Clockwise from the most close to the point, where he is to save the data,

So you can take clockwise the location of the saved, and then use this point is worth to the relative position of

Such as the index of 6, clockwise closest to the 13 (point 2)
The relative position of 13-6=7, that is, from which a

Mobile phone recovery, so perhaps, too tired to think so carefully,

This is a blog garden consistency hash of trying the tall, as I reply above also do not have what distinction, in essence is just the mathematics of space

CodePudding user response:

Delivery, such as idle, open the computer to give you STH over and over again, in fact you see how the results, linq lazy space, in time, be sure to tall on the consistency of the hash, though without loss of memory requires pretreatment

 int [] [] source={//this is crisscross array, not a multidimensional array 
New int [] {0, 1, 2, 3, 4},
New int [] {5,6,7,8,9,10,11, 12},
13,14,15 new int [] {}
};

//linq solution directly, I'll find the index is equal to 6
Var b=source. SelectMany (p=& gt; P). ToArray () [6-1);//array starting from 0 and 1

//consistency hash
Int [] w=new int [source Length];
int count=0;
//put the three points in
for (int i=0; i {
The count +=source [I]. Length;
W [I]=count;
}
//check first is greater than 6
for (int i=0; i {
[I] if (w & gt; 6)
{
Var arr=source [I];
Console. WriteLine (arr [arr. Length - [I] - 6 (w) - 1));
Break;

}
}
  •  Tags:  
  • C#
  • Related