【问题标题】:How to combine two list of different objects into one list by combining one element from each list?如何通过组合每个列表中的一个元素将两个不同对象的列表组合成一个列表?
【发布时间】:2019-11-01 06:29:31
【问题描述】:

您好,我正在尝试将两个列表与不同的对象结合起来。

[{"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],第一个元素来自 SaveValuesDeserialize[0],第二个元素来自 NoteValuesDeserialzeList[1],第三个元素来自 SaveValuesDeserialize[1]直到所有的值加起来。

各种疑问出现了。新增列表List&lt;?&gt;AllcombinedList =new List&lt;?&gt;();的对象会是什么。

还有什么将添加新列表的计数(两个列表的组合计数)?

【问题讨论】:

  • 将问题标记为重复会忽略问题的核心。
  • 我真的认为最好知道你为什么要这样做?不,您不能像人们所说的那样在列表中包含不同的类型,但是您可以通过其他方式存储这些数据,但这取决于您想要做什么
  • 我想把它全部放在一个列表中,这样我就可以将它与下一个和上一个按钮一起使用。我需要使用 counter++ 和 counter-- 来使用它。否则真的很难去做。

标签: c# list


【解决方案1】:

因为PlayerHandler和PlayerMovement的唯一祖先是object,所以只能创建一个List of Objects:

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

【讨论】:

  • 我尝试了你的解决方案的最后一个代码。SaveValuesDeserialize 计数为 10,NoteValuesDeserialzeList 计数为 4。所以它只需要最小计数。那么它将如何从 for ( int index = 0; index &lt; count; index++ ) AllcombinedList.Add(new PlayerItem { Movement = NoteValuesDeserialzeList[index] Handler = SaveValuesDeserialize[index], }); 我添加 SaveValuesDeserialize 的所有值尝试了代码,但是当我收到消息时,所有最后的消息都在重复。
  • 遇到了一个问题。我试图获取两个列表并合并到一个列表中,以便我可以轻松地使用下一个按钮和上一个按钮来实现一个一个获取数据。由于长度变化有点混乱.用JSON更新了问题。我认为这个逻辑在这里不起作用。因为我想最初显示notemsg [0]值。然后当我点击下一步按钮时想要应用SaveValues [0]的所有值。然后当我点击下一步再次 notemsg[1] ,当我点击下一个 SaveValues[1] 等等。但是在这里我一个一个地添加列表。
  • 如何从 Allcombined 列表中获取价值?
  • 带有元组列表:AllcombinedList[0].Item1 和 .Item2。带有 PlayerInfo 列表:AllcombinedList[0].Movement 和 .Handler。
【解决方案2】:

泛型 List&lt;T&gt; 在其定义中仅接受一种类型 T。因此,您不能在其中包含不同类型的元素,所有元素都必须属于同一类型。

您可以使用class 或struct 来保存和公开这两种数据类型,然后在通用List&lt;T&gt; 定义中使用此类或结构作为T 类型。或者,您可以使用tuples。

如果这种方法对您来说过于复杂,那么您可以将 T 用作您的 List&lt;T&gt; 的类型 PlayerHandler 和 PlayerMovement 的祖先 Object。

【讨论】:

  • 装箱和拆箱仅发生在值类型中,并且值类型 (int) 被包装在对象引用中并移动到堆中。上述类型是类,即已经在堆上创建的引用类型,因此没有装箱,您只有一个对 PlayerHandler 和 PlayerMovement 对象的对象引用列表。
猜你喜欢
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
  • 2017-04-12
  • 2020-01-27
  • 2015-10-30
  • 1970-01-01
  • 2018-10-05
  • 1970-01-01
相关资源
最近更新 更多