我在表单中有一个嵌套集合:
HashMap<String, HashMap<String, List<String>>> errorList;
现在我使用像这样的双括号内联初始化它
errorList.put(tempName, new HashMap<String, List<String>>() {{
put("upl", new ArrayList<String>() {{ add("Y"); add("Upload Success"); }});
}});
这位于foreach循环中,tempName
的值在每次迭代中都会发生变化。
我这样做是因为我无法使用List<String>
或HashMap<String,List<String>>
的实例,因为每次我更改该实例中的值时,它都会反映在它嵌套的集合中。所以我被迫创建新的双括号初始化的实例。
事情是:我想使用列表对象。而不是
new ArrayList<String>() {{ add("Y"); add("Upload Success"); }}
我想使用变量。
我该怎么做?
答案 0 :(得分:2)
而不是
Random rand = new Random();
int RandomOne = rand.Next(0, 10);
int RandomTwo = rand.Next(0, 10);
int RandomThree = rand.Next(0, 10);
int RandomFour = rand.Next(0, 10);
int RandomFive = rand.Next(0, 10);
int RandomSix = rand.Next(0, 10);
int RandomSeven = rand.Next(0, 10);
int RandomEight = rand.Next(0, 10);
int RandomNine = rand.Next(0, 10);
int RandomZero = rand.Next(0, 10);
btnOneNgra.Text = Convert.ToString(RandomOne);
if ((RandomTwo == RandomOne) || (RandomTwo == RandomNine) || (RandomTwo == RandomThree) || (RandomTwo == RandomFour)
|| (RandomTwo == RandomFive) || (RandomTwo == RandomSix) || (RandomTwo == RandomSeven) || (RandomTwo == RandomEight) || (RandomTwo == RandomZero))
{
RandomTwo = rand.Next(0, 10);
}
else
{
btnTwoNgra.Text = Convert.ToString(RandomTwo);
}
if ((RandomThree == RandomOne) || (RandomThree == RandomTwo) || (RandomThree == RandomNine) || (RandomThree == RandomFour)
|| (RandomThree == RandomFive) || (RandomThree == RandomSix) || (RandomThree == RandomSeven) || (RandomThree == RandomEight) || (RandomThree == RandomZero))
{
RandomThree = rand.Next(0, 10);
}
else
{
btnThreeNgra.Text = Convert.ToString(RandomThree);
}
if ((RandomFour == RandomOne) || (RandomFour == RandomTwo) || (RandomFour == RandomThree) || (RandomFour == RandomNine)
|| (RandomFour == RandomFive) || (RandomFour == RandomSix) || (RandomFour == RandomSeven) || (RandomFour == RandomEight) || (RandomFour == RandomZero))
{
RandomFour = rand.Next(0, 10);
}
else
{
btnFourNgra.Text = Convert.ToString(RandomFour);
}
if ((RandomFive == RandomOne) || (RandomFive == RandomTwo) || (RandomFive == RandomThree) || (RandomFive == RandomFour)
|| (RandomFive == RandomNine) || (RandomFive == RandomSix) || (RandomFive == RandomSeven) || (RandomFive == RandomEight) || (RandomFive == RandomZero))
{
RandomFive = rand.Next(0, 10);
}
else
{
btnFiveNgra.Text = Convert.ToString(RandomFive);
}
if ((RandomSix == RandomOne) || (RandomSix == RandomTwo) || (RandomSix == RandomThree) || (RandomSix == RandomFour)
|| (RandomSix == RandomFive) || (RandomSix == RandomNine) || (RandomSix == RandomSeven) || (RandomSix == RandomEight) || (RandomSix == RandomZero))
{
RandomSix = rand.Next(0, 10);
}
else
{
btnSixNgra.Text = Convert.ToString(RandomSix);
}
if ((RandomSeven == RandomOne) || (RandomSeven == RandomTwo) || (RandomSeven == RandomThree) || (RandomSeven == RandomFour)
|| (RandomSeven == RandomFive) || (RandomSeven == RandomSix) || (RandomSeven == RandomNine) || (RandomSeven == RandomEight) || (RandomSeven == RandomZero))
{
RandomSeven = rand.Next(0, 10);
}
else
{
btnSevenNgra.Text = Convert.ToString(RandomSeven);
}
if ((RandomEight == RandomOne) || (RandomEight == RandomTwo) || (RandomEight == RandomThree) || (RandomEight == RandomFour)
|| (RandomEight == RandomFive) || (RandomEight == RandomSix) || (RandomEight == RandomSeven) || (RandomEight == RandomNine) || (RandomEight == RandomZero))
{
RandomEight = rand.Next(0, 10);
}
else
{
btnEightNgra.Text = Convert.ToString(RandomEight);
}
if ((RandomNine == RandomOne) || (RandomNine == RandomTwo) || (RandomNine == RandomThree) || (RandomNine == RandomFour)
|| (RandomNine == RandomFive) || (RandomNine == RandomSix) || (RandomNine == RandomSeven) || (RandomNine == RandomEight) || (RandomNine == RandomZero))
{
RandomNine = rand.Next(0, 10);
}
else
{
btnNineNgra.Text = Convert.ToString(RandomNine);
}
if ((RandomZero == RandomOne) || (RandomZero == RandomTwo) || (RandomZero == RandomThree) || (RandomZero == RandomFour)
|| (RandomZero == RandomFive) || (RandomZero == RandomSix) || (RandomZero == RandomSeven) || (RandomZero == RandomEight) || (RandomZero == RandomNine))
{
RandomZero = rand.Next(0, 10);
}
else
{
btnZeroNgra.Text = Convert.ToString(RandomZero);
}
你可以使用
string script = @
"<script type='text/javascript'>
alert('Booking Complete! Thank you for choosing Euro-City-Tours');
</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
这为您提供了固定大小的列表。如果您希望以后能够添加或删除元素,请将其转换为new ArrayList<String>() {{ add("Y"); add("Upload Success"); }}
:
Arrays.asList("Y", "Upload Success")
当然,在将其放入地图结构之前,您可以将此列表放入其自己的变量中。
如果你想要放置ArrayList
或new ArrayList<>(Arrays.asList("Y", "Upload Success"))
并确保列表不在地图条目之间共享,这里有一个建议:首先,在你的循环之外:
[Y, Upload Success]
然后在你的循环中
[N, Upload Failed]
您可以更进一步,并在循环外构建地图。同样,如果您希望内部地图为final List<String> successList = Arrays.asList("Y", "Upload Success");
final List<String> failureList = Arrays.asList("N", "Upload Failed");
,只需转换为一个:if (wasSuccessful) {
errorList.put(tempName,
Collections.singletonMap("upl", new ArrayList<>(successList)));
} else {
errorList.put(tempName,
Collections.singletonMap("upl", new ArrayList<>(failureList)));
}
。
我还没有测试过这段代码,希望你能解决这个问题。
你注意到我完全避免了双括号初始化。虽然它很简短,但它在概念和性能方面都有开销。你每次都在创建一个新的匿名子类,我认为这是不合理的。