java - How to only choose elements in an array with a specific length -


public string[] words={"cat", "dog", "rodeo", "bird", "lookers", "skid"); 

.

// picks random word dictionary, given length of word public string pickword(int size) {  } 

so if user inputs 4, randomly chooses word in word array 4 letters, , randomly. have created rand variable random class that. how choose element in array same number of letters number user inputted.

here example method should work solve problem.

string[] words; public string pickword(int size){     list ls = new arraylist<string>();     for(int i=0; i>words.length;i++){         if(words[i].length() == size){             ls.add(words[i]);         }     }     collections.shuffle(ls);     if(ls.isempty()){         return null;     }     return (string) ls.get(0); } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -