Thursday 15 April 2010

Accesing arraylist property from another class using constructor -



Accesing arraylist property from another class using constructor -

so have class makes array list me , need access in class through constructor don't know set constructor because methods in class manipulating list. im either getting null pointer exception or out of bounds exception. ive tried leaving constructor empty dosent seem help. in advance. show code professor strict on academic dishonesty cant sorry if makes hard.

you confusing main question, potential solution.

main question:

i have class arraylistownerclass enclosed arraylist property or field. how should class arraylistfriendclass access property.

potential solution:

should pass arraylist arraylistownerclass arraylistfriendclass, in arraylistfriendclass constructor ?

it depends on sec class arraylist.

instead of passing list thru constructor, may add together functions read or change, public, elements of hidden internal arraylist.

note: did not specify programming language. i'll utilize c#, altought java, c++, or similar o.o.p. used, instead.

public class arraylistownerclass { protected int f_length; protected arraylist f_list; public arraylistownerclass(int alength) { this.f_length = alength; this.f_list = new arraylist(alength); // ... } // arraylistownerclass(...) public int length() { homecoming this.f_length; } // int length(...) public object getat(int aindex) { homecoming this.f_list[aindex]; } // object getat(...) public void setat(int aindex, object avalue) { this.f_list[aindex] = avalue; } // void setat(...) public void dootherstuff() { // ... } // void dootherstuff(...) // ... } // class arraylistownerclass public class arraylistfriendclass { public void usearraylist(arraylistownerclass alistowner) { bool cancontinue = (alistowner != null) && (alistowner.length() > 0); if (cancontinue) { int aitem = alistowner.getat(5); dosomethingwith(item); } // if (cancontinue) } // void usearraylist(...) public void alsodoesotherstuff() { // ... } // void alsodoesotherstuff(...) // ... } // class arraylistfriendclass

note, utilize indexed property.

arraylist constructor

No comments:

Post a Comment