那么如何制作这样的逻辑呢
int[] arr = {2, 5, 3};
if (/* arr is sorted */)
....
else
...
Run Code Online (Sandbox Code Playgroud)
它的方法Array.sort是无效的
我有以下代码,我正在创建一个数组并尝试在其中存储对象.在运行时,我得到一个ArrayStoreException
.
import java.lang.reflect.Array;
public class GenericsArrayCreation<T> {
public static <T> void Test(T[] A){
@SuppressWarnings("unchecked")
T[] temp = (T[]) Array.newInstance(A.getClass(), A.length);
for(int i = 0;i<temp.length;i++){
temp[i] = A[i];
System.out.println(temp[i].toString());
}
}
public static void main(String[] args){
String[] strs = {"a", "b", "c"};
GenericsArrayCreation.Test(strs);
}
}
Run Code Online (Sandbox Code Playgroud)
我莫名其妙地明白这是因为这句话
T[] temp = (T[]) Array.newInstance(A.getClass(), A.length);
Run Code Online (Sandbox Code Playgroud)
为什么这是错的?A.getClass()
在运行时返回一个String
,所以temp
应该是一个字符串数组.在那种情况下,为什么作业temp[i] = A[i]
不起作用?
[1, 1, 1, 2, 2, 3].count(True)
>>> 3
Run Code Online (Sandbox Code Playgroud)
如果所有值的返回值不等于,为什么返回3
而不是?6
bool(i)
True
i
0
我必须使用Scanner
,所以是有一个nextChar()
,而不是nextLine()
,我可以使用的方法?
谢谢!
我试着写匹配或者正则表达式\
或/
.
不管我写的是什么顺序:
[//\]
Run Code Online (Sandbox Code Playgroud)
要么
[/\\]
Run Code Online (Sandbox Code Playgroud)
它以某种方式逃脱我的方括号或我的正斜线.显示这种特殊情况的正确方法是什么?
为什么Collections.sort()
只适用于List
s而不适用于Set
s?有什么特别的原因吗?
public class Test {
public static void main(String... args) {
int i=010;
System.out.print(i);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
8
Run Code Online (Sandbox Code Playgroud)
为什么?逻辑是什么?
能告诉我有没有办法在java或spring中用单个空格替换多个空间?任何stringUtils函数都一样吗?
喜欢
1.
test test
test test
Run Code Online (Sandbox Code Playgroud)
2.
test test
test test
Run Code Online (Sandbox Code Playgroud)
3.
test test
test test
Run Code Online (Sandbox Code Playgroud) 我执行以下代码得到了一些令人惊讶的结果
class Test {
public static void main(String[] args) {
Short i = 122, j = 122;
if (i == j) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
Run Code Online (Sandbox Code Playgroud)
和
class Test {
public static void main(String[] args) {
Short i = 1222, j = 1222;
if (i == j) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我执行这些代码时,第一个代码提供true
输出,而第二个代码提供false
输出.我知道当我们使用==
它比较对象时,它不会查找实际值,而只是比较引用.但在第一种情况下,它正在比较值,而在第二种代码中却没有.
我在java中编写以下代码并检查存储在变量中的值.当我在一个双变量'y'中存储1.2时,它变为1.200000025443.为什么不是1.2亿个?
public class DataTypes
{
static public void main(String[] args)
{
float a=1;
float b=1.2f;
float c=12e-1f;
float x=1.2f;
double y=x;
System.out.println("float a=1 shows a= "+a+"\nfloat b=1.2f shows b= "+b+"\nfloat c=12e-1f shows c= "+c+"\nfloat x=1.2f shows x= "+x+"\ndouble y=x shows y= "+y);
}
}
Run Code Online (Sandbox Code Playgroud)
你可以在这里看到输出:
float a=1 shows a= 1.0 float b=1.2f shows b= 1.2 float c=12e-1f shows c= 1.2 float x=1.2f shows x= 1.2 double y=x shows y= 1.2000000476837158