本文介绍了机器人(方法用于检查数组的重复数字?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以任何一个能帮助我。我想提出一个应用程序,并在Java,号码发送到一个int数组,我需要检查是否有数组中的数字重复,如果有要调用一个方法或类似的东西。有没有一种方法来检查这个或类似的东西?或者,我会让它使用循环和if语句,这是我尝试过,但是是有点长,混乱的事情。任何意见将是巨大的,谢谢。

  INT测试[] = {0,0,0,0,0,0,0}; (数组)

(一种方法来检查是否有数组数被重复)
 

解决方案

您可以使用Collections.frequency()方法得到的数多少次号码repeated.Depending你想要什么,你可以遍历整数数组,并检查了多少次每一个重复。如果一个项目的频率大于1比它重复在该阵列。您可以根据自己的需要调整以下code。注:顺便说一句的System.out.println()给出的输出偏食日志猫部分。它只是为了演示。

 的ArrayList<整数GT; NUMS =新的ArrayList<整数GT;();
        nums.add(新的整数(3));
        nums.add(新的整数(3));
        nums.add(新的整数(3));
        nums.add(新的整数(2));
        nums.add(新的整数(2));
        nums.add(新整型(1));
        的System.out.println(++ Collections.frequency(NUMS的1的数量,1));
        的System.out.println(++ Collections.frequency(NUMS的2​​的数量,2));
        的System.out.println(++ Collections.frequency(NUMS3人数,3));
 

could any one help me. i am making an app, and in the java, numbers are send to a int array and i need to check if any of the numbers in the array repeated and if there are to call a method or something like that. Is there a method to check this or something similar? or would i have to do it using loops and if statements, which i have tried but is getting a bit long and confusing. Any advice would be great, thanks.

int test[] = {0,0,0,0,0,0,0}; (The Array)

(A method to check if any of the arrays numbers are repeated)
解决方案

You can use Collections.frequency() method to get count ofhow many times a number is repeated.Depending on what you want,you can iterate over an array of Integers and check how many times each one repeated.if an items frequency is greater than 1 than it is repeating in that array.You can adapt the following code according to your needs.Note: by the way System.out.println() gives output to eclipse log cat section.it is just for demonstration.

ArrayList<Integer> nums = new ArrayList<Integer>();
        nums.add(new Integer(3));
        nums.add(new Integer(3));
        nums.add(new Integer(3));
        nums.add(new Integer(2));
        nums.add(new Integer(2));
        nums.add(new Integer(1));
        System.out.println("Number of 1's" + " " + Collections.frequency(nums, 1));
        System.out.println("Number of 2's" + " " + Collections.frequency(nums, 2));
        System.out.println("Number of 3's" + " " + Collections.frequency(nums, 3));

这篇关于机器人(方法用于检查数组的重复数字?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:20