简体   繁体   English

需要帮助来理解本课程的各个部分

[英]Need help understanding parts of this class

I need someone to elaborate on certain parts of this code. 我需要有人详细说明此代码的某些部分。

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");
}
}

more specifically, I don't understand this part: 更具体地说,我不了解这一部分:

int n = substring.length();
        int j = i;
        int k = 0;
        while (n-- != 0) {
            if (searchMe.charAt(j++) != substring.charAt(k++)) {
                continue test;
            }
        }

Why is it necessary to declare j and k in this code at all? 为什么必须在此代码中完全声明j和k? I know that there is a reason in it for the if statement 我知道if语句有其原因

if (searchMe.charAt(j++) != substring.charAt(k++))

but I don't understand what the code is actually doing at this part. 但是我不明白这段代码的实际作用。

Also, what does 还有,什么

while (n-- != 0)

mean? 意思?

while (n-- != 0)

This is just looping around, reducing n by 1 each time around the loop and ending when n (before) reducing it by 1 is not 0. 这只是循环,每次循环将n减少1,并在n(之前)减少1不为0时结束。

int n = substring.length();
int j = i;
int k = 0;
while (n-- != 0) {
    if (searchMe.charAt(j++) != substring.charAt(k++)) {
        continue test;
    }
}

This code is starting with j and k at different positions in the string, and then it is looping through comparing the character in the String at that position. 该代码从字符串中不同位置的j和k开始,然后循环比较字符串中该位置的字符。 j++ just says "use the current value of j, and then afterwards add 1 to it". j ++只是说“使用j的当前值,然后将其加1”。

Starting with an n >= 0 (string length can not be negative): n >= 0 (字符串长度不能为负)开头:

while (n-- != 0) {
    ... 
}

is just a substitute for 只是替代

for (int t = n ; t > 0; t--) {
    .... // use t instead of n, no change in your example
}

it iterates just n times (length of substring). 它仅迭代n次(子字符串的长度)。

++ and -- are pre or post incrementor and decrementor in java. ++--++中的前置或后置增量器和减量器。

Imagine the following code to understand the behaviour: 想象下面的代码来理解行为:

int a = 42
System.out.println(a++) // prints 42
System.out.println(a) // prints 43
System.out.println(++a) // prints 44
System.out.println(a) // prints 44

In fact, it adds or subtracts 1 before or after the statement is processed. 实际上,它在处理语句之前或之后加或减1。

So in your situations, while (n-- != 0) means, that the condition is checked, wether n is not zero and after that decremented by 1. 因此,在您的情况下, while (n-- != 0)表示已检查条件,但n是否不为零,然后将其递减1。

To achieve the same, you could also write: 为了达到同样的目的,您还可以编写:

while (n != 0) {
    if (searchMe.charAt(j++) != substring.charAt(k++)) {
        continue test;
    }
    n = n - 1 // or n-- or n -= 1
}

Your second condition if (searchMe.charAt(j++) != substring.charAt(k++)) compares the character at the index j in searchMe against the character with the index k from substring and after that increments both indices to avoid two more lines, where these two are incremented. 您的第二个条件是, if (searchMe.charAt(j++) != substring.charAt(k++))searchMe中索引j的字符与substring索引k的字符进行比较,然后将两个索引加1,以避免再出现两行,这两个递增。

This can be broken down in to the following: 这可以细分为以下内容:

outer: loop(/* for each character in searchMe to max */) {

    loop(/* for each character in substring
          * starting with current index in outer loop */) {

        if(/* this substring does not match */)
           continue /* with next outer loop iteration */;
    }

    /* if inner loop completed
     * the substring did match at this index so break */;
 }

Out of all the variables, n is actually the one that's not needed. 在所有变量中, n实际上是不需要的变量。 I don't know why it's there except to try to confound. 除了尝试混淆之外,我不知道为什么会在那里。 The while loop could just as easily read while(k < substring.length()) . while循环可以很容易地读取while(k < substring.length())

This kind of loop is necessary because to search for a substring you have to search starting at every character: 这种循环是必需的,因为要搜索子字符串,您必须从每个字符开始搜索:

Loo
ook
ok 
k f
 fo
for
or 
r a
...
a s
 su
sub <- found it

Another way to express the same logic is the following but this creates a new object on each iteration: 下面是表达相同逻辑的另一种方法,但这会在每次迭代中创建一个新对象:

int max = searchMe.length() - substring.length();

for(int i = 0; i <= max; i++) {
    String toTest = searchMe.substring(i, i + substring.length());

    if(toTest.equals(substring)) {
        foundIt = true;
        break;
    }
}

Well, this is some interesting code. 好吧,这是一些有趣的代码。 To start off, the label on your break is unnecessary. 首先,您不需要break的标签。

Next, to your main questions: 接下来,您的主要问题:

  • n-- is a postfix decrement operator - the current value of n is evaluated, then it is decremented by 1. When n is evaluated next, it will have the value of n-1 . n--是一个后缀递减运算符-评估n的当前值,然后将其递减1。当接下来评估n ,它将具有n-1的值。

    In the context of the code while(n-- != 0) implies that, when n == 1 , this loop will still execute, but the next time we see n , we will be viewing 0 instead. 在代码上下文中, while(n-- != 0)表示,当n == 1 ,此循环仍将执行,但是下次我们看到n ,将改为查看0

  • The statement: 该声明:

     if (searchMe.charAt(j++) != substring.charAt(k++)) { continue test; } 

    ...indicates that, if the value in the position of the main search string doesn't match up with the value we're looking for, we need to immediately jump to the label test . ...表示,如果主搜索字符串位置的值与我们要查找的值不匹配,我们需要立即跳转到标签test This allows you to skip the execution of the following two statements after it, and continue looping and looking for positive matches. 这使您可以跳过后面的以下两个语句的执行,并继续循环并查找肯定的匹配项。

    j is constantly set to what i is, but it is not always equal to i . j始终设置为i ,但并不总是等于i If a partial positive match is found, then j will increment faster than i by virtue of the while loop around that statement. 如果找到部分正匹配,则j会由于围绕该语句的while循环whilei快增长。

 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(); //the length of what we're going to search through, we //subtract the substring.Length because if the 3rd to //last character of searchMe is not equal to the first //character of substring then searchMe can not contain substring. test: for (int i = 0; i <= max; i++) { //repeat until we pass the 3rd to last character of searchMe int n = substring.length(); //resets n to the length of the substring int j = i; //resets j to equal i so we search for the next letter in searchMe int k = 0; //resets k to equal 0 so we search for the first letter in substring while (n-- != 0) { //repeat this loop until n = 0, each pass will subtract 1 from //n. this will loop a maximum of 3 times (the number of characters in substring) if (searchMe.charAt(j++) != substring.charAt(k++)) { //the if statement above will add 1 to j and add 1 to k after it checks to make sure the //characters are not equal to each other. if they are equal then it will take the new //j and k variables and repeat the while loop. continue test; //if statement true then loop through the 'test' again. } } foundIt = true; break test; } System.out.println(foundIt ? "Found it" : "Didn't find it"); } } 

Oracle doesn't know how to write simple things in a tutorial for beginners . Oracle不知道如何在初学者教程中编写简单的东西。

This part: 这部分:

int n = substring.length();
int j = i;
int k = 0;
while (n-- != 0) {
    if (searchMe.charAt(j++) != substring.charAt(k++)) {
        continue test;
    }
}

can be changed to: 可以更改为:

int n = substring.length();
for (int k = 0; k < n; k++) {
    if (searchMe.charAt(i + k) != substring.charAt(k)) {
        continue test;
    }
}

So, you don't need j and instead of that tricky while you can use a simple for which is more intuitive, because it iterates through the substring . 所以,你不需要j和替代那种棘手while你可以使用一个简单for是更直观,因为它通过迭代substring

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM