通过循环访问数组 - java的 [英] Iterating through array - java

查看:89
本文介绍了通过循环访问数组 - java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果这是更好地为这个方法并通过阵列来的方法或我要检查,如果一个号码每次写出来是阵列

I was wondering if it was better to have a method for this and pass the Array to that method or to write it out every time I want to check if a number is in the array.

例如:

    public static boolean inArray(int[] array, int check) {
    for (int i = 0; i < array.length; i++){
        if (array[i] == check) 
            return true;
            }
    return false;
}

感谢您提前帮助!

推荐答案

您一定要封装这个逻辑到一个方法。

You should definitely encapsulate this logic into a method.

还有就是要重复相同的code多次没有任何好处。

There is no benefit to repeating identical code multiple times.

此外,如果您将逻辑的方法和它的变化,你只需要修改code在一个地方。

Also, if you place the logic in a method and it changes, you only need to modify your code in one place.

无论你是否希望使用第三方库是一个完全不同的决定。

Whether or not you want to use a 3rd party library is an entirely different decision.

这篇关于通过循环访问数组 - java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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