처음에는 길이가 같고, 배열에 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(0, 100).ToString(); this.Name = Guid.NewGuid().ToString(); this.Job = Guid.NewGuid().ToString(); } else { this.HP = rand.Next(0, 1000).ToString(); this.MP = rand.Next(0, 1000).ToString(); this.EXP = rand.Next(0, 100).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 반환
오 맞음. 감사감사 매우 감사. 원하는게 이거였는데 생각보다 로직이 엄청 복잡한 일이었구나;;
위와 같은 형식에서 캐릭터가 각각 NAME, JOB, LEVEL 필드값만 갖고 리스트에 올라가 있는데 NAME값과 LEVEL값을 확인해서 맞는 스탯값을 null 필드값으로 끌어오려고 하는게 목적인데 저 메인 로직을 활용하면 머징배열을 하나 더 만들어야하는건가?
1:1로 필드값을 밀어주는게 좋은지 머징배열을 하나 더 만드는게 좋은지 모르겠음...
너가 그 클래스 역할을 어떻게 부여하는지에 따라 다른데 내가봐서는 밀어주는게 나을거같음
저거 복잡하게 처리하는거는 저 값 부여하고 배열생성하고 그런것들 다 하다보니까 그렇게 보이는거고
핵심은 public EntityStatus(EntityStatus other1, EntityStatus other2) 생성자임
아. NAME, LEVEL을 key값 삼아서 ?? 연산자로 밀어주면 되겠다. 이거 때문에 여기저기 글 올렸는데 아무도 답변 안해줘서 한참 고생했음 감사감사 ㅠㅠ.
잉갤 십고수 등장 ㄷㄷ