2D字符串数组帮助 [英] 2d array of strings help
问题描述
大家好..
我正在使用小型实用程序从sql select语句中从数据库获取名称..
我有4个字段可以从db获取行,但是用户可以填写1或2或3或4个字段..
因此,如果填写了4个字段,则该语句应以
结尾
hello guys..
im working on small utility to get names from database in sql select statements..
i have 4 fields to get the rows from db, but users may fill 1 or 2 or 3 or 4 fields..
so if the 4 fields are filled, the statement should ends with
WHERE a = field1 and b = field2 and c = field3 and d = field4
如果它们是3,则where语句跟随已填充的字段,例如a和c和d已填充,则该语句应为
if they were 3 then the where statement follow the filled fields e.g a and c and d are filled then the statement should be
WHERE a = field1 and c = field3 and d = field4
以上所有工作都已经完成..
多个值之间的字段可能有所不同,我可以使用"IN"语句,但是查询时间太长..
我曾想过将"IN"语句拆分为"="语句,然后将结果合并为一个结果..
我的问题是怎么做!
可以说我有以下内容..
包含
的二维数组
a1,a2
b1
c1,c2
d1,d2,d3,d4
结果应该是:
a1b1c1d1
a1b1c1d2
a1b1c1d3
a1b1c1d4
a1b1c2d1
a1b1c2d2
a1b1c2d3
a1b1c2d4
a2b1c1d1
a2b1c1d2
a2b1c1d3
a2b1c1d4
a2b1c2d1
a2b1c2d2
a2b1c2d3
a2b1c2d4
我认为这很容易,但是我花了3天的时间工作,我再也想不起来了..
all the above i''ve work on and done..
fields may vary between multiple values, i can use the "IN" statement, but the query takes too long time..
i''ve thought to split the "IN" statement into "=" statements and then join the results in one result..
my problem is how to do that!!
lets say that i have the following..
2d array that contains
a1,a2
b1
c1,c2
d1,d2,d3,d4
the result should be:
a1b1c1d1
a1b1c1d2
a1b1c1d3
a1b1c1d4
a1b1c2d1
a1b1c2d2
a1b1c2d3
a1b1c2d4
a2b1c1d1
a2b1c1d2
a2b1c1d3
a2b1c1d4
a2b1c2d1
a2b1c2d2
a2b1c2d3
a2b1c2d4
i think it is very easy, but i''ve spent 3 days working and i can''t think anymore..
推荐答案
这完全取决于您的工作想要处理数组已创建的时间,频率等.问题是:本机" .NET数组并非旨在在创建元素后动态添加和删除元素.您可以调整数组的大小,但是最好避免这样做,因为它非常昂贵且难以维护.当您需要一些动态行为时,您需要使用集合.
现在,让我们介绍2D阵列.让我们回顾一下您的所有选项,从最简单,最不灵活的到最灵活的.请注意,所有选项都非常易于使用和实现.
- 2D数组声明为:
It all depends on what do you want to do with the arrays when they are already created, how often, etc. The problem is: the "native" .NET arrays are not designed to add and remove elements dynamically after they are created. You can resize the array, but even this is best avoided as it is pretty expensive and hard to maintain. When you need some dynamic behavior, you need to use collections.
Now, let''s approach 2D arrays. Let''s review all your options from the simplest and least flexible, to the more flexible. Mind you, all the options are quite easy to use and implement.
- 2D array declared as:
string[,] array = new string[numberOfRows, numberOfColumns];
由于您的示例在不同的行中显示了不同数量的元素,因此您可以将未使用"元素指定为null
.
这可能不是最适当的选择,但肯定非常简单.
As your example shows different number of elements in different rows, you can designate "unused" element as null
.
This is probably not the most adequate choice, but certainly very simple.
string[][] array = new string[numberOfRows][];
使用锯齿状数组,您可以将不同长度的数组用作外部数组的元素.请注意,由于这是一个数组(如果是数组),因此您可以一次创建外部数组,但必须使用new
运算符在循环中分别创建每个内部数组.例如:
With a jagged array, you can have arrays of different lengths uses as the elements of the outer array. Note that as this is an array if arrays, you can create the outer array once but have to create each inner array separately, in a loop, using new
operator. For example:
array[13] = new string[2] {"1", "2"}; //here, 2 is the number of some row
您不能插入或删除元素,但是在任何时候,您始终可以将内部数组的新实例替换为内部数组的新实例,并以不同的长度进行初始化.
另请参见 http://en.wikipedia.org/wiki/Jagged_array [
You cannot insert or remove elements, but at any time, you can always replace the element of the outer array with a new instance of an inner array initialized with a different length.
See also http://en.wikipedia.org/wiki/Jagged_array[^].
System.Collections.Generic.List<System.Collections.Generic.List<string>> list
= new System.Collections.Generic.List<System.Collections.Generic.List<string>>();
您需要使用new
运算符分别创建每个内部列表(例如,按照您的示例代表一行字符串);您可以在任何内部列表中添加/删除字符串元素,并随时将内部列表作为外部列表的元素添加/删除.
You need to create every internal list (let''s say, representing a row of string, as per your example) separately with new
operator; you can add/remove a string element in any inner list and add/remove an inner list as an element of outer list at any time.
这篇关于2D字符串数组帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!