[英]Copy Parts of a list to another list
我目前正在嘗試將一個列表的各個部分復制到另一列表中,但現在很難。
我對使用列表非常陌生。
我已經創建了一個構造函數。
然后,我創建了一個列表,其中包含我需要的所有項目。 我想從第一個列表中復制一個隨機片段到第二個列表中。 然后,我想從原始列表中刪除我復制的作品。
這是我當前的代碼。
我的構造函數代碼
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);
}
}
這是我當前嘗試將代碼從一個組復制到另一個組的嘗試。
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 () {
//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);
}*/
// Create 2 teams Off 3 Players
// Team 1
// Create Blue Team
List<Character_Setup> Blue_Team = new List<Character_Setup> ();
Blue_Team.AddRange (Characters);
foreach (Character_Setup character in Blue_Team)
{
print (character.Weapon);
}
// Team 2
//Create Red Team
//List<Character_Setup> Red_Team = new List<Character_Setup> ();
//Red_Team.AddRange (Characters);
}
// Update is called once per frame
void Update () {
}
}
當前,此代碼將所有內容復制過來。 我希望能夠在Characters中選擇某些內容並將其復制到Blue_Team。
我已經上網,現在迷路了。
我該怎么做,只復制字符列表中的一部分,然后將其刪除?
如果我理解您的要求,此代碼將根據Character.Weapon
值將單個Character
復制到Blue_Team
,但是可以將其更改為所需的任何值。
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);
這種方法使用System.Linq
,在使用列表時非常方便。 我用它所有的時間。
首先感謝您的回答。 他們沒有給我直接的答案,但是他們的確帶我走了正確的道路,這使我得到了我的答案。 這正是我所需要的。
我還學會了用代碼和注釋更加清晰。
這是我為實現所需代碼而編寫的代碼。
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);
}
}
一種方法是在Character_Setup
類中具有Team
屬性,然后您可以通過該屬性選擇誰進入列表。 應該有一個枚舉。.但是要開始,只需添加public int Team = 0
,然后在構造函數中允許將Team = 1
分配給“ blue team”,將Team = 2
分配給“ red team”(team 0是“ no team” ”,由您決定是否支持,如果不允許,則只允許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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.