我正在研究对象数组,直到没有元素为止,我才能获取元素。例如

Object[] elements={"Apple","Orange","Grape","Mango","Banana"};

我可以按索引获取元素,但是当我超出索引长度时。例如,这里有5个元素最后将由elements [4]访问,但是如何检查elements [5],elements [6]或不

最佳答案

例如,

    Object[] elements = {"Apple","Orange","Grape","Mango","Banana"};

    Object obj = null;
    int elementsLength = elements.length;

    int i = 10;

    if (i < elementsLength) {
        obj = elements[i];
    } else {
        System.out.println("Oops!.. Out of index!");
    }

    if (obj != null) {
        System.out.println(obj);
    }

关于java - 如何查找包含元素的对象数组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8546670/

10-12 20:11