在阵列的若干查找 [英] Lookup of a number in an array

查看:162
本文介绍了在阵列的若干查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要问我在做什么错误导致该if语句我每次运行程序时是假的。我也试过用 .equals(NUM)而不是 Array.asList(NUM)检查该值在那里,但导致循环的不在数据库中多次重复。

I wanted to ask what am I doing wrong that is causing the if statement to be false every time I run the program. I've also tried to use .equals(num) instead of the Array.asList(num) to check if the value is in there, but that caused the for loop to repeat the "Not in database" multiple times.

推荐答案

Arrays.asList(数据)创建(名单的唯一元素是一个int数组即列表与LT; INT []> )。这就是为什么 Arrays.asList(数据)。载有(NUM))始终返回false。

Arrays.asList(Data) creates a list whose only element is an array of int (i.e. List<int[]>). That's why Arrays.asList(Data).contains(num)) always returns false.

试着改变你的数组:

Integer Data[] = new Integer[n];

这将使 Arrays.asList(数据)创建整数列表清单&LT;整数方式&gt; ),包含原始阵列,这是你所需要的所有整数

This will make Arrays.asList(Data) create a list of Integer (List<Integer>) containing all the integers of the original array, which is what you need.

这样做的原因行为是 Arrays.asList 预计一个或多个对象译文]它的输入。如果你通过对象的数组S(如整数[] ),它相当于传递多个对象秒。但是,如果你传递原语的数组(如 INT [] ),唯一的对象在输入是数组本身,所以 Arrays.asList()创建一个列表,其唯一的元素是数组。

The reason for this behavior is the Arrays.asList expects one or more Objects as its input. If you pass an array of Objects (such as Integer[]), it is equivalent to passing multiple Objects. If, however, you pass an array of primitives (such as int[]), the only Object in your input is the array itself, so Arrays.asList() creates a list whose only element is that array.

这篇关于在阵列的若干查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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