取整数数组 [英] Taking integer arrays

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

问题描述

boolean isValidIndex (int [] x, int y) {
    if((y>=0) && (y<x.length)) {
        return true;
    }
    else {
        return false;

    }
}

写一个称为<$ c的方法$ c> isValidIndex()接受一个整数数组和一个索引,如果索引对该数组有效,则返回true。例如,如果数组有10个元素,则 isValidIndex(array,9)将返回True,但是 isValidIndex(array,10)将返回False,就像 isValidIndex(array,-1)

Write a method called isValidIndex() that takes an array of integers and an index, and returns true if the index is valid for the array. For example, if the array had 10 elements then isValidIndex(array, 9) would return True,but isValidIndex(array, 10) would return False,as would isValidIndex(array, -1).

这是我的代码。它有效,但是显然它只是一个陈述。我该怎么做?

Here is my code. It works but apparently it can just be one statement. How can I do that?

推荐答案

任何格式为的< expr>那么true否则为false ,总是可以缩短为< expr> 。因此,在您的情况下:

Anything on the format if <expr> then true else false, can always be shortened to just <expr>. So, in your case:

boolean isValidIndex (int [] x, int y) {
    return (y >= 0) && (y < x.length);
}

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

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