小编azr*_*zro的帖子

冒泡排序没有正确排序 - Java

我一直试图用Java中的一个简单的冒泡排序方法来工作,我看不出它为什么不起作用的问题.我希望数组中的最低元素是第一个,最后一个是最高元素.在这里,我给已经排序的数组赋值方法[1, 2, 3, 4].

输出是一个数组[1, 3, 2, 4]- 所以它排序的东西,虽然它不应该.有人看到了这个问题吗?

import java.util.Arrays;

public class BubbleSort {
    public static int [] bubblesortMethode(int sortMe[])
    {
        int nrOfSwaps = 0;

        for (int i = 0; i < sortMe.length - 1; i++)  {
            for (int j = 1; j < sortMe.length; j++) {
                if(sortMe[i] > sortMe[j]){
                    int temp  = sortMe[j];
                    sortMe[j] = sortMe[i];
                    sortMe[i] = temp;
                }
            }
            nrOfSwaps++;
        }
        System.out.println("Number of swaps" + " " + nrOfSwaps);
        return sortMe;
    } …
Run Code Online (Sandbox Code Playgroud)

java arrays sorting for-loop bubble-sort

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

用于自动格式化代码以匹配 google checkstyle 的 Maven 插件?

我在 Intellij 中使用 google checkstyle:https : //github.com/google/styleguide

当我在 IntelliJ 中进行任何编码期间进行格式化时,我运行了这个 eclipse checkstyle,它似乎工作正常。我想要一个 Maven 阶段,每当我运行mvn clean install.

我正在使用以下插件:

<plugin>
    <groupId>net.revelc.code.formatter</groupId>
    <artifactId>formatter-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>format</goal>
            </goals>
            <configuration>
                <configFile>src/main/resources/eclipse-java-google-style.xml</configFile>
                <encoding>UTF-8</encoding>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

不幸的是,它似乎没有正确注册该配置文件。我想知道获取正确的 checkstyle 配置的最佳经验法则是什么。我把它放在哪里以及如何在我的 POM 中表示它?

java checkstyle maven

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

while循环用分号java

为什么我们;在它之后使用带分号()的while循环?

我以此代码为例:

public static void Register()
{String name,code;
int posStudent,posCourse;
System.out.println("-----------------------------------------------");
System.out.println("Enter the name of the student");
name=in.next();

while ((posStudent=VerifyTheStudentName(name))==-1);

System.out.println("-----------------------------------------------");
System.out.println("The list of available courses is:");
for(int i=0;i<courses.size();i++)
    System.out.println(courses.get(i));
System.out.println("-----------------------------------------------");
System.out.println("Enter the course code");
code=in.next();
while((posCourse=VerifyTheCourseCode(code))==-1);
students.get(posStudent).registerTo(courses.get(posCourse));
}
Run Code Online (Sandbox Code Playgroud)

那么这里做什么呢?

java while-loop

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

如何查找我的对象是否扩展了给定的抽象类?

我有主要课程

public class Main(){
   public static void main(String[] args){
      ArrayList<A> as = new ArrayList<>();
      C c = new C();
      D d = new D();
      E e = new E();

      as.add(c);
      as.add(d);
      as.add(e);

      for(A a : as){
         a.print(); // want to do it conditionally when Object inside list have extended class B
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

方法print()从抽象类A扩展.类C和D扩展类B和B扩展A.

    A
  /   \
  B    E
 / \
C   D
Run Code Online (Sandbox Code Playgroud)

我该怎么做这种方法?

尝试:

for(A a : as){
   if(a.getClass().getSuperclass().getSimpleName().equals("B")){
      a.print(); // Doesn't work a.getClass().getSuperclass().getSimpleName() prints A
   }
} …
Run Code Online (Sandbox Code Playgroud)

java

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

在Java中将字符串日期转换为ISO日期格式“ 2019-07-01T03:50:00.000Z”

如何将Mon Jul 01 08:00:00 IST 2019字符串类型的日期“ ” 转换为ISODate“ 2019-07-01T03:00:00.000Z”?

java time spring datetime jodatime

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

如何在python中的两个列表之间找到不同字符的数量?

我有 2 个列表:

A:[2 1 2 6 6 7 1 7 5 9 2 6 6 6 6 6 1 0 5 8 8 7 8 1 7 5 4 9 2 9 4 7 6 8 9 4 3]

B:[2 8 2 6 6 7 1 9 8 5 2 2 6 6 6 6 1 0 5 2 8 7 3 4 7 5 4 9 2 9 4 7 6 8 9 4 3]
Run Code Online (Sandbox Code Playgroud)

我想找到相同字符的数量并在 PYTHON 中打印出来。我怎样才能做到这一点?它还应该测试该字符是否与另一个列表的位置相同。我是 …

python

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

字典列表在 Python 中引发了一个关键错误

我有一个字典列表,我想分别计算“增加”和“减少”键的值。我的脚本返回了一个关键错误,这可能是因为并非所有词典都有“增加”和“减少”。但我不知道如何解决它。作为 Python 初学者,任何帮助将不胜感激。

list_of_dicts = [{"decreasing": 1}, {"increasing": 4}, {"decreasing": 1}, {"increasing": 3},{"decreasing": 1},
             {"increasing": 1}]

values1 = [a_dict["decreasing"] for a_dict in list_of_dicts]
values2 = [a_dict["increasing"] for a_dict in list_of_dicts]

print(values1)
print(values2)
Run Code Online (Sandbox Code Playgroud)

预期的结果是:

[1,1,1]
[4,3,1]
Run Code Online (Sandbox Code Playgroud)

python dictionary list keyerror

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

无法在java中创建内部类对象

我无法在java中创建内部类对象:

package OOO;

class Car{
    class Engine{

        void display() {
            System.out.println("this is inner diaplay() method");
        }

    }
}

public class Sample8InnerClassCar {
    Car c = new Car();
    Car.Engine e = c.new Car.Engine();
}
Run Code Online (Sandbox Code Playgroud)

它给我一个错误:无法分配成员类型Car.Engine.有人可以帮我理解更多吗?

java

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

JAVA - For循环和If else语句

我一直试图弄清楚这个问题的答案有什么问题.真的需要帮助!任何帮助表示赞赏!谢谢

  • 你可以输入两个整数(a和b),和
  • 如果,程序将从数字a打印到数字b a<b.
  • 如果a>b,程序将从b打印到a.
  • 如果b is the same as a,程序将要求用户输入另一个数字b,直到它不等于a.

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a: ");
    int a = sc.nextInt();
    System.out.println("Enter b: ");
    int b = sc.nextInt();
    
    if(a > b) {
        for(int i = b; b >= a; b--) {
            System.out.println(b);
        }
    } else if (a < b) {
        for(int i = a; a <= b; a++) {
            System.out.println(i);
        }
    } else {
        System.out.println("Enter another number b: ");
        int numberb = …
    Run Code Online (Sandbox Code Playgroud)

java

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

有没有办法使用 : 在数组中添加一系列值?

我有一个数组,我正在尝试对数组的一部分的值进行求和。

例如我的数组是 [1,2,3,4,5,6],但我只想将值添加到第三个元素

[1,2,3]。我以为我以前使用过涉及分号的方法,但我记不清了。

像 int 之类的东西sum = Sum (arr[0]:arr[2]),但我不确定。这样的事情存在吗?

我想几年前我在Python中使用过它。

java arrays data-structures

0
推荐指数
2
解决办法
153
查看次数