Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何访问循环外部的字符串数组 for(int z=0;z_C#_Arrays_Loops - Fatal编程技术网

C# 如何访问循环外部的字符串数组 for(int z=0;z

C# 如何访问循环外部的字符串数组 for(int z=0;z,c#,arrays,loops,C#,Arrays,Loops,我必须访问循环外部的两个字符串数组。有更好的解决方案吗?我不能使用ArrayList或将它们声明为公共,因此如何访问它们?asd和def的范围都限于for循环的主体。如果必须访问它们,则需要将它们声明为外部e循环。把它们放出来有问题吗 查看MSDN上的。要访问循环外部的内容,只需在循环外部声明它,然后在循环处理完成后使用它: for (int z = 0; z < alParmValues.Count; z++) { //string[] def; string[] asd =

我必须访问循环外部的两个字符串数组。有更好的解决方案吗?我不能使用ArrayList或将它们声明为公共,因此如何访问它们?

asd和
def
的范围都限于
for
循环的主体。如果必须访问它们,则需要将它们声明为外部e循环。把它们放出来有问题吗


查看MSDN上的。要访问循环外部的内容,只需在循环外部声明它,然后在循环处理完成后使用它:

for (int z = 0; z < alParmValues.Count; z++)
{
   //string[] def;
   string[] asd = alParmValues[z].ToString().Split(',');//this is of type string.collections and u cant cast it to a arraylist or array 
   //if (HUTT.clsParameterValues.bCustomObj == false)

   string[] def = alMethSign[z].ToString().Substring(alMethSign[z].ToString().IndexOf('(') + 1, alMethSign[z].ToString().IndexOf(')') - (alMethSign[z].ToString().IndexOf('(') + 1)).Split(',');
}
string[]arr=。。。
对于(int z=0;z
但是,你的代码似乎有一些错误。我建议你多思考一下循环体和你正在做的事情。考虑这条线,例如:

string[] arr = ...

for (int z = 0; z < alParmValues.Count; z++)
{
  // work with arr...
}

var item = arr[3]; // Accessed outside of loop.
for(int z=0;z

此赋值毫无意义;每次通过循环时,您只需覆盖
asd
的上一个值,以后再也不会在循环中使用它。

asd和'def'都是字符串数组,其作用域仅限于for循环。您无法在循环外部访问它们。如果要这样做,请尝试在fo外部声明它们r循环。

首先,如果要访问循环内提取/计算的数据,必须在循环外为结果声明一个容器,然后在循环内填充其值

其次,不要考虑强制转换从
split
方法返回的数组,而是考虑处理它们的内容

假设您希望将原始
alParmValues
数组中所有元素的组合结果放在一对结果中,我将使用类似于以下伪代码的代码。当然,您需要为
alParmValues
alMethSign
元素填写类型,添加分号等。(因为您的问题没有解释循环中正在处理的两个数组之间的内容和关系,所以我只是单独处理它们。)这不是完整的代码,只是一个让您开始的草图:

for (int z = 0; z < alParmValues.Count; z++)
{
  // ...
  string[] asd = alParmValues[z].ToString().Split(',');

  // There aren't any more references to asd after this point in the loop,
  //   so this assignment serves no purpose and only keeps its last assigned
  //   value.
}

是的,我不能把它们放出来,因为我不知道它们动态增加的大小。如果你有一个动态增加的数组,那么数组是错误的数据结构。请使用列表。如果你以后需要转换回数组,请使用列表的ToArray方法。你不能强制转换字符串。集合到数组列表为什么每个人都在向下投票这个问题?它与编程有关,他需要一些帮助。谢谢你的支持,实际上我的问题是因为我使用split,所以我必须将它分配给字符串[]但是我不知道如何声明动态tring arrayi否决了他,因为他不使用任何标点符号或大写字母,我认为这种行为会降低论坛的质量,但是如果他改变这个,我会很高兴地删除我的downvoteI。我想我已经做了必要的改变,我会在这里跟进。问题是当我在外部声明是因为我拆分了字符串,数组大小增加超过了z值,例如一个循环周期,可能会添加3个字符串,那么我如何解决这个问题用几句清楚的话解释你试图用这两个字符串数组做什么。我们可能会提出更好的方法。alMethSign[z]包含(int32,int32)我使用了分隔符“,”进行拆分,因此无法知道字符串[]的大小,因此我对如何动态声明字符串[]感到困惑听起来您越来越不应该使用字符串数组。请改用列表。为了使用列表数据类型,我们应该添加什么引用
ArrayList allValues = new ArrayList()
foreach (??? parameter in alParmValues) {
    foreach (String value in parameter.ToString().Split(',')) {
        allValues.add(value)
    }
}

ArrayList allMethSignValues = new ArrayList()
foreach (??? methSign in alMethSign) {
    String thisString = methSign.toString()
    int open = thisString.indexOf('(')
    int close = thisString.indexOf(')')
    String parenPart = thisString.substring(open + 1, close - open - 1)
    foreach (String value in parenPart.split(',')) {
        allMethSignValues.add(value)
    }
}