如何在单个数组中查找长度为5的副本。 Java的 [英] How to find a duplicate of length 5 in a single array. Java

查看:105
本文介绍了如何在单个数组中查找长度为5的副本。 Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个数组,然后以五个为一组打印。我希望能够通过5搜索数组,看看是否有任何重复。我已经尝试了,但我只想到一种方法来搜索每个值而不是五个。如果有人能指出我正确的方向,那将是伟大的。谢谢。

I create an array that then prints in sets of five. I want to then be able to search the array by 5 to see if there are any duplicates. I've tried but I can only think of a way to search by each value not five. If anyone can point me in the right direction, that would be great. Thanks.

 public class findPat {
        static int arr [] = new int [10];
        static int st = 1;
        static int end = 56;
        static double t1;
        static double t2;

        public static void main(String[] args){

            t1=System.currentTimeMillis(); 
            for(int n=0; n<100; n++){   
                for (int i=0; i<arr.length; i++)
                    arr[i]= (int) (Math.random()* (end-st +1)) +st;

                for (int i=0; i<5; i++){ 

                    if (i%5==0) 
                        System.out.println(); 
                        System.out.print("\t" + arr[i]);} 
                    }
            t2=System.currentTimeMillis();
            System.out.println();
            System.out.println();
            System.out.println("\t" + "Total run time is " + ((t2-t1)) + "ms");

            }
        }

控制台如下所示:

    18  22  42  14  38
    2   2   14  9   8
    6   29  38  37  33
    6   41  41  27  7
    20  41  38  11  50
    16  17  41  21  19
    40  33  9   10  7
    12  54  10  30  36

但是每行都在同一个数组中,但一次只打印5行。
控制台将拥有的不仅仅是那几行。我希望能够搜索数组并检查每一行,看看它出现的次数,如果有的话。

however each row is in the same array but is just printing 5 at a time. the console will have more than just those few lines. I want to be able to search the array and check each row against the rest to see how many times it appears, if it does.

推荐答案

您可以使用 Hashtable 来实现此目的。使用您的代码作为基础,我编写了一个示例实现,但不知道您正在尝试做什么,我无法判断这是否是您正在寻找的。

You could implement this using a Hashtable. Using your code as a base, I've written an example implementation but without knowing what it is you are trying to do, I can't judge if this is what you are looking for.

import java.util.Hashtable;

public class findPat {
    static final int COUNT = 100;

    static Hashtable<String, Integer> compareSet = new Hashtable<String, Integer>();
    static String groupInteger = "";
    static int arr [] = new int [5];
    static int st = 1;
    static int end = 56;
    static double t1;
    static double t2;

    public static void main(String[] args) {
        t1=System.currentTimeMillis(); 
        for(int n = 0; n < COUNT; n++){
            for (int i = 0; i < arr.length; i++) {
                arr[i] = (int) (Math.random()* (end - st + 1)) + st;

            }
            for (int i = 1; i <= 5; i++) {
                groupInteger += arr[i-1];
                System.out.print("\t" + arr[i-1]);
                if (i % 5 == 0) {
                    System.out.println();
                    if (compareSet.containsKey(groupInteger)) {
                        System.out.println("duplicate found");
                        int currentCount = compareSet.get(groupInteger);
                        compareSet.put(groupInteger, currentCount + 1);
                    } else {
                        compareSet.put(groupInteger, 1);                        
                    }
                    groupInteger = "";
                }

            } 
        }
        t2=System.currentTimeMillis();
        System.out.println();
        System.out.println();
        System.out.println("\t" + "Total run time is " + ((t2 - t1)) + "ms");
    }
}

此代码跟踪独特的随机数集通过添加它们(为每个具有相同顺序的相同值的集合创建一个相同的键值,连接的字符串会处理这个)。

This code keeps track of the unique sets of random numbers by adding them (creating a key value that is the same for every set that has the same values in the same order, the concatenated string takes care of this).

你的代码在我的系统上运行了13秒,我的代码需要17秒。现在,如果运行时至关重要,您可能需要研究散列技术。但我不确定你是否能够削减很多,因为你将不得不添加一些额外的代码,这需要额外的时间。

Your code ran in 13 seconds on my system, mine takes 17 seconds. Now if runtime is of crucial importance, you might want to look into hashing techniques. But I'm not sure if you will be able to shave off a lot as you will have to add some extra code which will take extra time.

这篇关于如何在单个数组中查找长度为5的副本。 Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆