如何克隆ArrayList
并在Java中克隆其项目?
例如,我有:
ArrayList<Dog> dogs = getDogs();
ArrayList<Dog> clonedList = ....something to do with dogs....
Run Code Online (Sandbox Code Playgroud)
我希望那些物品clonedList
与狗列表中的物品不同.
import copy
a = "deepak"
b = 1, 2, 3, 4
c = [1, 2, 3, 4]
d = {1: 10, 2: 20, 3: 30}
a1 = copy.copy(a)
b1 = copy.copy(b)
c1 = copy.copy(c)
d1 = copy.copy(d)
print("immutable - id(a)==id(a1)", id(a) == id(a1))
print("immutable - id(b)==id(b1)", id(b) == id(b1))
print("mutable - id(c)==id(c1)", id(c) == id(c1))
print("mutable - id(d)==id(d1)", id(d) == id(d1))
Run Code Online (Sandbox Code Playgroud)
我得到以下结果 -
immutable - id(a)==id(a1) True
immutable - id(b)==id(b1) True
mutable - id(c)==id(c1) False
mutable - id(d)==id(d1) False
Run Code Online (Sandbox Code Playgroud)
如果我进行深度扫描 …
我一直在将从服务接收的所有数据直接保存到本地变量,控制器或范围.我认为会被认为是浅层副本,这是正确的吗?
Example:
DataService.callFunction()
.then(function(response) {
$scope.example = response.data;
});
Run Code Online (Sandbox Code Playgroud)
最近我被告知使用angular.copy来创建一个深拷贝.
$scope.example = angular.copy(response.data);
Run Code Online (Sandbox Code Playgroud)
但是,深度复制信息似乎在我的Angular应用程序使用时以相同的方式工作.使用深层复制(angular.copy)是否有特定的好处,你可以向我解释一下吗?
是否有任何内置功能可以让我深层复制NSMutableArray
?
我环顾四周,有些人说[aMutableArray copyWithZone:nil]
是深刻的副本.但我试过,它似乎是一个浅薄的副本.
现在我用for
循环手动执行复制:
//deep copy a 9*9 mutable array to a passed-in reference array
-deepMuCopy : (NSMutableArray*) array
toNewArray : (NSMutableArray*) arrayNew {
[arrayNew removeAllObjects];//ensure it's clean
for (int y = 0; y<9; y++) {
[arrayNew addObject:[NSMutableArray new]];
for (int x = 0; x<9; x++) {
[[arrayNew objectAtIndex:y] addObject:[NSMutableArray new]];
NSMutableArray *aDomain = [[array objectAtIndex:y] objectAtIndex:x];
for (int i = 0; i<[aDomain count]; i++) {
//copy object by object
NSNumber* n = [NSNumber …
Run Code Online (Sandbox Code Playgroud) 我对List副本有一些问题:
所以我E0
来之后'get_edge'
,我E0
打电话给我'E0_copy = list(E0)'
.在这里,我想E0_copy
是的深层副本E0
,和我通E0_copy
入'karger(E)'
.但在主要功能.
为什么'print E0[1:10]'
for循环之前的结果与for循环之后的结果不一样?
以下是我的代码:
def get_graph():
f=open('kargerMinCut.txt')
G={}
for line in f:
ints = [int(x) for x in line.split()]
G[ints[0]]=ints[1:len(ints)]
return G
def get_edge(G):
E=[]
for i in range(1,201):
for v in G[i]:
if v>i:
E.append([i,v])
print id(E)
return E
def karger(E):
import random
count=200
while 1:
if count == 2:
break
edge = random.randint(0,len(E)-1)
v0=E[edge][0]
v1=E[edge][1]
E.pop(edge)
if …
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace clone_test_01
{
public partial class MainForm : Form
{
public class Book
{
public string title = "";
public Book(string title)
{
this.title = title;
}
}
public MainForm()
{
InitializeComponent();
List<Book> books_1 = new List<Book>();
books_1.Add( new Book("One") );
books_1.Add( new Book("Two") );
books_1.Add( new Book("Three") );
books_1.Add( new Book("Four") );
List<Book> books_2 = new List<Book>(books_1);
books_2[0].title = "Five";
books_2[1].title = "Six";
textBox1.Text = books_1[0].title;
textBox2.Text = books_1[1].title;
} …
Run Code Online (Sandbox Code Playgroud) 如何将数组的每个元素(元素都是对象)复制到另一个数组中,以便它们完全独立?我不想更改一个数组中的元素来影响另一个元素.
可能重复:
如何克隆ArrayList并克隆其内容?
试图制作一个ArrayList的副本.底层对象很简单,包含字符串,整数,BigDecimals,Dates和DateTime对象.如何确保对新ArrayList所做的修改不会反映在旧的ArrayList中?
Person morts = new Person("whateva");
List<Person> oldList = new ArrayList<Person>();
oldList.add(morts);
oldList.get(0).setName("Mortimer");
List<Person> newList = new ArrayList<Person>();
newList.addAll(oldList);
newList.get(0).setName("Rupert");
System.out.println("oldName : " + oldList.get(0).getName());
System.out.println("newName : " + newList.get(0).getName());
Run Code Online (Sandbox Code Playgroud)
干杯,P
我做了一些搜索,发现了一些关于创建深度复制操作符的不同方法和帖子.
是否有一种快速简便的(内置)方式来深入复制Ruby中的对象?字段不是数组或散列.
在Ruby 1.9.2中工作.
deep-copy ×10
copy ×4
clone ×2
java ×2
javascript ×2
list ×2
python ×2
shallow-copy ×2
angularjs ×1
arraylist ×1
arrays ×1
c# ×1
cocoa ×1
cocoa-touch ×1
collections ×1
immutability ×1
nsarray ×1
object ×1
objective-c ×1
ruby ×1