static void eldontes() {
System.out.println("--- Choosing ---");
int[] sorozat = new int[]{1, -1, 3, 5};
boolean exists = false;
for (int i = 0; i < sorozat.length && !exists; i++) {
int elem = sorozat[i];
if (elem < 0) {
exists = true;
}
}
System.out.println("There's negative: " + exists);
首先,for循環在做什麼? !存在應該意味着布爾值現在是真實的,不是嗎?所以增加我utnil sorozat.length和存在=真?這是什麼意思?基本的java代碼問題。這些exmaples如何工作?
那麼這裏的這個例子:從int tmp = sorozat[j];
開始我真的不知道發生了什麼
static void buborekRendezes() {
System.out.println("--- Bubble order ---");
int[] sorozat = new int[]{9, 4, 6, 2, 3, 0, 5, 7, 8, 1};
for (int i = sorozat.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (sorozat[j] > sorozat[j + 1]) {
int tmp = sorozat[j];
sorozat[j] = sorozat[j + 1];
sorozat[j + 1] = tmp;
}
}
}
System.out.println(Arrays.toString(sorozat));
}
。最後三行是做什麼的?爲什麼代碼末尾的tmp在右側?
對不起,我知道這些是真正的基本問題,我是Java新手。
感謝您的快速解答! :) – JustMatthew