检查两个阵列分享相同的元件 [英] Check if two arrays share the same elements

查看:107
本文介绍了检查两个阵列分享相同的元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了这个code在.playgraound

I've written this code in a .playgraound

var a = [1, 2, 3]
var b = a
var c = a

if b === c
{
    "b and c still share the same array elements."
}
else
{
    "b and c now refer to two independent sets of array elements."
}

结果是b和现在c指的是两套独立的数组元素的,但在雨燕程序设计语言苹果表示,

The result is "b and c now refer to two independent sets of array elements" but in "The Swift Programming Language" Apple says that

下面的示例使用等同于运算符(===),检查是否b和c仍共享相同的数组元素。

The example below uses the "identical to" operator (===) to check whether b and c still share the same array elements.

你能解释我为什么它们是不同的?

Can you explain me why they are different?

推荐答案

究其原因,书上说

的结果是b和现在c指的是两组独立的数组元素的

The result is "b and c now refer to two independent sets of array elements"

是,code本书前面停下阵列之间共享b 并通过建立 C A 的分配给两个人:

is that the code earlier in the book stopped array sharing between b and c established by the assignment of a to both of them:

b.unshare() // Page 306, line 3

阵列 A 已经取消共享从 B C 隐式305页追加一个元素是,1号线。

Array a has been unshared from b and c implicitly by appending an element to it on page 305, line 1.

随着code为你展示b和c仍共享相同的数组元素。将打印信息。

With the code as you show the "b and c still share the same array elements." message will be printed.

这篇关于检查两个阵列分享相同的元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆