小编ole*_*nik的帖子

是否可以在 Java 中创建一个包含另一个非枚举类的对象的枚举类?

例如,我有Subscriber类及其对象subscriber1, subscriber2, subscriber3。是否可以创建一个enum包含这些对象的对象?我正在尝试这样做

enum Subscribers{
    subscriber1,subscriber2,subscriber3;
}
Run Code Online (Sandbox Code Playgroud)

但我得到的是 strings subscriber1, subscriber2,subscriber3相反。如果有任何反馈,我将不胜感激

java enums

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

排列数组,使相邻的空间较小,从而给出最小的总和

我有一个大小均匀的数字数组,这是我的任务:

a) 丢弃数组中的任意 2 个元素。b) 然后将元素配对并计算该对中元素之间的差值之和,使之和最小。

例子:

array size even say 8.
array elements : 1,3,4,6,3,4,100,200

Ans:
5
Run Code Online (Sandbox Code Playgroud)

解释:

在这里,我将删除 100 和 200,因为将它们配对后,差值为 (200 - 100) = 100。因此剩余元素为 [1,3,4,6,3,4] 具有最小总和的对为:(1 3 ) , (4 3), (6 4)。=|3-1| = 2, |4-3|=1,|6-4| = 2. 所以总和 = 2 + 1 + 2 = 5

例子:

array size even say 4.
array elements : 1,50,51,60

Ans:
1
Run Code Online (Sandbox Code Playgroud)

解释:这里我将删除 1 和 60,这样我将得到最小的总和。所以剩下的元素是 [50, 51],与相邻的 [50 51] = 1 相同。在这种情况下,我的代码将失败并返回 49。

在java中如何实现这一点呢?

我尝试像这样对元素进行排序,但这并不是适用于所有类型输入的正确方法。

public static …
Run Code Online (Sandbox Code Playgroud)

java algorithm

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

如何在mavenrepository中向artefact添加类别和标签

有谁可以解释如何更新我的工件的 Maven 存储库存储库中的这些值:

  • 类别
  • 标签

Maven 工件描述

maven maven-central maven-repository

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

在一个字符串中搜索多个字符串的最快方法

下面是我的代码,用于查找给定单个字符串中所有子字符串的出现次数

public static void main(String... args) {
    String fullString = "one is a good one. two is ok. three is three. four is four. five is not four";
    String[] severalStringArray = { "one", "two", "three", "four" };
    Map<String, Integer> countMap = countWords(fullString, severalStringArray);
}

public static Map<String, Integer> countWords(String fullString, String[] severalStringArray) {
    Map<String, Integer> countMap = new HashMap<>();

    for (String searchString : severalStringArray) {
        if (countMap.containsKey(searchString)) {
            int searchCount = countMatchesInString(fullString, searchString);
            countMap.put(searchString, countMap.get(searchString) + searchCount);
        } else
            countMap.put(searchString, …
Run Code Online (Sandbox Code Playgroud)

java

5
推荐指数
0
解决办法
98
查看次数

如何在 Java 中使用 super.toString() 将 toString 从子级传递到父级

从事一个Java项目来研究多态性。我正在尝试学习如何从底部子级向上传递 toString() 。我必须将 Hardware.java 中的 toString 传递到 Tool.java 到 ScrewDriver.java,然后在 Player.java 中,我需要打印出我定义的数组。

这是文件:

public class Player {
    public static void main(String[] args) {
        List<Hardware> hardware = new ArrayList<>();
        ScrewDriver screwdriver1 = new ScrewDriver("Flathead", "Use this to open paint cans", 150, "Woodworking", true, 15, 10);

        hardware.add(screwdriver1);

        for(Hardware tool : hardware)
            System.out.print(tool);
    }
}

public class Hardware {
    private String name;
    private String message;
    private int points;

    @Override
    public String toString() {
        super.toString();
        return "Name: " + name + " Message: " …
Run Code Online (Sandbox Code Playgroud)

java arrays arraylist object tostring

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

使用java查找数组中的第二大值?

我的问题是我需要从我的数组中找到第二个最大值,但我得到的值与第一个值相同。请帮忙

int[] nums = { 50, 6, 60, 70, 80, 90, 9, 150, 2, 35 };
int max = 0;
int secmax = 0;

for (int x = 0; x < nums.length; x++) {
    if (nums[x] > max)
        max = nums[x];
    if (nums[x] > secmax && secmax != max)
        secmax = nums[x];
}

System.out.println("1st H value: " + max);
System.out.println("2nd H Value: " + secmax);
Run Code Online (Sandbox Code Playgroud)

java

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

为什么我找不到迭代器的值?

为什么这段代码不打印带有 a 的值"q"

Iterator<String> it = ZoneId.getAvailableZoneIds().iterator();

while (it.hasNext()) {
    if (it.next().contains("q"))
         System.out.println(it.next());
}
Run Code Online (Sandbox Code Playgroud)

输出:

Pacific/Kwajalein
Etc/GMT-3
America/Martinique
Pacific/Funafuti
Canada/Pacific
Eire
America/Sitka
Brazil/West
US/Aleutian
Run Code Online (Sandbox Code Playgroud)

java

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

在 Java 中对列表进行排序后,如何访问第一个元素?

我创建了一个类 Car,具有这些属性和方法。我使用了一个列表来存储多个对象。

package com.company;
import java.util.ArrayList;
import java.util.Collections;

class Car implements Comparable<Car> {
    private String manufacturer;
    private int speed;
    private double price;
    private int year; // manufacture year

    public Car(String manufacturer, int speed, double price, int year) {
        this.manufacturer = manufacturer;
        this.speed = speed;
        this.price = price;
        this.year = year;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public int getSpeed() {
        return speed;
    }

    public double getPrice() {
        return price;
    }

    public int getYear() {
        return year;
    }

    public …
Run Code Online (Sandbox Code Playgroud)

java

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