检查数组是否包含某些整数 [英] Checking if an array contains certain integers

查看:60
本文介绍了检查数组是否包含某些整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个长度为10个整数的数组,因此每个位置0-9都包含一个不同的整数0-9.

I am making an Array that is 10 integers long, so that each place 0-9 contains a different integer 0-9.

我在弄清楚如何检查数组是否已经包含某个数字时遇到了麻烦,如果是,则重新生成一个新的数字.

I am having trouble figuring out how to check if the array already contains a certain number, and if so, regenerating a new one.

到目前为止,我有:

for (int i = 0; i < perm.length; i++)
{
    int num = (int) (Math.random() * 9); 
    int []

    perm[i] = num;   
}

推荐答案

Arrays.asList(perm).contains(num) 

来自如何我测试数组是否包含某个值?

for (int i = 0; i < perm.length; i++)

这还不足以像这样循环,如果发生碰撞,某些插槽将不会被初始化.

this is not enough to loop like this, if collision happens some slots would have been not initalized.

总体,对于此任务,您最好依次使用值初始化数组,然后

Overall, for this task you better initialize array with values in order and then shuffle it by using random permutation indexes

这篇关于检查数组是否包含某些整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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