小编000*_*laH的帖子

使用不同的模板重新组合相似的类

语境

我想为我的 JavaFX 应用程序创建颜色图,以便根据其值显示具有不同颜色的网格。定义了两种类型:使用DiscreteColorMap整数ContinuousColorMap双键。两者都必须实现该接口,以便可以像这样调用它:ColorMap

ColorMap palette1 = new DiscreteColorMap();
ColorMap palette2 = new ContinuousColorMap();
Run Code Online (Sandbox Code Playgroud)

问题

由于这两个类都依赖于相同的接口,因此我指定了一个模板 ( public interface ColorMap<T>) 以适应它们中的每一个:

ColorMap<Integer> palette1 = new DiscreteColorMap();
ColorMap<Double> palette2 = new ContinuousColorMap();
Run Code Online (Sandbox Code Playgroud)

我想要最简单的颜色图语法,所以我需要去掉<Integer><Double>字符串。最优雅的方法是什么?

来源

完整的代码可以在这个GitHub 项目中找到。

编辑

我的英语并不完美^^我用了“摆脱”,但这并不清楚:当我实例化我的颜色图时,我想要制作<Integer><Double>消失,所以我可以写ColorMap palette...而不是ColorMap<Integer> palette...

java oop templates design-patterns javafx

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

如何找到给定字符串中最长的单词?

在给定的字符串中,我想找到最长的单词,然后在控制台中打印它。

我得到的输出是第二长的单词 ie "Today",但我应该得到"Happiest"
我可以知道我做错了什么吗?有没有更好/不同的方法来找到字符串中最长的单词?

public class DemoString {
    public static void main(String[] args) {
        String s = "Today is the happiest day of my life";
        String[] word = s.split(" ");
        String longword = " ";
        for (int i = 0; i < word.length; i++)
            for (int j = 1 + i; j < word.length; j++)
                if (word[i].length() >= word[j].length())
                    longword = word[i];

        System.out.println(longword + " is the longest word with " + longword.length() + …
Run Code Online (Sandbox Code Playgroud)

java string

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

JavaFX 用旧文本替换新文本,而不在场景中覆盖它

我对 Java 还很陌生,现在已经是培训的第二个月了。我希望每次按下 时New,旧文本都会被删除,然后新文本就会出现。但这不起作用。我已经在互联网上进行了研究。

当我按下 时New,新文本会覆盖旧文本。我想如果我text(null)之前做了,它就会删除它。但这不起作用。

多次按下按钮后:

图像

这是我的代码:

buttonNew.setOnAction(ae -> {
    int randomM1 = (int) (Math.random() * 11);
    int randomM2 = (int) (Math.random() * 11);
    System.out.println(randomM1 + " x " + randomM2);
    System.out.println(randomM1 * randomM2);
    
    Text mText = new Text();
    mText.setText(null);
    mText.setText(randomM1 + " x " + randomM2);
    rootM.getChildren().add(mText);
    mText.setLayoutX(300);
    mText.setLayoutY(200);
    mText.setFont(Font.font("Verdana", 50));
}); 
Run Code Online (Sandbox Code Playgroud)

java eclipse javafx

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

Java 中的 ArrayList.get() 函数需要 int 作为索引,而不是所需的 long

import java.util.ArrayList;

public class LargestPrimeFactor {
    private static long maxNum;

    public LargestPrimeFactor(long maxNum) {
        LargestPrimeFactor.maxNum = maxNum;
        ArrayList<Long> listOfPrimes = new ArrayList<>();
        listOfPrimes.add(2L);

        for (long i = 3; i < maxNum / 2; i++) {
            if (i % 2 == 1 && isPrime(i)) {
                listOfPrimes.add(i);
            }
        }

//        for (long i = 0; i < listOfPrimes.size(); i++) {
//            System.out.println(listOfPrimes.get((int) i));
//        }

        for (long i = listOfPrimes.size() -1; i >= 0; i--) {
            if (maxNum % ((long) listOfPrimes.get(i)) == …
Run Code Online (Sandbox Code Playgroud)

java

-1
推荐指数
1
解决办法
63
查看次数

标签 统计

java ×4

javafx ×2

design-patterns ×1

eclipse ×1

oop ×1

string ×1

templates ×1