c# - 如何通过组合每个列表中的一个元素将两个不同对象的列表组合成一个列表?
问题描述
嗨,我正在尝试将两个列表与不同的对象结合起来。
[{"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"}]}]
下面给出的类。
[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<?>();
。
加起来新列表的计数是多少(两个列表的组合计数)?
解决方案
因为 PlayerHandler 和 PlayerMovement 的唯一祖先是对象,所以只能创建一个对象列表:
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]));
}
或者您可以创建一个提供类型和命名的类:
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 或创建一个默认实例。
推荐阅读
- reactjs - Trying to up a server on MERN returning error
- javascript - 如何让我的 Discord Bot 忽略 TypeError 并继续执行我的功能?
- pandas - 如何按特定月份的时间组件过滤带有 xarray.sel() 的 NetCDF 数据集?
- python - 如何限制 PyTorch 张量的大小?
- javascript - 如何防止反应钩子中的竞争条件?
- selenium - 我有一个场景可以在 java selenium 中从我当前名为 test2 的测试中运行 test1
- vba - MS Outlook:自动保存和排序附件宏?
- jenkins - 将变量传递给詹金斯管道中的顺序阶段
- python - 如何从 DateTime 中删除 MicroSeconds
- c - 我试图允许用户将成员添加到结构数组中。我对 c 很陌生,所以我不确定要去做。代码链接如下