我们可以在finally块中使用return语句.这会导致任何问题吗?
Hashtable和Collections.synchronizedMap是线程安全的,但仍然是复合操作
if (!map_obj.containsKey(key)) {
map_obj.put(key, value);
}
Run Code Online (Sandbox Code Playgroud)
需要外部同步:
synchronized(map_obj) {
if (!map_obj.containsKey(key)) {
map_obj.put(key, value);
}
}
Run Code Online (Sandbox Code Playgroud)
假设我们有ConcurrentHashMap(CHM)而不是Hashtable或HashMap.CHM为putIfAbsent()上述复合操作提供了另一种方法,因此无需外部同步.
但是假设putIfAbsent()CHM 没有提供.然后我们可以写下面的代码:
synchronized(concurrenthashmap_obj) {
if (!concurrenthashmap_obj.containsKey(key)) {
concurrenthashmap_obj.put(key, value);
}
}
Run Code Online (Sandbox Code Playgroud)
我的意思是我们可以在CHM对象上使用外部同步吗?它会起作用吗?
对于上述复合操作,putIfAbsent()在CHM中有方法,但是如果我们使用CHM,我们如何实现其他复合操作的线程安全性.我的意思是我们可以在CHM对象上使用外部同步吗?
我想减去2个double值,我尝试了以下代码.
double val1 = 2.0;
double val2 = 1.10;
System.out.println(val1 - val2);
Run Code Online (Sandbox Code Playgroud)
我得到了输出,
0.8999999999999999
Run Code Online (Sandbox Code Playgroud)
为了获得输出,0.9我尝试BigDecimal如下,
BigDecimal val1BD = new BigDecimal(val1);
BigDecimal val2BD = new BigDecimal(val2);
System.out.println(val1BD.subtract(val2BD));
Run Code Online (Sandbox Code Playgroud)
我得到了输出,
0.899999999999999911182158029987476766109466552734375
Run Code Online (Sandbox Code Playgroud)
然后我尝试了 BigDecimal.valueOf()
val1BD = BigDecimal.valueOf(val1);
val2BD = BigDecimal.valueOf(val2);
System.out.println(val1BD.subtract(val2BD));
Run Code Online (Sandbox Code Playgroud)
最后我得到了输出0.9.
我的问题是案例2和案例3有什么区别?
在案例2中为什么我得到这样的输出?
请帮我如何更改AVCaptureVideoPreviewLayer帧大小和
我过世了Height =100px ; and width = 300px.
如果我通过高度大小100比宽度不改变为300px
这是代码:
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
previewLayer.frame = CGRectMake(20, 50, 300, 100);
self.view.frame=CGRectMake(20, 50, 300, 100);
CGRect layerRect = [[[self view] layer] bounds];
[previewLayer setBounds:layerRect];
[previewLayer setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
[[[self view] layer] addSublayer:previewLayer];
[previewView.layer addSublayer:previewLayer];
Run Code Online (Sandbox Code Playgroud) 我尝试将两个BigDecimal值乘以multiply如下方法,
BigDecimal dur = BigDecimal.valueOf(60/1.1);
BigDecimal bal = BigDecimal.valueOf(1.1);
BigDecimal ans = dur.multiply(bal);
System.out.println("Ans:"+ans);
Run Code Online (Sandbox Code Playgroud)
我除了ans之外60.但我得到了,
Ans:59.999999999999994
Run Code Online (Sandbox Code Playgroud)
为什么会这样,我们如何解决它.
我尝试了以下代码:
String str = "Str1";
switch(str) {
case Constants.First_String : System.out.println("First String");
break;
case Constants.Second_String : System.out.println("Second String");
break;
default : System.out.println("Default String");
}
Run Code Online (Sandbox Code Playgroud)
我的Constants班级是,
public class Constants {
public static String First_String = "Str1";
public static String Second_String = "Str2";
public static String Third_String = "Str3";
}
Run Code Online (Sandbox Code Playgroud)
我收到了编译错误,
线程"main"中的异常java.lang.Error:未解决的编译问题:case表达式必须是常量表达式
但是当我尝试使用以下代码时,
switch(str){
case "Str1" : System.out.println("First String");
break;
case "Str2" : System.out.println("Second String");
break;
default : System.out.println("Default String");
}
Run Code Online (Sandbox Code Playgroud)
没有编译错误,并将输出打印为,
First String
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么在第一种情况下会出现补偿错误.我该如何解决呢.
我设置了一个数组,我想创建一个方法,返回一个反向元素的数组.例如,如果有10个槽,那么array1[9] = 6那么array2[0] = 6.
我想我必须返回一个数组 - 我该怎么做?
而且我不知道如何反转并添加到另一个数组.
谢谢!
int[] arr = {43, 22, 1, 44, 90, 38, 55, 32, 31, 9};
Console.WriteLine("Before");
PrintArray(arr);
Console.WriteLine("After");
Reverse(arr);
Console.ReadKey(true);
}
static int[] Reverse(int[] array)
{
for (int i = array.Length; i < 1; i--)
{
int x = 0;
array[i] = array[x++];
Console.WriteLine(array[i]);
}
}
static void PrintArray(int[] array)
{
for (int j = 0; j < array.Length; j++)
{
Console.Write(array[j] + " ");
}
Console.WriteLine("");
Run Code Online (Sandbox Code Playgroud) 我正在为我的雇主制作一份自定义Excel电子表格.
我想出了一个非常讨厌的功能,它可以实现我想要的功能,但我希望能够简化它.
我很熟悉C,C#和Java.我想VBA尽可能避免使用,但如果这是我唯一的选择,我可以考虑使用它.
我正在为制造工厂制定计划表,处理订单并从库存中扣除材料等.我为工厂生产的所有产品设置了页面.每种产品都使用不同的材料.我目前有一个讨厌的if语句,它检查2个表之间的匹配字符串,然后在找到匹配时用相关信息填充其他一些单元格.
我目前有一个包含20个条件的if语句!我永远不会在真正的程序中做这样的事情!
无论如何,我可以基本上循环遍历一系列单元格,直到找到匹配然后从那里开始?这是我正在尝试做的一个例子:
选项卡:MoldInformation - 包含工厂生产的产品的所有必要信息.选项卡:订单 - 包含客户已下达的所有订单.
在Orders选项卡中,第一个单元格标题为Mold,用户将在其中输入字符串,然后在orders选项卡中,有关于生成订单所需的材料数量的信息.如果输入某个模具,则与该模具对应的材料将填充订单中的相应选项卡.
我所有的计算都很好,但我的if语句看起来像这样:
= IF(A3 = MoldInformation!$ A $ 3 E3/MoldInformation!$ F $ 3,如果(A3 = MoldInformation!一个$ 4中,E3/MoldInformation!$ F $ 4中,IF(A3 = MoldInformation!一个$ 5中,E3/MoldInformation!$ F $ 5中,IF(A3 = MoldInformation!一个$ 6中,E3/MoldInformation!$ F $ 6中,IF(A3 = MoldInformation!甲$ 7,E3/MoldInformation!$ F $ 7,IF(A3 = MoldInformation!A $ 8,E3/MoldInformation! $ F $ 8,IF(A3 =模具信息!9澳元,E3 /模具信息!F $ 9,IF(A3 =模具信息!10美元,E3 /模具信息!F $ 10,IF(A3 =模具信息!11美元,E3 /模具信息!F $ 11,IF(A3 =模具信息!12澳元,E3 /模具信息!12美元,IF(A3 =模具信息!13美元,E3 /模具信息!F …
我可以ArrayList通过以下两种方式添加所有数组元素,
第一,
List<String> list1 = new ArrayList<String>();
list1.addAll(Arrays.asList("23,45,56,78".split(",")));
System.out.println(list1);
Run Code Online (Sandbox Code Playgroud)
第二,
List<String> list2 = new ArrayList<String>();
list2.addAll(new ArrayList<String>(Arrays.asList("23,45,56,78".split(","))));
System.out.println(list2);
Run Code Online (Sandbox Code Playgroud)
两者都很好.我的问题是这两者之间有什么区别吗?哪一个可以用于更好的练习为什么?