처음에는 길이가 같고, 배열에 null인 요소가 포함된 두 배열을 합쳐서 null인 요소가 없어지게 합치는거 인줄 알았는데


댓글보고 아닌거 같더라

배열에 값은 다 들어가 있는데, 그 객체의 각 필드나 속성의 값이null 인 것들을 합치고 싶다는 거지?


배열 요소가 null인거도 합쳐야 한다해도 해결방법은 비슷함


처리 결과




메인 로직

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayTest
{
    public class Program
    {
        public static void Main(string[] args)
        {
            int arrayLength = 10;
 
            var rand = new Random();
            var array1 = CreateRandomElementArray(arrayLength, true, rand);
            var array2 = CreateRandomElementArray(arrayLength, false, rand);
 
            PrintArray(array1, "Array1");
            PrintArray(array2, "Array2");
 
            var mergedArray = MergeArray(array1, array2);
            PrintArray(mergedArray, "MergedArray");
        }
 
        public static EntityStatus[] MergeArray(EntityStatus[] array1, EntityStatus[] array2)
        {
            Trace.Assert(array1.Length == array2.Length, "Array's length not same");
            var array = new EntityStatus[array1.Length];
 
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = new EntityStatus(array1[i], array2[i]);
            }
 
            return array;
        }
 
        public static void PrintArray(EntityStatus[] array, string name)
        {
            Console.WriteLine("Array " + name + "'s elements");
 
            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine(i.ToString() + " : " + string.Concat(array[i]));
            }
 
            Console.WriteLine();
        }
 
        public static EntityStatus[] CreateRandomElementArray(int length, bool flag, Random rand)
        {
            var array = new EntityStatus[length];
 
            for (int i = 0; i < array.Length; i++)
            {
                var element = new EntityStatus();
                element.Init(flag, rand);
 
                array[i] = element;
            }
 
            return array;
        }
 
    }
 
}
 
cs


EntityStatus.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayTest
{
    public class EntityStatus
    {
        private object _Level = null;
        public object Level { get { return this._Level; } set { this._Level = value; } }
 
        private object _Name = null;
        public object Name { get { return this._Name; } set { this._Name = value; } }
 
        private object _Job = null;
        public object Job { get { return this._Job; } set { this._Job = value; } }
 
        private object _HP = null;
        public object HP { get { return this._HP; } set { this._HP = value; } }
 
        private object _MP = null;
        public object MP { get { return this._MP; } set { this._MP = value; } }
 
        private object _EXP = null;
        public object EXP { get { return this._EXP; } set { this._EXP = value; } }
 
        public EntityStatus()
        {
 
        }
 
        public EntityStatus(EntityStatus other1, EntityStatus other2)
        {
            if (other1 == null)
            {
                throw new ArgumentNullException("other1");
            }
 
            if (other2 == null)
            {
                throw new ArgumentNullException("other2");
            }
 
            this.Level = other1.Level ?? other2.Level;
            this.Name = other1.Name ?? other2.Name;
            this.Job = other1.Job ?? other2.Job;
 
            this.HP = other1.HP ?? other2.HP;
            this.MP = other1.MP ?? other2.MP;
            this.EXP = other1.EXP ?? other2.EXP;
        }
 
        public void Init(bool flag, Random rand)
        {
            if (flag == true)
            {
                this.Level = rand.Next(0100).ToString();
                this.Name = Guid.NewGuid().ToString();
                this.Job = Guid.NewGuid().ToString();
            }
            else
            {
                this.HP = rand.Next(01000).ToString();
                this.MP = rand.Next(01000).ToString();
                this.EXP = rand.Next(0100).ToString();
            }
 
        }
 
        public override string ToString()
        {
            var lines = new List<string>();
            lines.Add(this.ToString("Level"this.Level));
            lines.Add(this.ToString("Name"this.Name));
            lines.Add(this.ToString("Job"this.Job));
 
            lines.Add(this.ToString("HP"this.HP));
            lines.Add(this.ToString("MP"this.MP));
            lines.Add(this.ToString("EXP"this.EXP));
 
            return string.Join(", ", lines);
        }
 
        private string ToString<E>(string name, E value)
        {
            return new StringBuilder().Append(name).Append("=").Append(value).ToString();
        }
 
    }
 
}
cs


그 필드들이 참조형이라면 ?? 연산자(NULL 병합 연산자)를 써보자

A ?? B 꼴로 사용하는데
A랑 B중에서 null이 아닌 값을 반환함
둘 다 null이면 null 반환
둘 다 null이 아니면 A 반환