samedi 18 mars 2017

Arrays and random removal of found values

Greetings I have an array with n size where say for example they contain strings a[] = "1","2","3","3","5","6"; The algorithm i used for this is not working as intended not certain why

where the index selected for removal is random but i wish to remove all other same values as well

My attempt at this problem //count is the number of items in array,(global variable) //array is set to 100 by default

// Any help would be greatly appreciated, this problem has been bugging me all //day.

public String findRandom ( ){ Random rand = new Random();

    int randomNumber =   rand.nextInt(((count-1)-front)+1)+front; 

    String wordChosen = items [randomNumber]; 

     int counter=0; 



        for (int k=0;k<count;k++){
            if (wordChosen.equals(k)){
            for (int i=randomNumber-1;i>=0;i--){

            items[i+1] = items[i]; 

        }
        counter = counter-1;

            }
         count = counter; 


        }






    return wordChosen; 
}




Aucun commentaire:

Enregistrer un commentaire