我一直试图用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) 我在 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 中表示它?
为什么我们;在它之后使用带分号()的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)
那么这里做什么呢?
我有主要课程
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) 如何将Mon Jul 01 08:00:00 IST 2019字符串类型的日期“ ” 转换为ISODate“ 2019-07-01T03:00:00.000Z”?
我有 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 初学者,任何帮助将不胜感激。
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) 我无法在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.有人可以帮我理解更多吗?
我一直试图弄清楚这个问题的答案有什么问题.真的需要帮助!任何帮助表示赞赏!谢谢
题
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)我有一个数组,我正在尝试对数组的一部分的值进行求和。
例如我的数组是 [1,2,3,4,5,6],但我只想将值添加到第三个元素
[1,2,3]。我以为我以前使用过涉及分号的方法,但我记不清了。
像 int 之类的东西sum = Sum (arr[0]:arr[2]),但我不确定。这样的事情存在吗?
我想几年前我在Python中使用过它。
java ×8
arrays ×2
python ×2
bubble-sort ×1
checkstyle ×1
datetime ×1
dictionary ×1
for-loop ×1
jodatime ×1
keyerror ×1
list ×1
maven ×1
sorting ×1
spring ×1
time ×1
while-loop ×1