如何通过将每个列表中的一个元素组合在一起,将两个不同对象的列表组合为一个列表?

齐奥尼奥

嗨,我正在尝试将两个列表与不同的对象组合在一起。

[{"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>();

我想将上面的两个列表合并到第三个列表中,其中NoteValuesDeserialzeList [0]中的第0个元素,SaveValuesDeserialize [0]中的第一个元素,NoteValuesDeserialzeList [1]中的第二个元素,SaveValuesDeserialize [1]中的第三个元素,直到所有值相加。

出现各种疑问。新添加的列表的对象是什么List<?>AllcombinedList =new List<?>();

另外,新列表的总和(两个列表的总和)是多少?

奥利维尔·罗吉尔(Olivier Rogier)

因为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或创建默认实例。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Kotlin通过汇总每个元素将“ N”个列表组合在一起

合并两个具有Employee不同属性的数组列表以创建一个新的数组列表,将两个数组列表的属性组合在一起

两个列表整数在python中组合在一起

.net core-将一个func列表与单个func组合在一起

将字典列表与一个公共键组合在一起而不合并值

在SELECT中将两个列表组合在一起

将两个不同的表组合在一起进行查询

将一个元素与不同列的所有其他元素组合在一起 / R

将列表与列表中不同长度的列表组合在一起

JSON.net将2个列表(JObject)组合在一起

如何将两个通用队列组合在一起?

将两个SQL结果组合在一起以形成一个具有不同列名的结果集

将列表中的元素组合在一起形成新列表

如何将两个SQL查询与不同的条件组合在一起?

如何将两个具有不同行大小的数据框组合在一起?

将两个列表结合在一起

如何在没有任何包的情况下将列表中的“x”个元素组合在一起

使用dplyr,purrr将列表列表中的每个ith元素组合在一起

如何获得将其他两个列表中的所有项目组合在一起的列表?

如何将列与 Pandas 中的虚拟变量组合在一起(一个输出)?

如何返回将两个函数与两个参数组合在一起的函数

是否有GIMP功能通过将图层与不同背景上的一个对象组合在一起来删除背景?

将具有共同元素的两个列表结合在一起-Scala

将两个查询组合在一起以形成月份计数

将两个或多个线程组合在一起

将两个IQueryable组合在一起不起作用

将两个 -filter_complex 命令组合在一起

Python 以字母开头的行,将两个 for 循环组合在一起?

Lodash或将两个对象数组与匹配键组合在一起的最佳方法