java - modify an array list -


i've been struggling modify array list passed method. can modify array list without creating new array list in method.

here problem:

write method stutter takes arraylist of strings , integer k parameters , replaces every string k copies of string. example, if list stores values ["how", "are", "you?"] before method called , k 4, should store values ["how", "how", "how", "how", "are", "are", "are", "are", "you?", "you?", "you?", "you?"] after method finishes executing. if k 0 or negative, list should empty after call.

one way creating new array list:

public static void stutter(arraylist<string> list, int k) {      arraylist<string> = new arraylist<string>();      string s = "";      if(k > 0) {          for(int = 0; < list.size(); i++) {              s = list.get(i);              for(int j = 0; j < k; j++) {                 a.add(s);              }          }      }  } 

what 1 way modify arraylist<string> list without creating new array list? thanks

public static void additem(list<string> list, int k){     int size = list.size() * k;      (int i=0; i<size; i=i+k){         string s = list.get(i);         (int j=0; j<k-1; j++){             list.add(i+j, s);         }     } } 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -