简体   繁体   English

以相反的顺序将数组值传递给另一个数组

[英]Passing array value to another array in reverse order

I want to pass int a[] values into int b[].我想将 int a[] 值传递给 int b[]。 but I'm facing confusion.但我正面临困惑。 Can anyone correct me out.任何人都可以纠正我。 The output of b is 0,0,0,45,4,2,1. b 的输出是 0,0,0,45,4,2,1。 it supposed to be like 7,6,5,4,45,2,1.它应该像 7,6,5,4,45,2,1。

static void Main()
{
    int[] a = new int[] { 1, 2, 45, 4, 5, 6, 7 };
    int[] b = new int[7];
    int temp = 0;

    for (int i = 0; i <= a.Length - 1; i++)
    {
        b[(b.Length - i) - 1] = a[i];

        Console.WriteLine(b[i]);
    }     
}   

The problem here is that you're inserting items starting from the last index of b , but you're outputting them starting from the first index.这里的问题是您从b的最后一个索引开始插入项目,但是您从第一个索引开始输出它们。 The code to copy the items is correct, but you need to adjust your line that outputs the result to the console to show the items in b using the same index that you just used to insert the item.复制项目的代码是正确的,但您需要调整将结果输出到控制台的行,以使用与插入项目相同的索引显示b中的项目。

Note there are a couple of other improvments you can make, such as using array initializer syntax for a , using a.Length to instantiate b instead of a hard-coded number, removing the unused temp variable, using i < a.Length for the for condition (instead of i <= Length - 1 , which does a subtraction operation on each iteration), and storing the b index in a variable instead of calculating it twice:请注意,您还可以进行一些其他改进,例如对a使用数组初始化语法,使用a.Length实例化b而不是硬编码数字,删除未使用的temp变量,使用i < a.Length作为for条件(而不是i <= Length - 1 ,它在每次迭代时执行减法运算),并将b索引存储在变量中而不是计算两次:

static void Main()
{
    int[] a = new int[] {1, 2, 45, 4, 5, 6, 7};
    int[] b = new int[a.Length];

    for (int i = 0; i < a.Length; i++)
    {
        int bIndex = b.Length - i - 1;
        b[bIndex] = a[i];
        Console.WriteLine(b[bIndex]);
    }

    Console.ReadLine();
}

However, this will still output the items in the order in which you insert them , which will be the same order as they appear in a .但是,这仍然会按照您插入它们的顺序输出项目,这与它们a . If you want to show that the items in b are the reverse of a , the easiest way is to do it after you've populated b .如果你想证明 b 中的项目是b a反面,最简单的方法是在填充b之后再做。 Note we can make use of the string.Join method here to join each item with a comma:请注意,我们可以在这里使用string.Join方法用逗号连接每个项目:

static void Main()
{
    int[] a = new int[] {1, 2, 45, 4, 5, 6, 7};
    int[] b = new int[a.Length];

    for (int i = 0; i < a.Length; i++)
    {
        b[b.Length - i - 1] = a[i];
    }

    Console.WriteLine($"'a' array: {string.Join(",", a)}");
    Console.WriteLine($"'b' array: {string.Join(",", b)}");

    Console.ReadLine();
}

Output输出

在此处输入图像描述

If you want to create a reverse array from an existing array, you can use Reverse extension method:如果要从现有数组创建反向数组,可以使用Reverse扩展方法:

//using System.Linq;
int[] a = new int[] { 1, 2, 45, 4, 5, 6, 7 };
int[] b = a.Reverse().ToArray();

You can learn more about Extension Methods .您可以了解有关扩展方法的更多信息。

You can have this syntax你可以有这个语法

int[] a = {1, 2, 45, 4, 5, 6, 7};
int[] b = new int[7];

for (int i = 0, j = b.Length - 1; i < a.Length; i++, j--)
{
    b[i] = a[j];
    Console.WriteLine(b[i]);
}

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

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