Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 这段代码检查什么?_Java_Arrays - Fatal编程技术网

Java 这段代码检查什么?

Java 这段代码检查什么?,java,arrays,Java,Arrays,有人能告诉我下面的代码是做什么的吗?更具体地说是!重复部分。if语句检查的具体内容是什么 for (int i = 0; i < list.length; i++) { boolean duplicate = false; for (int x = 0; x < list.length; x++) { if (integer[x] == list[i

有人能告诉我下面的代码是做什么的吗?更具体地说是!重复部分。if语句检查的具体内容是什么

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }

您提供的示例代码没有任何作用,因为它充满了错误。我不知道你从哪里得到这个代码,但需要重新思考。代码的初衷其实很明显,它将创建一个新的整数数组,该数组将包含所有不同且唯一的整数值。所提供的名为list的整数数组中不包含可能包含或不包含重复值的重复项。重复值将被忽略,并且它只是应用于新数组的该重复项的第一个实例以及“列表”数组中没有重复项且因此被视为唯一的任何值

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
布尔重复变量仅在外部for循环期间用作标志,以指示内部for循环在迭代期间在“list”数组中发现重复值。当内部for循环完成时,将检查此标志的状态,如果发现为false,则将该值添加到“integer”数组中。如果在确定当前正在处理的值确实是重复的并且重复变量设置为boolean true时,立即使用break语句退出内部for循环,则会更加有效,因为不再需要进一步处理初始整数值,也不需要处理外部for循环我们可以继续

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
在您的示例中显示的代码意图是实现这类事情的一种方法有几种方法,只有Google:使用Java从整数数组中删除重复项

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
由于您处理的是数组,因此必须正确声明它们,并使用该数组的元素所需的长度初始化它们,例如:

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
int[] integer = new int[12];
int[] integer = int[list.length]; 
通常,对于此特定任务和许多其他任务,许多新程序员将声明并初始化新数组整数本身,使其长度与他们正在检查重复或使用的原始数组的长度相同,例如:

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
int[] integer = new int[12];
int[] integer = int[list.length]; 
这实际上是做这类事情的错误方法,因为如果遇到重复项,新数组中可能有很多元素可能包含0。对于列表数组中遇到的每个重复值,整数数组将少包含1个元素,但由于整数数组已初始化为与原始列表数组相同的长度,因此您认为不存在的元素实际上将存在,并且它将包含0,因为int的默认值为0,即单个整数变量或整数数组中的元素。如果您准备在代码执行过程中处理这种情况,或者在大多数情况下,列表数组中实际上没有包含不确定的重复值,那么这样做是可以的。这就是为什么大多数程序员在执行这种特殊类型的任务时往往不使用数组,而是使用其他类和或方法

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
要演示上述场景,请在新Java应用程序项目的主方法中输入以下代码并运行它:

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
    // Our Hard Coded Integer 'list' Array.
    int[] list = {1, 3, 5, 2, 4, 5, 6, 3, 6, 7, 
                  2, 9, 5, 8, 0, 0, 1, 5, 8, 2};
    // Declare & initialize the 'integer' Array
    int[] integer = new int[list.length]; // Premature initialization
    boolean duplicate;
    idxCnt = 0;
    for (int i = 0; i < list.length; i++) {
        duplicate = false;
        for (int x = (i + 1); x < list.length; x++) {
            if (list[i] == list[x]) {
                duplicate = true;
                break;
            }
        }
        if (!duplicate) {
            integer[idxCnt] = list[i];
            idxCnt++;
       }        
    }

    System.out.println(Arrays.toString(integer));
通过查看“list”数组的内容,我们已经可以看到,尽管有20个元素,但仍然有10个不同的数字,因此显然也有10个重复值。请注意,在控制台显示中,只有来自“list”数组的第一个实例和唯一值确实被放入了“integer”数组中,但在“integer”数组的末尾有10个零0,它们根本不需要。这是因为'integer'数组被声明并初始化为与'list'数组相同的长度,我们已经知道它包含20个元素

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
注意:您可能还注意到上面的代码显示了您在帖子中显示的意图的工作版本

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
在我看来,当您要填充数组时,一定要声明并初始化该数组,使其达到所需的精确长度。为了做到这一点,在这种情况下,您需要首先迭代“列表”数组,以确定实际存在多少重复值,这是一种方法,您可以坚持已经建立的原始编码格式:

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
    // Our Hard Coded Integer 'list' Array.
    int[] list = {1, 3, 5, 2, 4, 5, 6, 3, 6, 7, 
                  2, 9, 5, 8, 0, 0, 1, 5, 8, 2};
    int idxCnt = 0;  // To eventually hold the number of duplicates.

    // Find out how many duplicates there are within
    // the 'list' int Array.
    for (int i = 0; i < list.length; i++) {
        for (int x = (i + 1); x < list.length; x++) {
            if (list[i] == list[x]) { 
                idxCnt++; 
                break; 
            }
        }
    }
    System.out.println("There are " + idxCnt + " duplicate values "
                     + "contained within the 'list' Array of\n" 
                     + list.length + " elements. We only "
                     + "need " + (list.length - idxCnt) + " elements "
                     + "for our 'integer' Array.");
20个元素-10个重复=将不同值放入“整数”数组所需的10个元素

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
现在我们知道“整数”数组需要多少个元素,我们可以将其初始化为:

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
int[] integer = new int[idxCnt];
当所有内容都放在一起时,代码将如下所示:

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
There are 10 duplicate values contained within the 'list' Array of
20 elements. We only need 10 elements for our 'integer' Array.
// Our Hard Coded Integer 'list' Array.
    int[] list = {1, 3, 5, 2, 4, 5, 6, 3, 6, 7, 
                  2, 9, 5, 8, 0, 0, 1, 5, 8, 2};
    boolean duplicate;  // Flag to indicate duplicate encountered.
    int[] integer;      // Declare the 'integer' Array
    int idxCnt = 0;     // To eventually hold the number of duplicates.

    // Find out how many duplicates there are within
    // the 'list' int Array.
    for (int i = 0; i < list.length; i++) {
        for (int x = i+1; x < list.length; x++) {
            if (list[i] == list[x]) { 
                idxCnt++; 
                break; // No need to continue inner loop
            }
        }
    }

    // Initialize the new 'integer' Array
    integer = new int[list.length - idxCnt];

    // Retrieve distinct values from 'list' Array
    // and place them into the 'integer' Array.
    idxCnt = 0;     // Used as Index Counter.
    for (int i = 0; i < list.length; i++) {
        duplicate = false;
        for (int x = (i + 1); x < list.length; x++) {
            if (list[i] == list[x]) {
                duplicate = true;
                break; // No need to continue inner loop
            }
        }
        if (!duplicate) {
            // Deemed as not duplicate so add the value
            // to the 'integer' Array
            integer[idxCnt] = list[i];
            idxCnt++; // Increment the index for 'integer' Array.
       }        
    }

    // Sort the 'integer Array.
    Arrays.sort(integer); 

    // Display the 'integer' Array to Console Window
    System.out.println(Arrays.toString(integer));
很长时间了,不是吗。这就是为什么大多数必须处理数组的程序员更喜欢做一些看起来更简单的事情,比如:

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }
使用Java 8:

        for (int i = 0; i < list.length; i++) {
                boolean duplicate = false;
                    for (int x = 0; x < list.length; x++) {
                        if (integer[x] == list[i]) {
                            duplicate = true;
            //If integer is new, duplicate = false, if it's repeated in 
            //list, duplicate = true
        }
     }
        if (!duplicate) {
                integer[value++] = list[i];
        }

需要更多的上下文以便更好地解释。从这一点来看,duplicate似乎是一个布尔变量。如果为false,则使用列表[i]中的值递增索引值处整数数组的值
@clinomaniac添加了更多代码。希望这有助于不要标记垃圾邮件,除非你想引起负面注意。你在问什么!方法您的具体问题是什么?第一部分是确定名为list的数组中的任何值是否等于数组中的任何其他值的低效方法。如果找到一个,则将索引编号值处名为integer的数组中的值设置为名为list的数组中的最后一个值,之后该数值将递增一。所有这些变量的名称都非常误导人。最终的效果完全取决于其他代码如何使用这些变量-脱离上下文,此代码根本没有意义。new ArrayListnew HashSetlst绝对不会对值排序-它将以未定义的顺序生成列表。你可能想要树集而不是哈希集。不,我不是有意的。我的意图与我写的一模一样:lst=newarraylistnewhashsetlst;使用diamond reference,它确实使用Java 8对整数列表元素进行排序,我现在在我的回答中已经提到了Java 8。请为该断言提供一个参考,因为它与我的个人经验和理论相矛盾,它说:它不保证集合的迭代顺序;特别是,它不能保证订单在一段时间内保持不变。它将删除重复项,但这与排序不同。我向@DanielPryden和其他所有人表示歉意,没错,HashSet不会对大于一位数的较大整数值进行排序,但是对于从0到9的整数值,正如上面的示例所示,这样做没有问题。由于HashSet只在列表中使用了一次,所以随着时间的推移保持不变的顺序实际上并不相关。由于HashSet具有这种排序限制,因此它几乎不值得用于排序机制。编辑答案。最重要的是,谢谢丹尼尔的耐心,让我的眼睛睁大了一点。