在两个数组中查找重复值

时间:2016-10-01 14:21:19

标签: java arrays

我已将两个文本文件存储到两个单独的数组中。现在,我正在尝试比较两个数组以查找重复值。我的逻辑问题,我无法打印重复值出现的次数。

file1包含:

1913 2016 1 1913 186
2016 1711 32843 2016 518
3 1913 32843 32001 4
250 5 3500 6 7
8 27 73 9 10
1711 73 11 2 1.4
1.4 12 33.75278 84.38611 1913
19 1930 20 21 1947
22 1955 23 1961 23
1969 27 1995 26 27
1962 28 29 30 1970
31 31 

file2包含:

1913 2016 32843 31 27 1.4 4 7 2 23

我试图在file2中找到在file1中重复的值,以及多少次。

我有以下代码:

 public static void findDuplicates() {

        // array for first file
        for (int n = 0; n < nums.size(); n++) {

            // matches are false by default
            boolean match = false;

            int count = 0;

                String v = nums.get(n);

            // array for second file
            for (int k = 0; k < nums1.size(); k++) {

                String p = nums1.get(k);

                // second file contains values from first file
                if (p.contains(v)) {

                    // there is a match
                    match = true;

                    // when there is a match print out matched values and the number of times they appear in second file
                    if (match) {

                    count++;

                        System.out.println( p + " " + "is duped" + " " + count + " " + "times");

                    }


                }

            }

        }


    }

当我编译并运行此代码时,这是输出:

31 is duped 1 times

有人能让我知道我在这里做错了吗?

修改

以下是我的其余代码:

 public static ArrayList<String> nums;
 public static ArrayList<String> nums1;

    //Create a main method to start the program.
    //Add FileNot FoundException in case the file can't be found by computer.
    public static void main(String[] args) throws FileNotFoundException{

        //The while will help us read the content into our computer piece by piece. It will not stop until the end of assignment.csv.

        while(FILE1.hasNext()){

                //Create a String variable - TempString. We use TempString to store each piece temporarily.

                String TempString = FILE1.next();

                String temp1 = TempString.replaceAll("[\\,]", "");

                String pattern1 = "[0-9]+\\.{1}[0-9]+";

                //Compile the Regular Expression into Pattern and store it in r1 so that the computer can understand the Regular Expression.
                Pattern r1 = Pattern.compile(pattern1);


                Matcher m1 = r1.matcher(temp1);

                String pattern2 = "[0-9]+";

                //Compile the Regular Expression into Pattern and store it in r2 so that the computer can understand the Regular Expression.
                Pattern r2 = Pattern.compile(pattern2);


                Matcher m2 = r2.matcher(temp1);

                nums = new ArrayList<String>();



                //Recollect, m1 is used to match decimal numbers.

                if(!(m1.find())){//if a decimal number CAN'T be found

                    //We use while statement instead of if statement here. 
                    //If there is only one piece per line, we can use either while statement or if statement.
                    //However, we have to use while statement if there is more than one piece per line.
                    while(m2.find()) {//if an integer number CAN be found
                        //If an Integer is found, we add 1 to Variable count.

                        count++;
                        //Even though the number (i.e., m2.group(0)) is an Integer, its data type is String. So we store it to a String variable - number.

                        String number = m2.group(0);


                        nums.add(number);

                        //If the remainder of count by 5 is zero, we display the number and advance to a new line.
                        if (count % 5 == 0){

                            System.out.println(number);

                        }
                        //Otherwise, we just display the number on the same line and divide numbers by a space.
                        else
                            System.out.print(number + " ");

                    }
                }

                //If we find a decimal number
                else{
                        //We add 1 to Variable count.



                        count++;

                        //Even though the number (i.e., m1.group(0)) is a decimal number, its data type is String. So we store it to a String variable - number.

                        String number = m1.group(0);

                        nums.add(number);

                        //If the remainder of count by 5 is zero, we display the number and advance to a new line.
                        if (count % 5 == 0) {

                            System.out.println(number);

                        }

                        //Otherwise, we just display the number on the same line and divide numbers by a space.
                        else
                            System.out.print(number + " ");

                }


        }



    FILE1.close();//Once we finish the task, we close the file.

        while(FILE2.hasNext()){

            //Create a String variable - TempString. We use TempString to store each piece temporarily.
            String TempString = FILE2.next();


            //So I use replaceAll function to eliminate comma (,) and store the new string in temp1.
            String temp1 = TempString.replaceAll("[\\,]", "");



            String pattern1 = "[0-9]+\\.{1}[0-9]+";

            //Compile the Regular Expression into Pattern and store it in r1 so that the computer can understand the Regular Expression.
            Pattern r1 = Pattern.compile(pattern1);

            //Match the Regular Expression with the piece (temp1) we read from assignment.csv.
            Matcher m1 = r1.matcher(temp1);

            String pattern2 = "[0-9]+";

            //Compile the Regular Expression into Pattern and store it in r2 so that the computer can understand the Regular Expression.
            Pattern r2 = Pattern.compile(pattern2);

            //Match the Regular Expression with the piece (temp1) we read from assignment.csv.
            Matcher m2 = r2.matcher(temp1);

            nums1 = new ArrayList<String>();


            //We have two types of numbers - Integer and Decimal
            //Let's start us Integer.
            //Recollect, m1 is used to match decimal numbers.
            if(!(m1.find())){//if a decimal number CAN'T be found

                //We use while statement instead of if statement here.
                //If there is only one piece per line, we can use either while statement or if statement.
                //However, we have to use while statement if there is more than one piece per line.
                while(m2.find()) {//if an integer number CAN be found
                    //If an Integer is found, we add 1 to Variable count.


                    count++;
                    //Even though the number (i.e., m2.group(0)) is an Integer, its data type is String. So we store it to a String variable - number.

                    String number = m2.group(0);

                    nums1.add(number);

                    //If the remainder of count by 5 is zero, we display the number and advance to a new line.
                    if (count % 5 == 0){

                        //System.out.println(number);

                    }
                    //Otherwise, we just display the number on the same line and divide numbers by a space.
                    else
                        System.out.println(/*number + " "*/);

                        }
            }

            //If we find a decimal number
            else{
                //We add 1 to Variable count.


                count++;

                //Even though the number (i.e., m1.group(0)) is a decimal number, its data type is String. So we store it to a String variable - number.

                String number = m1.group(0);

                nums1.add(number);

                //If the remainder of count by 5 is zero, we display the number and advance to a new line.
                if (count % 5 == 0){

                    //System.out.println(number);
                }
                //Otherwise, we just display the number on the same line and divide numbers by a space.
                else
                    System.out.println(/*number + " "*/);

            }


            findDuplicates();


        }


        FILE2.close();//Once we finish the task, we close the file.

  }

我试图删除尽可能多的不必要的代码。

修改

预期输出应为:

1913 is duplicated 3 times.
2016 is duplicated 2 times.
32843 is duplicated 1 times.
31 is duplicated 2 times..... 

修改

所以我相信我发现了这个问题。出于某种原因,

String p = nums.get(k)
我的findDuplicates()方法中的

仅返回值31,而不返回其他值。我正在努力解决问题,并会在我做的时候发布答案。

3 个答案:

答案 0 :(得分:1)

我认为最大的问题是printline在第二个for循环中。
此外我会删除布尔值并只比较2个字符串(p==v)

所以代码看起来更像是这样:

public static void main(String[] args) {
    // array for second file
    for (int n = 0; n < nums1.size(); n++) {

        // matches are false by default

        int count = 0;

            String v = nums1.get(n);

        // array for first file
        for (int k = 0; k < nums.size(); k++) {

            String p = nums.get(k);

            // second file contains values from first file
            if (p==v) {

                count++;

                }


            }
        System.out.println( v + " " + "is duped" + " " + count + " " + "times");

        }

    }

}

根据我所做的更改,代码按预期运行。
您可以查看实时演示here

输出:

1913 is duped 4 times
2016 is duped 3 times
32843 is duped 2 times
31 is duped 2 times
27 is duped 3 times
1.4 is duped 2 times
4 is duped 1 times
7 is duped 1 times
2 is duped 1 times
23 is duped 2 times

答案 1 :(得分:0)

你应该在内部循环之外使用System.out.println语句,这样第一个完整的第二个arraylist会在重复编号的次数之前被迭代。

您还需要进行一些其他更改才能正确运行程序

for(int n = 0; n&lt; nums.size(); n ++){

        // matches are false by default
        boolean match = false;

        int count = 0;

            String v = nums.get(n);

        // array for second file
        for (int k = 0; k < nums1.size(); k++) {

            String p = nums1.get(k);

            // second file contains values from first file
            if (p.contains(v)) {

                // there is a match
                match = true;

                // when there is a match print out matched values and the number of times they appear in second file
                if (match) {

                count++;

                match = false;
                }
           }
           System.out.println( p + " " + "is duped" + " " + count + " " + "times");
           count = 0;               

        }

    }

但是你的逻辑仍然不能用于所有情况,因为你没有比较第一个文件中重复的次数。您只是将第二个文件编号与第一个文件编号进行比较。对于您提出的问题,在修改代码之后交换这两个文件的情况如我所提到的那样它将起作用。

答案 2 :(得分:0)

请试一试。

<input id="LastName" name="LastName" type="text"  placeholder="Last Name" required/>