克隆ConcurrentHashMap(Cloning ConcurrentHashMap)
为什么我不能克隆
ConcurrentHashMap
?ConcurrentHashMap<String, String> test = new ConcurrentHashMap<String, String>(); test.put("hello", "Salaam"); ConcurrentHashMap<String, String> test2 = (ConcurrentHashMap<String, String> ) test.clone(); System.out.println(test2.get("hello"));
如果我使用
HashMap
而不是ConcurrentHashMap
,它将起作用。Why can't I clone
ConcurrentHashMap
?ConcurrentHashMap<String, String> test = new ConcurrentHashMap<String, String>(); test.put("hello", "Salaam"); ConcurrentHashMap<String, String> test2 = (ConcurrentHashMap<String, String> ) test.clone(); System.out.println(test2.get("hello"));
If I use
HashMap
instead ofConcurrentHashMap
, it works.
原文:https://stackoverflow.com/questions/38417830
最满意答案
您要做的第一件事是创建一个空数据帧。 其次,您需要使用for循环在此数据帧中写入,而不是直接打印出结果。 此外,您不希望在已经使用i的for循环中使用包含i作为变量的for循环。 我建议你尝试以下方法:
data <- rep(1:3, times = c(10,4,6)) datafr <- data.frame(Sample=rep(NA,5),Probability.Dist=rep(NA,5)) for(i in 1:5) { samp <- sample(data, 4) data <- exclude(data, samp) datafr$Sample[i] <- samp[1]*1000+samp[2]*100+samp[3]*10+samp[4] #easy way of getting your wanted sample layout prsamp <- rep(0,3) for(j in 1:3) { prsamp[j] <- sum(samp == j)/4 } datafr$Probability.Dist[i] <- toString(prsamp) if (length(data) == 0) { break } } datafr # Sample Probability.Dist #1 1231 0.5, 0.25, 0.25 #2 2132 0.25, 0.5, 0.25 #3 1313 0.5, 0, 0.5 #4 2111 0.75, 0.25, 0 #5 3131 0.5, 0, 0.5
我还必须建议您不要在数据帧的单个列中使用3个值。 为了进一步分析甚至可读性,最好将每个值赋予它自己的列。
The first thing you will have to do is create an empty dataframe. Secondly, you will want your for loop to write in this dataframe instead of simply printing out the results directly. Also, you don't want to use a for loop containing i as variable in a for loop already using i. I suggest you try the following:
data <- rep(1:3, times = c(10,4,6)) datafr <- data.frame(Sample=rep(NA,5),Probability.Dist=rep(NA,5)) for(i in 1:5) { samp <- sample(data, 4) data <- exclude(data, samp) datafr$Sample[i] <- samp[1]*1000+samp[2]*100+samp[3]*10+samp[4] #easy way of getting your wanted sample layout prsamp <- rep(0,3) for(j in 1:3) { prsamp[j] <- sum(samp == j)/4 } datafr$Probability.Dist[i] <- toString(prsamp) if (length(data) == 0) { break } } datafr # Sample Probability.Dist #1 1231 0.5, 0.25, 0.25 #2 2132 0.25, 0.5, 0.25 #3 1313 0.5, 0, 0.5 #4 2111 0.75, 0.25, 0 #5 3131 0.5, 0, 0.5
I also have to advise you against using 3 values in a single column of a dataframe. For further analysis and even readability, it would be much preferred to give each value it's own column.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
要对代码进行矢量化,请使用ifelse()和| 而不是|| : ifelse( (tarr$month==12) | (tarr$month==3) |(tarr$month==6) | (tarr$month==9), tarr$day, ifelse((tarr$month==1) | (tarr$month==4), 31+tarr$day, ifelse((tarr$month==7) ...
-
将具有多个值的向量传递到R函数以生成数据帧(Passing vector with multiple values into R function to generate data frame)[2023-10-18]
在基地R, # To make a named vector, either: tmp <- sapply(split(table_wo_nas, table_wo_nas$ID), nrow) # OR just: tmp <- table(table_wo_nas$ID) # AND # arrange into data.frame nobs_df <- data.frame(ID = names(tmp), nobs = tmp) 或者,直接将表强制转换为data.frame ,并重命名: ... -
当你的数据expand.grid每行有两个以上的列时,这可能不会缩放,但是你可以在第一列使用expand.grid ,然后merge第二列。 df <- data.frame(a = 1:3, b = 5:7) c <- 9:10 combined <- expand.grid(a=df$a, c=c) combined <- merge(combined, df) > combined[order(combined$c), ] a c b 1 1 9 5 3 2 9 6 5 3 9 7 2 1 ...
-
如果你真的想要使用一个循环,猜猜这个有效 Results <- data.frame() Mystep <- 2 for(i in seq(0, 10, by = Mystep )){ xy = c(i*10, i/10) Results = rbind(Results, xy) } names(Results) = c("X", "Y") > Results X Y 1 0 0.0 2 20 0.2 3 40 0.4 4 60 0.6 5 80 0 ...
-
R很容易处理。 这是一种方法,但可能需要修改,具体取决于您想对所有这些随机数据集执行的操作。 这将建立一个包含100个标有“data001”到“data100”的矩阵的列表: library(MASS) d.cor <- .10 DATA <- replicate(100, mvrnorm(n=20, mu=c(0, 0), Sigma=matrix(c(1, d.cor, d.cor, 1), ncol=2), empirical=TRUE), simplify=FALSE) names <- ...
-
尝试这个: import numpy from matplotlib import pyplot as plt if __name__ == '__main__': x = [1, 2, 3] plt.ion() # turn on interactive mode for loop in range(0,3): y = numpy.dot(x, loop) plt.figure() plt.plot(x,y) plt ...
-
从计数数据生成数据帧(Generate Data Frame from Count Data)[2022-02-01]
我不完全确定采样与这个问题有什么关系? 它看起来像你在寻找untable 。 这是一个例子 数据: set.seed(1) no_drag = sample(1:5, 5) drag = sample(15:25, 5) df <- data.frame(names = LETTERS[1:5], drag, no_drag) names drag no_drag 1 A 24 2 2 B 25 ... -
您要做的第一件事是创建一个空数据帧。 其次,您需要使用for循环在此数据帧中写入,而不是直接打印出结果。 此外,您不希望在已经使用i的for循环中使用包含i作为变量的for循环。 我建议你尝试以下方法: data <- rep(1:3, times = c(10,4,6)) datafr <- data.frame(Sample=rep(NA,5),Probability.Dist=rep(NA,5)) for(i in 1:5) { samp <- sample(data, 4) data <- ...