将列表的部分复制到另一个列表

时间:2016-10-26 15:30:15

标签: c# list unity3d

我目前正在尝试将一个列表的部分复制到另一个列表中,我很难过。

我很擅长使用列表。

我创建了一个构造函数。

然后我创建了一个包含我需要的所有项目的列表。 我想将我的第一个列表中的随机片段复制到我的第二个列表中。 然后我想删除我从原始列表中复制的那篇文章。

这是我目前的代码。

我的构造函数代码

export class AppComponent implements OnInit {
 state: string = 'firstpos';

 toggleStates() {
   this.state = (this.state === 'firstpos' ? 'secondpos' : 'firstpos');
 }

 ngOnInit () {
  this.state = 'secondpos';
 }

}

这是我目前尝试将代码从一个组复制到另一个组。

using UnityEngine;
using System.Collections;

public class Character_Setup {

    // Declare Attributes
    public int Agility = 1;
    public int Stamina = 1;
    public int Strength = 1;
    public int Critical_Hit = 1;
    public string Weapon ="";


    // Declare Stats
    public int Speed = 1;
    public int Health= 2;
    public int Damage = 3;
    public int Acuity = 4;

    public Character_Setup(string _name)
    {
        Weapon = _name;
        Speed = Agility * Random.Range (1,10);
        Health = Stamina * Random.Range (1,10);
        Damage = Strength * Random.Range (1,10);
        Critical_Hit = Acuity * Random.Range (1,10);

    }

}

此代码目前复制了所有内容。我希望能够在Characters中选择一些内容并将其复制到Blue_Team。

我已经通过互联网了,此刻我有点失落。

如果只从字符列表中复制一个部分然后将其删除,我该怎么办?

3 个答案:

答案 0 :(得分:1)

如果我了解您要求的内容,则此代码会根据prototyping值将单个Character复制到Blue_Team,但这可以更改为您喜欢的任何内容

Character.Weapon

如果您想要多个对象,可以使用以下代码返回列表:

Character selectedChar = Characters.Where(objChar => objChar.Weapon == "Knife");

Blue_Team.Add(selectedChar);

这种方法使用List<Character> selectedCharList = Characters.Where(objChar => objChar.Weapon == "Knife").ToList(); Blue_Team.AddRange(selectedCharList); ,这在处理列表时非常方便。我一直都在使用它。

答案 1 :(得分:1)

首先要感谢答案。他们没有给我直接答案,但他们确实引导我走上了正确的道路,这使我得到了答案。这正是我所需要的。

我还学会了更清楚我的代码和评论。

这是我为实现我想要的目的而编写的代码。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Game_Play : MonoBehaviour {

    public int Num_Players = 11;
    public int Players = 1;




    // Use this for initialization
    void Start () {

        //Section 1
        //Create a list
        // Using Character_Setup
        //add 10 characters to list

        List <Character_Setup> Characters = new List<Character_Setup> ();

        Characters.Add (new Character_Setup ("Flamethrower"));
        Characters.Add (new Character_Setup ("Shotgun"));
        Characters.Add (new Character_Setup ("Rifle"));
        Characters.Add (new Character_Setup ("Pistol"));
        Characters.Add (new Character_Setup ("Machine Gun"));
        Characters.Add (new Character_Setup ("Grenade Launcher"));
        Characters.Add (new Character_Setup ("Knife"));
        Characters.Add (new Character_Setup ("Rocket Launcher"));
        Characters.Add (new Character_Setup ("Throwing Star"));
        Characters.Add (new Character_Setup ("Sling Shot"));

        foreach (Character_Setup character in Characters) {
            print (character.Weapon);
        }

        //Section 2
        // Create 2 teams Off 3 Players

        // Team 1
        //Create Blue Team
        //use loop to fill team
        //Create Variable to hold selection number
        //Add to team
        //Remove from Characters availble
        List<Character_Setup> Blue_Team = new List<Character_Setup> ();
        while (Blue_Team.Count < 3)
        {
            Character_Setup PlayerSelection = Characters [UnityEngine.Random.Range(0, Characters.Count)];
            Blue_Team.Add (PlayerSelection);
            Characters.Remove(PlayerSelection);
        }

        foreach (Character_Setup character in Blue_Team)
        {
            print (character.Weapon);
        }

        // Team 2
        //Create Red Team
        List<Character_Setup> Red_Team = new List<Character_Setup> ();
        while (Red_Team.Count < 3)
        {
            Character_Setup PlayerSelection = Characters [UnityEngine.Random.Range (0, Characters.Count)];
            Red_Team.Add (PlayerSelection);
            Characters.Remove(PlayerSelection);
        }

        foreach (Character_Setup character in Red_Team)
        {
            print (character.Weapon);
        }

    }

答案 2 :(得分:0)

一种方法是在Team课程中拥有Character_Setup属性,然后您可以选择由该属性进入哪个列表的人员。应该有枚举支持..但要开始只需添加public int Team = 0,然后在构造函数中允许为“蓝队”分配Team = 1,为“红队”分配Team = 2(团队0)是“没有团队”,如果你想支持或不支持,如果不是只允许1和2,而不是0,1,2)。

然后你可以做Blue_Team.AddRange(Characters.Where(c => c.Team == 1));

没有LINQ的另一种方法就是这样:

foreach (var charact in Characters)
{
    if (charact.Team == 1)
    {
        Blue_Team.Add(charact);
    }

    if (charact.Team == 2)
    {
        Red_Team.Add(charact);
    }    
}