Java - ArrayList default initial values -


when create arraylist of type integer in java default values? need check if arraylist full , going size of array value @ last index , check if default value.

is there better way? default value?

hope makes sense. cheers

int size = a.size(); int last = a.get(size); if( last == null ) {     return true; }else{     return false; } 

edit;

is possible create arraylist max size can not go on stop dynamically expanding? when create arraylist , use size() return actual size or amount of elements in arraylist? when doing create max size default values null?

public boolean isfull() {     int size = a.size();     int last = 0;     try{         last = a.get(size-1);     }catch (exception e){     }     if( last == null )     {         return true;     }else{         return false;     } } 

i have this, how look? make sense now?

when declare arraylist empty. dynamic container meaning grow ask if "full" more of constraint you'd need add code.

so, if want achieve goal describe.

list<integer> list = new arraylist<integer>(); int max_elements = 5; //set want constrain datatype  public boolean addelement(integer value) {    if (list.size() < max_elements) {      list.add(value);      return true;    } else {      return false;    } }  public boolean isfull() {   return (list.size() == max_elements); }  public integer getlast() {   if (!list.isempty())     return list.get(list.size()-1);   else     return null; } 

as others have stated though, if generate list preset size such:

list<integer> list = new arraylist<integer>(10); 

you'd have list of 10 elements large being null in value. should add additional elements list still grow larger 10 elements unless constrain did above.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -