构建数组A& B,在数组C中添加值 [英] Build arrays A & B, add values in array C

查看:88
本文介绍了构建数组A& B,在数组C中添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我添加两个数组

构建数组A和B,
像数组A = {1,2,3,4}
并且数组B = {5,6,3,2}
将这两个数组添加到数组C中
并按降序对数组显示元素进行排序.
请帮我怎么做.

问候
Basheer

Hi please help me to add the two array

Build arrays A & B,
like Array A={1,2,3,4}
and Array B={5,6,3,2}
add these two array in Array C
and sort the array display elements in descending order.
Please Help me to how can i do that.

Regards
Basheer

推荐答案

有两种方法可以做到这一点:一种在结果中包含重复项,另一种则不.
简单方法:
There are two ways to do this: One way includes the duplicates in the result, the other doesn''t.
Easy way:
int[] A = new int[] { 1, 2, 3, 4 };
int[] B = new int[] { 5, 6, 3, 2 };
List<int> combined = new List<int>();
combined.AddRange(A);
combined.AddRange(B);
combined.Sort();
int[] C = combined.ToArray();</int></int>


较难的方法:


Harder way:

int[] A = new int[] { 1, 2, 3, 4 };
int[] B = new int[] { 5, 6, 3, 2 };
List<int> combined = new List<int>();
combined.AddRange(A);
foreach (int i in B)
    {
    if (!combined.Contains(i))
        {
        combined.Add(i);
        }
    }
combined.Sort();
int[] C = combined.ToArray();</int></int>


这篇关于构建数组A&amp; B,在数组C中添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆