java - How can I invoke this method with an array? Is it possible to do? -
i've been given array use within class assignment.
/** * compacthand - method supplied you! * re-order array cards occupy indices, i.e. 0,1,2,... * , nulls in higher indices. * count number of cards in array , set ncard accordingly. * example: if array [kclub][null][2diamond][null][7spade] * routine sets array [kclub][2diamond][7spade][null][null] , sets ncard 3. * note relative order of cards not changed. */ private void compacthand(){ int nc=0, ix=0, nullix=-1; // find first null do{ if (cards[ix]==null){ nullix = ix; break; } } while (++ix<cards.length); if (nullix==-1){ // there no nulls. set ncards , return. ncard = cards.length; return; } nc = nullix; // loop end of array , swap cards nulls for(ix=nullix; ix<cards.length; ix++) if (!(cards[ix]==null)){ cards[nullix++] = cards[ix]; cards[ix] = null; nc++; } ncard = nc; }
i've completed majority of assignment, 1 of methods need invoke within on array named cards. i've tried invoking using:
cards.compacthand();
the method doesn't accept arrays argument, in method need complete, instructions state use it.
/** * delete cards @ specified indices , compact array. * if indices not correspond cards array must unchanged * , member returns false. * otherwise specified cards deleted, array compacted , true returned. * @param index index of array element delete * @return true if elements deleted, false if none deleted. */
the method marked private, can access since it's in same class.
any appreciated.
the cards
array should instance variable in class. instance variables declared outside methods of class, @ beginning of it. these can called method of class. this answer has examples instance variables.
Comments
Post a Comment