[英]I don't understand what the multiplication in q.offer(x*m + y) does and what the while loop is doing and everything in the code
[英]what does while (n— != 0){} is doing in this code?
在以下代碼中,此行的含義是什么?
while (n-- != 0){}
嗎? 如果當我從子字符串的長度中減去searchMe
長度時,我不認為n為18,那么這行是否表示18減量(17)不等於0,那么搜索呢?
class ContinueWithLabelDemo {
public static void main(String[] args) {
String searchMe = "Look for a substring in me";
String substring = "sub";
boolean foundIt = false;
int max = searchMe.length() -
substring.length();
test:
for (int i = 0; i <= max; i++) {
int n = substring.length();
int j = i;
int k = 0;
while (n-- != 0) {
if (searchMe.charAt(j++) != substring.charAt(k++)) {
continue test;
}
}
foundIt = true;
break test;
}
System.out.println(foundIt ? "Found it" : "Didn't find it");
}
}
條件n-- != 0
表示“減少n
之前先檢查n
不等於零;檢查之后將n-1
設置為n-1
”。
這意味着當n
在循環之前等於某個數字K
,循環將精確地重復K
次。
while (n-- != 0) { // checks (n != 0) then n = n-1;
if (searchMe.charAt(j++) != substring.charAt(k++)) {
continue test;
}
}
此n--
將進行遞減n--
,首先將檢查條件,然后將n的值減小1。
假設n的值為10,則首先將檢查條件,
if(10 != 0)
之后,它將減少到9。
在您的代碼中,您編寫了:
while (n-- != 0) {
if (searchMe.charAt(j++) != substring.charAt(k++)) {
continue test;
}
}
因此,如果將代碼替換為以下代碼:
while (n != 0) {
n = n-1;
if (searchMe.charAt(j++) != substring.charAt(k++)) {
continue test;
}
}
這兩個代碼可以完全相同地工作,所以我認為通過比較您可以理解它的含義。
n--實際上是一種在后台將n的值更改為n-1的方法,但是當前返回n的實際值,因為在這種情況下它是后綴操作。
如果它是前綴操作(--n),則返回的值將如您所說首先為17。
基本上,n--表示取n的當前值並將其用於此行,但是在此當前行之后,將值減一。 在這種情況下,n的第一個值將是字符串的長度
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.