是否有一种简单的方法可以基本上只使用此方法获取数据的副本而不是引用?我试过.ToArray().Where()但似乎仍然传递了一个引用.
例:
static void Main(string[] args)
{
List<ob> t = new List<ob>();
t.Add(new ob() { name = "hello" });
t.Add(new ob() { name = "test" });
ob item = t.Where(c => c.name == "hello").First();
// Changing the name of the item changes the original item in the list<>
item.name = "burp";
foreach (ob i in t)
{
Console.WriteLine(i.name);
}
Console.ReadLine();
}
public class ob
{
public string name;
}
Run Code Online (Sandbox Code Playgroud) 我有一个指向带有union的结构的指针
所以我们说我们有
struct A {
union {
char **word;
struct A *B
} u;
};
Run Code Online (Sandbox Code Playgroud)
我有类型A*的变量x和y
typedef A* A_t;
A_t x;
A_t y;
Run Code Online (Sandbox Code Playgroud)
将x-> u = y->你足以复制联盟中的东西.
假设您使用嵌套的PHP数组创建一个复杂的数据结构,如下所示:
$a1 = array(
'b' => array('foo' => 1),
'c' => array('bar' => 1)
);
Run Code Online (Sandbox Code Playgroud)
想象一下,数组要更加嵌套,具有更多元素和更长,更有意义的名称.
如果需要经常访问$ a1的子结构,对于读写,可能会想要创建这样的"别名":
$b = &$a1['b'];
Run Code Online (Sandbox Code Playgroud)
然而,由于"任务"实际上改变了$ a1,这导致了巨大的混乱.
我认为许多缺乏经验的PHP开发人员(比如我)会认为$ b是在分配后对$ a1 ['b']的引用.真正发生的是$ b AND $ a1 ['b']变成对元素的引用array('foo' => 1),带来意想不到的后果.我发现这非常不直观.
假设您需要保留$ a1,但是您还需要$ a1的副本,让我们调用副本$ a2,并更改$ a2的一些元素:
$a2 = $a1; // Copy $a1 to $a2
$a2['b']['foo'] = 2; // GOTCHA! This will change $a1['b']['foo'] as well!
$a2['c']['bar'] = 2; // This will not change $a1['c']['bar'] however
Run Code Online (Sandbox Code Playgroud)
因为我们之前创建了对$ a1 ['b']的引用,所以该元素将通过引用分配,而$ a1的其余部分按值复制.这花了我几个小时才弄明白. …
如果您的问题作为此问题的重复项而被关闭,那是因为您有一些通用形式的代码
x = X()
# later...
x = x.y()
# or:
x.y().z()
Run Code Online (Sandbox Code Playgroud)
其中X是某种类型,它提供了y旨在z变异(修改)对象(X类型的实例)的方法。这可以适用于:
list、dict和setbytearray这种形式的代码很常见,但并不总是错误的。问题的明显迹象是:
与x.y().z()一样,会引发异常AttributeError: 'NoneType' object has no attribute 'z'。
有了x = x.y(),x就变成None, 而不是被修改的对象。这可能会被后来的错误结果发现,或者被像上面这样的异常(x.z()稍后尝试时)发现。
Stack Overflow 上有大量关于这个问题的现有问题,所有这些问题实际上都是同一个问题。之前甚至有多次尝试在特定上下文中涵盖同一问题的规范。然而,理解问题并不需要上下文,因此这里尝试一般性地回答:
代码有什么问题吗?为什么这些方法会这样,我们如何解决这个问题?
另请注意,当尝试使用 alambda(或列表理解)来产生副作用时,会出现类似的问题。
同样明显的问题可能是由因其他原因返回的方法引起的None …
朋友们,
我正面临一个问题,我有phoneContacts列表,其中包含姓名和电话号码.我想将它复制到两个不同的静态列表中,以便我可以将它用于其他活动.我正在使用以下代码,但它在显示我最后一个列表引用时检索数据任何一个指导我如何能够采取这两个对象的单独副本?
MyContacts.attackContacts = new ArrayList(phoneContacts);
Collections.copy(MyContacts.attackContacts,phoneContacts);
MyContacts.attackContacts.get(0).setType("attack");
MyContacts.medicalContacts = new ArrayList(phoneContacts);
Collections.copy(MyContacts.medicalContacts,phoneContacts);
MyContacts.medicalContacts.get(0).setType("medical");
System.out.println("attack" + MyContacts.attackContacts.get(0).getType() + " medical " + MyContacts.medicalContacts.get(0).getType());
// result "attack medical" "medical medical"
// it should show independent list results like "attack attack" "medical medical"
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.
我有两个JSON文件,我从wordpress导出,具有相应的ID我想将它们组合成一个Json文件,所以我可以把它带到我用Gatsby JS构建的网站.其中一个文件是posts.json,另一个是postsMeta.json.postsMeta中的post_id 与帖子中的ID相对应
我怎样才能最好地合并两者呢?我可以for在js中运行某种循环吗?我怎么会这样?我在窗户上有一些json探险家,可以帮助我做到这一点.
最后我还想修剪出一些unnecasry fiels如post_parent在帖子 JSON和喜欢的东西meta_key在postsMeta JSON.
好的,希望这很清楚,提前谢谢.
以下是两个文件中第一个对象对应的示例
posts.json
{"ID":"19","post_author":"2","post_date":"2010-12-31 23:02:04","post_date_gmt":"2010-12-31 23:02:04","post_content":"Harry Potter was not available for the first sitting of the Halloween Picture. I hope everyone had a safe and fun Halloween. Tomorrow is picture retake day, please send back your previous prints if you want retakes. It is also hot lunch. See You tomorrow!","post_title":"Happy Halloween","post_excerpt":"","post_status":"publish","comment_status":"open","ping_status":"open","post_password":"","post_name":"happy-halloween","to_ping":"","pinged":"","post_modified":"2011-01-03 05:26:11","post_modified_gmt":"2011-01-03 05:26:11","post_content_filtered":"","post_parent":"0","guid":"http:\/\/localhost\/mrskitson.ca_wordpress\/?p=19","menu_order":"0","post_type":"post","post_mime_type":"","comment_count":"1"},
Run Code Online (Sandbox Code Playgroud)
postsMeta.json
{"meta_id":"27","post_id":"19","meta_key":"large_preview","meta_value":"http:\/\/www.mrskitson.ca\/wp-content\/uploads\/2010\/12\/halloween.jpg"},
Run Code Online (Sandbox Code Playgroud)
更新: …
在LISP中,我有一个传递列表的函数.我想在不更改原始列表的情况下更改此列表的元素.通常,我会copy-list用来创建我将更改的列表的本地副本,但这似乎不起作用:
CL-USER> (defun test (item)
(let ((copy (copy-list item)))
(setf (nth 0 (nth 0 (nth 0 copy))) t)
(print item)
(print copy)))
CL-USER> (defparameter item `(((NIL NIL) (NIL NIL) (NIL NIL))
((NIL NIL NIL) (NIL NIL NIL))
((3 3) (NIL NIL))))
CL-USER> (test item)
(((T NIL) (NIL NIL) (NIL NIL)) ((NIL NIL NIL) (NIL NIL NIL)) ((3 3) (NIL NIL)))
(((T NIL) (NIL NIL) (NIL NIL)) ((NIL NIL NIL) (NIL NIL NIL)) ((3 3) (NIL NIL)))
(((T NIL) …Run Code Online (Sandbox Code Playgroud) 我正在研究d3js和分层布局的数据可视化.我的数据如下:
0
/ | \
/ | \
1 5 3
\ | |
\ | |
4 /
| /
2
Run Code Online (Sandbox Code Playgroud)
因为我无法链接到多个父节点,所以我复制了显示的节点:
0
/ | \
/ | \
1 5 3
| | |
| | |
4 4 |
| | |
2 2 2
Run Code Online (Sandbox Code Playgroud)
我做了一个小提琴演示来展示我的问题:
这是我用来解析输入的循环:
for (i in root[2].Arcs){
var d = root[1].Nodes[root[2].Arcs[i].D];
var s = root[1].Nodes[root[2].Arcs[i].S];
if (!d.children){
d.children = [];
}
d.children.push(s);
}
Run Code Online (Sandbox Code Playgroud)
对我来说:控制台中的两个打印元素都是相同的,但不是布局的渲染.对象引用中可能存在一些不同之处.
我发现一个糟糕的解决方案是解码然后编码我的var:
var root …Run Code Online (Sandbox Code Playgroud) 我是一个仍然在java的新手,你能告诉我这两个构造函数之间的区别吗?
第一:
public class Plan
{
ArrayList<Point2D> points;
public Plan(ArrayList<Ponto2D> points)
{
this.points = new Arraylist<Point2D>(points);
}
}
Run Code Online (Sandbox Code Playgroud)
这个:第二个:
public class Plan
{
public Plan(ArrayList<Point2D> lpoints)
{
points = new ArrayList<Point2D>();
for(Point2D p : lpoints) point.add(p.clone());
}
}
Run Code Online (Sandbox Code Playgroud) 这是我最近在我的课程中提出的T/F问题,我发现有点令人困惑.
教科书指出:
=运算符可用于将一个对象的数据分配给另一个对象,或者用另一个对象的数据初始化一个对象.默认情况下,一个对象的每个成员都复制到另一个对象中的对应成员.
逐字的问题是:
除非重载运算符,否则不能使用=运算符将一个对象的值分配给另一个对象.T/F?
从教科书的那一段来看,我的回答是错误的.然而,事实证明测验答案实际上是正确的.
当我在网上查询问题时,我看到其他来源也将答案列为"假".当然,这些只是通用的闪卡/测验网站,所以我不会在其中放入太多的股票.
基本上,我只是好奇真正的答案是为了未来的学习目的.
PS:教科书后来继续说:"为了改变赋值运算符的工作方式,它必须重载.运算符重载允许你在与类对象一起使用时重新定义现有运算符的行为."
我觉得这是相关的,支持"真实"的答案,但我不太确定.