这个 java 程序的输出是如何生成的?

How is the output generated for this java program?

谁能解释一下这段代码的逻辑?怎么运行的?输出打印为 2 3 4 但我不明白如何......我正在写这些额外的行因为不幸的是堆栈溢出不允许我 post 这个问题如果没有足够的解释,这很烦人,因为我来这里只是为了得到这段代码的解释。希望它允许我现在 post 我的问题!

import java.util.Arrays;

public class Javaexcercise {
    public static void main(String[] args)

    {

        int[] my_array = {1, 2, 3, 4, 5, 6, 7, 2, 3, 4};
        

        for (int i = 0; i < my_array.length-1; i++)

        {

            for (int j = i+1; j < my_array.length; j++)

            {

                if ((my_array[i] == my_array[j]) && (i != j))

                {

                    System.out.println(my_array[j]);

                }

            }

        }

    }   

}

输出:

2
3
4

遍历数组中的每个索引

for (int i = 0; i < my_array.length-1; i++)

对于每个索引,遍历它后面的索引

    for (int j = i+1; j < my_array.length; j++)

如果数组中两个索引处的值相同

        if ((my_array[i] == my_array[j]) && (i != j))

打印值

            System.out.println(my_array[j]);

因此,您将打印任何匹配对中的第一项。

请注意,i != j 子句是多余的,可以删除,因为 ji + 1 开始。

您的程序将比较数组中每一对的值并仅打印匹配项。