java - Adding Strings to List with the number of a certain letter in them increasing -


so have 2 int's v , w, string q, , list list. need make list adds string duplication of q, ranging v through w.

for example: v 2, w 4, q "a". list should [aa, aaa, aaaa] time program done.

i have done various programs similar this, , felt easy, , still think reason missing something. have attempted solve doing

for (int j = v; j <= w; j++) {     string s = "";     (int k = 0; k < j ; k++) {         s+=q;     }     list.add(s); }  

but gives constant number of qs. not changing.

first, prefer stringbuilder string concatenation (that pollutes intern cache one). , need begin looping v times create initial string; append q. like

int v = 2; int w = 4; string q = "a"; list<string> list = new arraylist<>(); stringbuilder sb = new stringbuilder(); (int = 0; < v; i++) {     sb.append(q); } (int j = v; j <= w; j++) {     list.add(sb.tostring());     sb.append(q); } system.out.println(list); 

output (as requested)

[aa, aaa, aaaa] 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -