1不能使用“==”来判断相等,这种方法是值类型的
2不能使用Equals这种方法判断
需求分类(代码差别不大,就是细微的改动)
1首先只需要判断里面的元素是否相同,不关注每个对应的下标的元素是否相同,这样就很简单了,这样就是说,只要你们两个泛型的元素数目相同,然后元素也相同,就可以了,但是就是没有1对1的对应下标的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
[SerializeField ]
List<GameObject> game1 = new List<GameObject>();
[SerializeField ]
List<GameObject> game2 = new List<GameObject>();
int b = 0;
// Use this for initialization
void Start () {
JuegeList(game1 ,game2);
}
// Update is called once per frame
void Update () {
}
bool JuegeList(List <GameObject > game1,List <GameObject > game2) {
if (game1.Count == game2.Count)
{
for (int i = 0; i < game1.Count; i++)
{
for (int j = 0; j < game2.Count; j++)
{
if (game1[i] == game2[j]) {
b += 1;
}
}
}
if (b == game1.Count)
{
b = 0;
Debug.Log("相同");
return true;
}
else
{
b=0;
Debug.Log("不相同");
return false;
}
}
else {
Debug.Log("不相等");
return false;
}
}
}
2如果要求既要数量相等,也要相对应的索引所对应的元素相等,那就如下了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
[SerializeField ]
List<GameObject> game1 = new List<GameObject>();
[SerializeField ]
List<GameObject> game2 = new List<GameObject>();
int b = 0;
// Use this for initialization
void Start () {
JuegeList(game1 ,game2);
}
// Update is called once per frame
void Update () {
}
bool JuegeList(List <GameObject > game1,List <GameObject > game2) {
if (game1.Count == game2.Count)
{
for (int i = 0; i < game1.Count; i++)
{
if (game1[i] == game2[i]) {
b += 1;
}
}
Debug.Log(b);
if (b == game1.Count)
{
b = 0;
Debug.Log("相同");
return true;
}
else
{
b = 0;
Debug.Log("不相同");
return false;
}
}
else {
Debug.Log("不相等");
return false;
}
}
}