我如何在数据结构中添加对象到数组列表?

时间:2017-06-02 00:04:01

标签: java arrays

欢迎来到我的难题;)在java中我有一个问题..我有我的特定列表实现存储对象

我能做些什么来解决这个问题?任何身体都可以帮助我并挽救我的一天?

public class Alist implements ListInterface {

      private Object entry[];   // array of list entries
     static public int length;  // current number of entries in list
      private static final int MAX_SIZE = 50; // max length of list
public Alist()
      { length=0;
        entry= new Object [ MAX_SIZE ];
      }// end default constructor

public Alist ( int maxSize)
      { length=0;
        entry= new Object[ maxSize ];
      }// end user constructor

public boolean add ( Object newEntry)
      {
        Boolean isSuccessful =true;
        if(!isFull())
        {
          entry [length]=newEntry;
          length++;
        }
       else
        isSuccessful =false;
      return isSuccessful;
      }//end add

public boolean add (int newPosition, Object newEntry)
      {  boolean isSuccessful =true;
          if( (! isFull() && newPosition >=0)&&(newPosition<=length))
            { if (newPosition < length)      // optional
                makeRoom (newPosition);// private method that helps to shift items
                                                                   //in order to make space for the new entry;
              entry [newPosition]=newEntry;
              length++;
            }
           else
            isSuccessful =false;
          return isSuccessful;
     }//end add

private void makeRoom (int newPosition)
       {  for (int index=length; index> newPosition; index--)
             entry [index]=entry[index-1];
       }//end makeRoom

public Object remove (int givenPosition)
      {    Object result=null; // returned value
           if(! isEmpty() &&(givenPosition >=0)&&(givenPosition<length))
              {    result= entry [givenPosition];
                   if (givenPosition <length-1)     // optional
                     removeGap (givenPosition); // private method that helps to shift
                                                                          // items in order to remove space
                                                                          // after removing the entry;
                    length--;
              }
          return result;
       }//end remove

private void removeGap (int givenPosition)
        {  for (int index= givenPosition; index <length-1;index++)
              entry [index]=entry[index+1];
         }//end removeGap

public boolean replace (int givenPosition, Object newEntry)
      { boolean isSuccessful = true;
         if (! isEmpty() &&(givenPosition >= 0) && (givenPosition <  length))
            entry [givenPosition] = newEntry;
        else
           isSuccessful = false;
        return isSuccessful;
       } // end replace

public Object getEntry (int givenPosition)
      {
        Object result = null; //  result to return
        if (! isEmpty() &&(givenPosition >= 0) && (givenPosition < length))
           result = entry [givenPosition];
        return result;
       } // end getEntry

public boolean contains (Object anEntry)
      { boolean found = false;
        for (int index = 0; !found && (index < length); index++){
          if (anEntry.equals (entry [index]))
            found = true;
       } // end for
       return found;
      } // end contains

public int getLength()
      { return length;
       }//end getLength

public void clear()
      {   length=0;
      }

public boolean isEmpty()
      {    boolean itEmpty =false;
           if (length ==0)                        // return length==0;
             itEmpty =true;
           return itEmpty;
       }//end isEmpty

public boolean isFull()
      { boolean itFull =false;           // return length = = entry. length;
        if (length== entry. length)
          itFull =true;
        return itFull;
      }// end isFull

 public void display()
       {  for (int index=0;index< length; index++)
          System.out.println (entry [index]);
        }
}//end of class AList

在应用程序级别..当我将数据存储到我的数组时

static Alist Amman = new Alist();


public static void main(String[] args) {

   blablablablabla

 Amman.add(x);

出现错误说:

Exception in thread "main" java.lang.NullPointerException
        at Cleint.main(Cleint.java:93)

1 个答案:

答案 0 :(得分:0)

您可能正在访问列表的无效位置,请尝试在方法isFull()中使用(entry.length - 1)。此外,您可以使用&gt; =代替==,只是为了安全编程。

public boolean isFull()
  {         
    return length >= (entry.length - 1);
  }