我想从字符串中删除最后一个字符.我试过这样做:
public String method(String str) {
if (str.charAt(str.length()-1)=='x'){
str = str.replace(str.substring(str.length()-1), "");
return str;
} else{
return str;
}
}
Run Code Online (Sandbox Code Playgroud)
获取字符串的长度 - 1并替换最后一个字母(删除它),但每次运行程序时,它都会删除与最后一个字母相同的中间字母.
例如,这个词是"崇拜者"; 在我运行该方法后,我得到了"赞赏".我想让它返回admire这个词.
字符数组的默认值是给出一个空指针异常...可以告诉为什么这个异常仅适用于字符数组...即使其他dataype数组的默认值为null.例如
class Test{
char[] x;
public static void main(String[] args) {
Test t=new Test();
System.out.println(t.x);
}
}
Run Code Online (Sandbox Code Playgroud)
抛出空指针异常
class Test{
int[] x;
public static void main(String[] args) {
Test t=new Test();
System.out.println(t.x);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:空
我有对象列表说需要转换为地图的汽车.
Public Class Car {
private Integer carId;
private Integer companyId;
private Boolean isConvertible;
private String carName;
private String color;
private BigDecimal wheelBase;
private BigDecimal clearance;
}
Run Code Online (Sandbox Code Playgroud)
我有另一个对象,我想把它当作Map的关键.
public class Key<L, C, R> {
private L left;
private C center;
private R right;
}
Run Code Online (Sandbox Code Playgroud)
我想从List of Car对象创建一个地图.
List<Car> cars;
Map<Key, Car> -> This map contains Key object created from 3 field of Car object namely carId, companyId, isConvertible.
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何使用Java 8 Lambda执行此操作
cars.stream.collect(Collectors.toMap(?, (c) -> c);
Run Code Online (Sandbox Code Playgroud)
在上面的语句中,代替?,我想使用当前car对象中存在的值创建Key类的对象.我怎样才能做到这一点?
我想使用反射API调用Java 8接口的静态方法.
public interface TimeClient {
static void testStatic() {
System.out.println("In the Static");
}
}
Run Code Online (Sandbox Code Playgroud)
我能够调用Interface的默认方法但无法调用静态方法.
我发现在HashMap我们的内部类中Holder有以下描述:
/**
* holds values which can't be initialized until after VM is booted.
*/
Run Code Online (Sandbox Code Playgroud)
我们如何以及何时使用该课程?它的用途是什么?请解释.
对于ArrayList,我们有:
public boolean add(E e) {
Run Code Online (Sandbox Code Playgroud)
但要删除我们有:
public boolean remove(Object o) {
Run Code Online (Sandbox Code Playgroud)
为什么不呢?:
public boolean remove(E e) {
Run Code Online (Sandbox Code Playgroud) 有没有办法禁用这个jconsole弹出窗口?
我知道我可以根据http://docs.oracle.com/javase/7/docs/technotes/guides/management/agent.html#gdemv设置SSL ,但这意味着我需要在所有服务器上执行此操作(java)和所有客户端(jconsole).对于非关键的内部应用程序而言,这不是最友好的解决方案.还有其他方法吗?
我是出于好奇而不是必然而已.我想答案就像是"因为这就是Java的构建方式".我想知道为什么选择这种方式.
例如,如果我的超类有这个方法:
public void foo()
{
System.out.println("We're in the superclass.");
}
Run Code Online (Sandbox Code Playgroud)
并且子类重写该方法:
@Override
public void foo()
{
super.foo();
System.out.println("We're in the subclass.");
}
Run Code Online (Sandbox Code Playgroud)
为什么需要super.foo()在子类的方法的顶部(如果它将被使用)?为什么我不能交换这两行来使它看起来像这样:
@Override
public void foo()
{
System.out.println("We're in the subclass.");
super.foo();
}
Run Code Online (Sandbox Code Playgroud) 为什么在转换为小写字母后s打印字符串"Hello, World"?
public class Practice {
public static void main(String[] args){
String s = "Hello, World";
s.toLowerCase();
System.out.println(s);
}
}
Run Code Online (Sandbox Code Playgroud) 我在不同版本的Java中运行了以下程序.
final double[] values = new double[10000];
final long start = System.currentTimeMillis();
double sum = 0;
for (int i = 0; i < values.length; i++)
sum += Math.pow(values[i], 2);
final long elapsed = System.currentTimeMillis() - start;
System.out.println("Time elapse :: " + elapsed);
Run Code Online (Sandbox Code Playgroud)
Java 7:输出
时间流逝:: 1
Java 8:输出
时间流逝:: 7
为什么Java 8中的性能问题与7相比?
java ×9
java-8 ×3
string ×2
arraylist ×1
arrays ×1
collections ×1
dictionary ×1
hashmap ×1
inheritance ×1
jconsole ×1
lambda ×1
list ×1
math ×1
performance ×1
reflection ×1