繁体   English   中英

将特定索引中的值放入Char数组,然后将Char数组的特定元素复制到C#中的另一个Char数组

[英]Put value in specific index to Char array then copy specific element of an Char array To another Char array in C#

我正在尝试编写C#代码来创建两个名称为ex“ ptrArr ”,“ ptrArr2 ”的Char Array。

并将*放入数组“ ptrArr ”中的每个索引(2 ^ i)-1
例如:索引0、1、3、7 ...

然后将“ ptrArr2 ”的元素复制到“ ptrArr ”, 而不复制具有“ *”值的数组“ ptrArr”的索引。

正如我在以下图片中的链接中所解释的

https://app.box.com/s/e6qwsnw7iwg86c90vohm

在此处输入图片说明

请我帮忙

这是我的尝试

int i = 0, j = 0, n = 0;

while (n < ptrArr.Length)
{
    if (ptrArr[i] != '*')
    {
        ptrArr2[j] = ptrArr[i];
        i++;
        j++;
    }
    else
        i++;
}
string s = new string(ptrArr);
textBox5.Text = s;

这可以解决您的问题,并且您可以根据需要获得ptrArr2。 请评论

 static void Main(string[] args)
        {
            char[] ptrArr = new[] { '0','1','0','1','0','1','0','1' };
            char[] ptrArr2=  new char[0];
            List<char> ptrArr2temp = new List<char>();

            for (int i = 0; i < ptrArr.Length; i++)
            {   
                ptrArr2temp.Add(ptrArr[i]);
            }

            for (int i = 0; i < ptrArr.Length; i++)
            {
                int internalIndex = ((int)(Math.Pow(2, i))) - 1;
                if (ptrArr2temp.Count > internalIndex)
                {
                    ptrArr2temp.Insert(internalIndex, '*');
                }
            }
            ptrArr2 = ptrArr2temp.ToArray();          
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM