如何使用整数数组比较整数 [英] How to compare Integer with integer array

查看:193
本文介绍了如何使用整数数组比较整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的机器人。我想知道如何用整数整数数组进行比较。有一组整型数组(防爆)array_int = {1,2,3,4}和一个整数为int i = 2,在这里,我想这两个整数比较,并在情况下,如果一个整数出现在阵整数,我想打破过程。

i am new to android. I want to know how to compare Integer with integer array. There is a set of integer array (Ex) array_int={1,2,3,4} and single integer int i=2, here i want to compare both integers,and in case if single integer appears in array integer, i want to break the process.

for(i=0;i<integerArray.length;i++){
    if(singleinteger!=integerArray[i]){ // some action       }
else{
// Stop the action  }

在这种情况下,这两个整数进行比较。在当两个整数都是平等的过程中得到休息时间,否则重复动作直到循环结束时获得。

In this case it compares both integer. and at the time when two integers are equal process getting break, otherwise it iteration action till the loop getting end.

推荐答案

对于一个简单的解决方案,使用:

For a simple solution, use:

for (i = 0; i < intArray.length; i++) {
    if (singleInt != intArray[i]) {
        // some action
    } else {
        break;
    }
}

这将来自回路,当两个值相等断裂。然而,一些较真不喜欢使用,因为它可以引进可读性问题的尤其的如果你的一些动作大(在code线而言),因为这消除退出条件远离本身。

That will break from the loop when the two values are equal. However, some purists don't like the use of break since it can introduce readability issues, especially if your some action is large (in terms of lines of code) since that removes an exit condition far away from the for itself.

要解决这个问题,你可能要考虑扭转如果语句的感觉,这样的退出条件更接近

To fix that, you may want to consider reversing the sense of the if statement so that that exit condition is closer to the for:

for (i = 0; i < intArray.length; i++) {
    if (singleInt == intArray[i])
        break;

    // some action
}

和,也将让你删除其他(这是其他本身的的内容),因为它并不需要更多的。

and that will also let you remove the else (that's the else itself, not its contents) since it's not needed any more.

但是,如果你要做到这一点,你可能也纳完全进入,并用它来完成:

But, if you're going to do that, you may as well incorporate it totally into the for and be done with it:

for (i = 0; (i < intArray.length) && (singleInt != intArray[i]); i++) {
    // some action
}

这篇关于如何使用整数数组比较整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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