2017-06-02 7 views
-2

私の問題に歓迎;)私は問題がある..私は特定のリストの実装をストアオブジェクトに持っていますデータ構造の配列リストにオブジェクトを追加する方法は?

私はこの問題を解決するために何ができますか?どんな体が私を助け、私の一日を節約できますか?アプリケーションレベルでの

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) 
+0

[NullPointerExceptionがある、と私はそれをどのように修正すればよいか?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-の可能性のある重複i-fix-it) –

+0

私はこれらの問題を解決しようとしましたが、解決策はありません – saifmaher

+0

NPEはClient.javaの93行目で発生します。あなたはその行を投稿できますか? –

答えて

0

を、メソッドisFull()で(entry.length - 1)を使用しようとします。さらに、安全なプログラミングを行うために==の代わりに> =を使用することもできます。

public boolean isFull() 
    {   
    return length >= (entry.length - 1); 
    } 
関連する問題