繁体   English   中英

添加到数组并返回布尔值

[英]add to array and return boolean

我想向数组添加一个对象,如果数组未满,则可以返回true,以便可以添加该项目,如果数组已满,则返回false。

我现在所拥有的相信只是部分正确的。

private Object[] theList;
//constructors omitted
@Override
public boolean add(Object toAdd) {
    for(int i=0; i < theList.length;i++){
        if(toAdd != null){                  
            theList[i] = toAdd; 
        }         
    }
    return true;
}

我认为使用元素计数器是跟踪当前数组中元素数量的正确方法。

如果重要的话,该方法将从接口覆盖。

不知道它是基于循环和条件返回的是true还是不管返回什么。

您尚未初始化object[] ObjectArray theList 因此,它将无法为此添加新元素。

在这里,我使用ArrayList类为您的问题提供解决方案。 ArrayList是在运行时动态增加其大小的自动可增长数组。

 private ArrayList<Object> theList = new ArrayList<>(); //A Auto Growable Array Increased Its Size Dynamically at Runtime.
    //constructors omitted
    @Override
    public boolean add(Object toAdd) {
         if(toAdd != null){                  
            theList.add(toAdd); 
            return true; //returns true id Object is added to theList.
         }else
             return false; //returns false if Object is Null .
    }

获取ArrayList大小的代码

theList.size() //Use This Code to know the Size of Array List.

似乎您正在尝试查找数组中的第一个null值,如果存在该值,则将其替换为toAdd对象并返回true。

相应的代码如下:

private Object[] theList;

@Override
public boolean add(Object toAdd) {
    for(int i=0; i < theList.length; i++) {
        if(theList[i] == null){                  
            theList[i] = toAdd;

            return true;
        }         
    }

    return false;
}

如果出于性能原因,如果您还希望在toAdd对象为null情况下返回false ,则应遍历数组之前进行检查:

@Override
public boolean add(Object toAdd) {
    if (toAdd != null) {
        for(int i=0; i < theList.length; i++) {
            if(theList[i] == null){                  
                theList[i] = toAdd;

                return true;
            }         
        }
    }

    return false;
}

显然,改用ArrayList更方便。

首先,您必须初始化数组。 否则,它将返回空指针异常。 这是我的代码。 希望对您有帮助...

class Demo{
    static boolean result;
    public static void main(String args[]){

        Object ob= new Object();
        result = A.add(ob);

        System.out.println(result);

        Object ob1= new Object();
        result = A.add(ob1);
        System.out.println(result);

    }

}

class A{
    static private Object[] theList = new Object[10];
    public static boolean add(Object ob){
        boolean result = false;
            for(int i=0; i<theList.length; i++){
                if(ob != null){
                    theList[i] = ob;
                    result = true;
                }
            }
        return result;  
    }
}

对象数组的默认值为null 这样就可以检查一个元素是否为空。

for(int i=0:i<theList.length;i++) {
   if(theList[i] == null && toAdd!=null)  // Element is null or not
       theList[i]=toAdd;
       return true;   
}
return false;

如果element为null,则array仍然为空,您可以在其中放置值并返回true

您可能要考虑使用ArrayList,因为它们是可扩展的并且没有设置大小。 因此,每当添加一些东西时,它只会增加ArrayList而不是更改索引的值。

根据同行贡献者的建议,第二次编辑了另一种解决方案。 我还在评论中发表了对问题的理解。 请提出谅解,如果有任何偏差。

请查看一种可能的解决方案:

public class TestCode {

public static void main(String[] args) {
    /*
     * Question: 
     * I would like to add an object to an array and 
     * return true if the array is not full so the item can be added, 
     * and false if the array is full of items.
     */
    Object[] objects = new Object [] {1, 2, null, 4, 5, null , 7};
    SomeClass someClass = new SomeClass(objects);
    // just for sake of passing;
    Integer toAdd = 10;
    boolean isNotFull = someClass.add(toAdd);

    System.out.println("Array after adding: "+Arrays.toString(objects));
    System.out.println("Array is Not full:"+isNotFull);

    isNotFull = someClass.add(toAdd);
    System.out.println("Array after adding: "+Arrays.toString(objects));
    System.out.println("Array is Not full:"+isNotFull);

    isNotFull = someClass.add(toAdd);
    System.out.println("Array after adding: "+Arrays.toString(objects));
    System.out.println("Array is Not full:"+isNotFull);



}

}

class SomeClass {
private Object[] theList;
//constructors omitted

public SomeClass(Object[] theList) {
    this.theList = theList; 
}
/*
 * returns true if array is not full and element is inserted.
 */
public boolean add(Object toAdd) {
    int i = 0;
    boolean flag = false; // if flag is true, the array is not empty
    // question says: 
    // "I would like to add an object to an array"
    // means we need to first find an empty slot to insert in the array
    for( ; i < theList.length;i++){
        // to check if there is any first empty space in the array
        // so that the element can be inserted.
        // (finding the empty slot
        if(theList[i] == null){
            // to check if elements passed as an aargument itself is false
            if (toAdd!=null) {
                theList[i] = toAdd;
                break;
            }

        }

    }

    for (;i <theList.length; i++) {
         if(theList[i] == null){
            flag = true;
        }
    }
    return flag;
}
}

解决方案:我得到的代码输出:

Array after adding: [1, 2, 10, 4, 5, null, 7]
Array is Not full:true
Array after adding: [1, 2, 10, 4, 5, 10, 7]
Array is Not full:false
Array after adding: [1, 2, 10, 4, 5, 10, 7]
Array is Not full:false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM