簡體   English   中英

如何通過組合每個列表中的一個元素將兩個不同對象的列表組合成一個列表?

[英]How to combine two list of different objects into one list by combining one element from each list?

嗨,我正在嘗試將兩個列表與不同的對象結合起來。

[{"SaveValues":[{"id":1,"allposition":{"x":-0.015270888805389405,
"y":9.267399787902832},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},
"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":1}],

"NoteValues":[{"movenumber":1,"notemsg":"Added one"}]},
{"SaveValues":[{"id":1,"allposition":{"x":-0.015270888805389405,
"y":9.267399787902832},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},
"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2},{"id":1,
"allposition":{"x":-0.02840942144393921,"y":6.721944808959961},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},
"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2}],
"NoteValues":[{"movenumber":2,"notemsg":"Added two"}]},
{"SaveValues":[{"id":1,"allposition":{"x":-0.015270888805389405,
"y":9.267399787902832},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},
"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":3},{"id":1,
"allposition":{"x":-0.02840942144393921,"y":6.721944808959961},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},
"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},
"movetype":3},{"id":1,"allposition":{"x":-0.10085266828536987,
"y":4.49822473526001},"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},
"linepos0":{"x":0.0,"y":0.0,"z":0.0},
"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":3}],"NoteValues":
[{"movenumber":3,"notemsg":"Added three"}]},{"SaveValues":
[{"id":1,"allposition":{"x":-0.015270888805389405,"y":9.267399787902832}
,"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},

"linepos0":{"x":0.0,"y":0.0,"z":0.0},
"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":4},{"id":1,
"allposition":{"x":-0.02840942144393921,"y":6.721944808959961},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},
"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":4},{"id":1,"allposition"
:{"x":-0.10085266828536987,"y":4.49822473526001},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},
"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},
"movetype":4},{"id":1,"allposition":{"x":0.17862117290496827,"y":1.5408382415771485},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},
"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":4}],
"NoteValues":[{"movenumber":4,"notemsg":"Added four"}]}]

Class 下面給出。

[System.Serializable]
public class PlayerHandler 
{
public int id; 
public Vector2 allposition;
public Quaternion allrotation;
public Vector2 allscale;

public Vector3 linepos0;
public Vector3 linepos1;
public int movetype;


public PlayerHandler(int ids,Vector2 allpos,Quaternion allrot,Vector2 allscal,Vector3 Line0,Vector3 Line1,int Moves)

{
    this.id = ids;
    this.allposition = allpos;
    this.allrotation = allrot;
    this.allscale = allscal;
    this.linepos0 = Line0;
    this.linepos1 = Line1;
    this.movetype = Moves;
}



}

[System.Serializable]
public class PlayerMovement
{
public int movenumber;
public string notemsg;

public PlayerMovement(int Movenum,string Note)
{
    this.movenumber = Movenum;
    this.notemsg = Note;

}

}

現在我有兩個列表及其對應的值。

List<PlayerHandler> SaveValuesDeserialize = new List<PlayerHandler>();
List<PlayerMovement> NoteValuesDeserialzeList = new List<PlayerMovement>();

我想將上述兩個列表組合成第三個列表,其中第 0 個元素來自 NoteValuesDeserialzeList[0],第 1 個元素來自 SaveValuesDeserialize[0],第 2 個元素來自 NoteValuesDeserialzeList[1],第 3 個元素來自 SaveValuesDeserialize[1],直到所有值相加。

各種疑問出現。 新增列表List<?>AllcombinedList =new List<?>();的 object 會是什么? .

加起來新列表的計數是多少(兩個列表的組合計數)?

因為 PlayerHandler 和 PlayerMovement 的唯一祖先是 object,所以只能創建一個對象列表:

List<object> AllcombinedList = new List<object>();

int count = Math.Min(SaveValuesDeserialize.Count, NoteValuesDeserialzeList.Count);
// Or raise an exception if SaveValuesDeserialize.Count != NoteValuesDeserialzeList.Count
// Or ask the user what to do with the rest if it can happen
// Else use count = SaveValuesDeserialize.Count for example if always same
for ( int index = 0; index < count; index++ )
{
  AllcombinedList.Add(NoteValuesDeserialzeList[index]);
  AllcombinedList.Add(SaveValuesDeserialize[index]);
}

也許您可以使用元組列表來實現您的目標:

var AllcombinedList = new List<Tuple<PlayerHandler, PlayerMovement>>();

int count = Math.Min(SaveValuesDeserialize.Count, NoteValuesDeserialzeList.Count);
// Or raise an exception if SaveValuesDeserialize.Count != NoteValuesDeserialzeList.Count
// Or ask the user what to do with the rest if it can happen
// Else use count = SaveValuesDeserialize.Count for example if always same
for ( int index = 0; index < count; index++ )
{
  var item = new Tuple<PlayerMovement, PlayerHandler>(NoteValuesDeserialize[index],
                                                      SaveValuesDeserialzeList[index]);
  AllcombinedList.Add(item);
}

或匿名元組:

var AllcombinedList = new List<(PlayerHandler, PlayerMovement)>();

int count = Math.Min(SaveValuesDeserialize.Count, NoteValuesDeserialzeList.Count);
// Or raise an exception if SaveValuesDeserialize.Count != NoteValuesDeserialzeList.Count
// Or ask the user what to do with the rest if it can happen
// Else use count = SaveValuesDeserialize.Count for example if always same
for ( int index = 0; index < count; index++ )
{
  AllcombinedList.Add((NoteValuesDeserialize[index], SaveValuesDeserialzeList[index]));
}

或者您可以創建一個 class 提供打字和命名:

var AllcombinedList = new List<PlayerItem>();

int count = Math.Min(SaveValuesDeserialize.Count, NoteValuesDeserialzeList.Count);
// Or raise an exception if SaveValuesDeserialize.Count != NoteValuesDeserialzeList.Count
// Or ask the user what to do with the rest if it can happen
// Else use count = SaveValuesDeserialize.Count for example if always same
for ( int index = 0; index < count; index++ )
  AllcombinedList.Add(new PlayerItem
  {
    Movement = NoteValuesDeserialzeList[index],
    Handler = SaveValuesDeserialize[index]
  });

public class PlayerItem
{
  public PlayerMovement Movement { get; set; }
  public PlayerHandler Handler { get; set; }
}

如果您想管理其他項目,例如可以這樣做:

int count = Math.Max(SaveValuesDeserialize.Count, NoteValuesDeserialzeList.Count);
for ( int index = 0; index < count; index++ )
{
  var movement = index < NoteValuesDeserialzeList.Count 
               ? NoteValuesDeserialzeList[index] 
               : null;
  var handler = index < SaveValuesDeserialize.Count 
              ? SaveValuesDeserialize[index] 
              : null;
  AllcombinedList.Add(new PlayerItem
  {
    Movement = movement,
    Handler = handler
  });
}

您必須自己決定如何處理其他項目(當其中一個計數多於另一個時):什么都不做,將另一個添加為 null 或創建一個默認實例。

泛型List<T>在其定義中僅接受一種類型T 因此,您不能在其中包含不同類型的元素,所有元素都必須是相同類型。

您可以使用classstruct來保存和公開這兩種數據類型,然后將此 class 或結構用作通用List<T>定義中的類型T 或者,您可以使用tuples

如果這種方法對您來說似乎過於復雜,那么您可以將PlayerHandlerPlayerMovement的祖先Object用作List<T>的類型T

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM