小编Aru*_*ran的帖子

如何从Eclipse中删除这个不需要的栏?

在Eclipse(Indigo)中进行编码时,我不小心碰到了一个键组合,当我做一些快捷方式时,这个键就出现了.这个问题可能之前已经得到了回答,但由于我不知道酒吧谷歌搜索的确切名称,这个问题毫无结果.我花了两个小时试图解决它.所以任何人都知道如何摆脱下图中的这个栏?

问题

eclipse breadcrumbs

137
推荐指数
4
解决办法
3万
查看次数

非空字符串中的空字符串

我对代码感到困惑

public class StringReplaceWithEmptyString 
{
    public static void main(String[] args) 
    {
        String s1 = "asdfgh";
        System.out.println(s1);
        s1 = s1.replace("", "1");
        System.out.println(s1); 
    }
}
Run Code Online (Sandbox Code Playgroud)

输出是:

asdfgh
1a1s1d1f1g1h1
Run Code Online (Sandbox Code Playgroud)

所以我的第一个意见是String中的每个字符都在""两边都有一个空字符串.但是,如果是'a'(在字符串中)之后就应该有两个'1'输入(第一行为'a',第二行为's').

现在我检查了String是否在这些链接中表示为char [] 在Java中,是一个字符串数组?在Java中字符串表示我得到的答案是YES.

于是,我就一个空字符分配''给一个char变量,但它给我一个编译器错误,

字符常量无效

我尝试时,同样的过程给出了编译器错误 char[]

char[] c = {'','a','','s'};  // CTE
Run Code Online (Sandbox Code Playgroud)

所以我对三件事感到困惑.

  1. char []如何表示空字符串?
  2. 为什么我得到上述代码的输出?
  3. 首次初始化时,字符串s1如何在char []中表示?

对不起,如果我在问题的任何部分出错了.

java arrays string char

13
推荐指数
2
解决办法
853
查看次数

使用DecimalFormat格式化抛出异常 - "无法将给定对象格式化为数字"

这可能看起来像一个重复的问题,但我尝试了以下所有链接,无法得到正确的答案.

无法将给定对象格式化为数字组合框

非法论证例外

但我没有弄错.这是我的代码

DecimalFormat twoDForm = new DecimalFormat("#.##");
double externalmark = 1.86;
double internalmark = 4.0;
System.out.println(String.valueOf((externalmark*3+internalmark*1)/4));
String val = String.valueOf((externalmark*3+internalmark*1)/4);
String wgpa1=twoDForm.format(val); // gives exception
String wgpa2=twoDForm.format((externalmark*3+internalmark*1)/4)); // works fine
System.out.println(wgpa1);
Run Code Online (Sandbox Code Playgroud)

format方法采用Object类型参数,这就是我传递给出异常的String对象的原因

线程"main"中的异常java.lang.IllegalArgumentException:无法 将给定的Object格式化为数字.

但是当我将double值作为参数时,程序运行正常.但是如果方法是用Object类型参数定义的,为什么我在传递时传递一个异常并且在传递时String没有获得异常double

java overloading exception illegalargumentexception decimalformat

9
推荐指数
1
解决办法
2万
查看次数

Java 21 DateFormat.getDateTimeInstance().format(new Date()) 问题

这是我的代码

import java.util.Date;
import java.text.DateFormat;

class DateTime {
    public static void main(String[] args) {
        String dt = DateFormat.getDateTimeInstance().format(new Date());
        System.out.println(dt);
    }
}
Run Code Online (Sandbox Code Playgroud)

当使用 Java 21 编译和执行时,对“format()”的调用返回一个UTF-16包含无效字节的字符串,用问号表示:

2023 年 10 月 3 日 7:01:17?PM

还有其他人看到这个问题吗?谢谢。

java formatting datetime java-21

9
推荐指数
2
解决办法
939
查看次数

为什么在return语句之后,当允许其他语句时,不允许使用额外的分号?

我在分号后添加了一个额外的分号System.out.println:

System.out.println();;
Run Code Online (Sandbox Code Playgroud)

这对Java编译器来说是合法的,所以我检查了其他语句,它们都是合法的.所以当我搜索并找到这些链接时:

  1. 为什么Java在语句结尾处不显示双分号错误?
  2. 当我用两个分号结束一行时,编译器不会抱怨.为什么?
  3. 什么时候你会在一个方法关闭括号后放一个分号?
  4. 为什么连续分号的代码会编译?
  5. 分号在'if'语句结束时

我开始明白额外的分号意味着额外的空话.

但是当我在return声明后添加一个额外的分号时,我得到了编译时错误.我得出的结论是,该return声明被认为是执行流程中的最后一个声明,因此在声明之后添加一个额外的声明return是非法的.

这个代码也发生了同样的事情:

if(a == b)
    System.out.println();;
else
    System.out.println();
Run Code Online (Sandbox Code Playgroud)

if语句内部System.out.println();;给出了编译时错误,因为编译器期望elseifelse.我是对的还是还有其他原因吗?

java compiler-errors return

7
推荐指数
1
解决办法
1101
查看次数

在引用变量上调用方法vs在新对象上调用方法

我在调用非静态方法时感到困惑

class A {
    void doThis() {}

    public static void main(String... arg) {
        A a1 = new A();
        a1.doThis();        // method - 1
        new A().doThis();   // method - 2
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道方法1方法2都会调用doThis(),但有没有任何功能差异?

java instance-variables instance

6
推荐指数
1
解决办法
1158
查看次数

JPA findBy 方法总是转到 orElseThrow

这是我们的代码

private IdentificationMaster validateIdentificationType(String idType) {
    if(!StringUtils.isNotBlank(idType))
        throw new IllegalArgumentException("Invalid idType");

    Optional<IdentificationMaster> op1 = specRepo.findById(idType); //testing purpose

    Optional<IdentificationMaster> op2 = specRepo.findByIdentificationType(idType); //testing purpose

    return specRepo.findById(idType)
            .orElse(specRepo.findByIdentificationType(idType)
                    .orElseThrow(() -> new ResourceNotFoundException("Id Type Not Found " + idType)));
}
Run Code Online (Sandbox Code Playgroud)

因为idType我们期待两个值,它可以是主键 id或其对应的identificationType. 表只有两列ididentificationType。问题是ResourceNotFoundException即使op1op2不为空它也会抛出。现在如果我像这样改变我的回报

return specRepo.findByIdentificationType(idType)
            .orElse(specRepo.findById(idType)
                .orElseThrow(() -> new ResourceNotFoundException("Id Type Not Found " + idType)));
Run Code Online (Sandbox Code Playgroud)

它再次抛出相同的异常!

存储库

@Repository
public interface IdentificationSpecRepository extends CrudRepository<IdentificationMaster, String>{

    Optional<IdentificationMaster> findByIdentificationType(String …
Run Code Online (Sandbox Code Playgroud)

java optional findby spring-data-jpa spring-boot

6
推荐指数
1
解决办法
862
查看次数

持续时间 - 当秒为负数时为什么要进行额外的工作?

toNanos这是类中的方法Duration

public long toNanos() {
    long tempSeconds = seconds;
    long tempNanos = nanos;
    // TODO: Here makes me confused
    if (tempSeconds < 0) {
        // change the seconds and nano value to
        // handle Long.MIN_VALUE case
        tempSeconds = tempSeconds + 1;
        tempNanos = tempNanos - NANOS_PER_SECOND;
    }
    long totalNanos = Math.multiplyExact(tempSeconds, NANOS_PER_SECOND);
    totalNanos = Math.addExact(totalNanos, tempNanos);
    return totalNanos;
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么当秒为负数时需要做额外的工作。

正数的 max 为2^63-1,负数的 min 为2^63,看起来是转移-2^63s,-1ns-2^63+1s,1000_000_000-1ns,但最终还是要参与计算。在我看来这是没有意义的,因为当数字溢出时Math.multiplyExactMath.addExact抛出异常,无论判断是否存在,它都不会改变

java duration

6
推荐指数
1
解决办法
99
查看次数

如果没有多线程,使用Future有什么意义呢?

我继承了一些代码,原来的开发人员已经没有人留下了。该代码大量使用了CompletableFuture,这是我第一次使用它,所以我仍在尝试理解它。据我了解, a(Completable)Future通常与某种多线程机制一起使用,该机制允许我们在执行耗时的任务时执行其他操作,然后只需通过 Future 获取其结果。正如javadoc中所示:

interface ArchiveSearcher { String search(String target); }
class App {
    ExecutorService executor = ...
    ArchiveSearcher searcher = ...
    void showSearch(final String target) throws InterruptedException {
        Future<String> future = executor.submit(new Callable<String>() {
        public String call() {
            return searcher.search(target);
        }});
        displayOtherThings(); // do other things while searching
        try {
            displayText(future.get()); // use future
        } catch (ExecutionException ex) { cleanup(); return; }
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,在我继承的这个应用程序中,以下不使用任何多线程的模式多次出现:

public Object serve(Object input) throws ExecutionException, InterruptedException { …
Run Code Online (Sandbox Code Playgroud)

java multithreading asynchronous future completable-future

5
推荐指数
1
解决办法
1839
查看次数

检查数字是否是两个素数的总和

问题是检查随机数n可以是2个随机素数的总和.例如,

如果n = 34,则可能是(3 + 31),(5 + 29),(17 + 17)......

到目前为止,我已设法将素数保存到数组中,但我不知道如何检查,如果n是2个素数的总和.

这是我的代码的一部分:

public static void primeNumbers(int n) {
    int i = 0, candidate = 2, countArray = 0, countPrime = 0;
    boolean flag = true;

    while (candidate <= n) {
        flag = true;
        for (i = 2; i < candidate; i++) {
            if ((candidate % i) == 0) {
                flag = false;
                break;
            }
        }
        if (flag) {
            countPrime++;
        }
        candidate++;
    }
    int[] primeNumbers = new …
Run Code Online (Sandbox Code Playgroud)

java arrays primes

5
推荐指数
1
解决办法
948
查看次数