0

i've just recently started with java on my school, and i have a bit of a problem. Well not really a problem, since i've found a way to solve it (complicated one though).

im supposed to make a poemgenerator.

so, in a for loop, i want to build a string, of words that is in some arrayLists, so im using a StringBuilder for that.

the thing is, that my sentence should consists of words from 4 different arrayLists, (different wordclasses)

and, a while ago, i programmed in actionscript3, and there was something there that i could really use for this problem, that would make the code so much simplier. using This[]

so. i have 4 lists: list0, list1, list2, list3

and in my loop, i would've done something like the following in AS3(flash, actionscript3)

for(int i = 0; i<4; i++){
    poem.append(This["list" + i].get(randomindex))
}

so that the first time the loop goes, i get a random word from list0, the second time, i get a random word from list1, and so on... is there any way to do this simple in java? x)

3 Answers3

3

You can use array;

List<String> list1 = Arrays.asList("String1", "String2");
List<String> list2 = Arrays.asList("String3", "String4");
List<String> list3 = Arrays.asList("String5", "String6");
List<String> list3 = Arrays.asList("String7", "String8");
List<String>[] lists = new List[]{ list1, list2, list3, list4};

Then replace your code with

for(int i = 0; i<4; i++){
    poem.append(lists[i].get(randomindex));
}
talex
  • 17,973
  • 3
  • 29
  • 66
  • I can't seem to make this work, but im sure im doing something wrong... as i said, im quite new to java x) should i change my wordlists : list0, list1 and so on to arrays instead? or can i keep them as ArrayLists? – Ketil Gustavsen Sep 25 '14 at 14:24
  • as far as i understood, i need to do this : List[] lists = new List[]{list0, list1, list2, list3}; but then i get the error : Cannot create a generic array of List – Ketil Gustavsen Sep 25 '14 at 15:00
  • `List list1 = Arrays.asList(); List list2 = Arrays.asList(); List list3 = Arrays.asList(); List list4 = Arrays.asList(); List[] lists = new List[]{list1, list2, list3, list4};` i still get same error – Ketil Gustavsen Sep 25 '14 at 15:57
  • I get it. it is impossible create generic array. Fixed. – talex Sep 25 '14 at 16:35
  • i Fixed it! but you clearly helped me alon the way! :) thx :P just the names of them are made so they fit in my program in my language x) `ArrayList ordListe = new ArrayList(); ArrayList adjektivListe = new ArrayList(); ArrayList artikkelListe = new ArrayList(); ArrayList substantivListe = new ArrayList(); ArrayList verbListe = new ArrayList(); List[] lists = new List[]{verbListe, artikkelListe, adjektivListe, substantivListe, verbListe};` – Ketil Gustavsen Sep 25 '14 at 17:31
1

Make a fullList : List<List<String>> contains your list0-3

Random r = new Random();
StringBuildr poem = new StringBuilder();
for (List<String> list : fullList)
    poem.append(list.get(r.nextInt(list.size())));

poem.toString(); //this is your poem

codes are not written in IDE, not tested either.

Kent
  • 189,393
  • 32
  • 233
  • 301
0

You can use a two dimensional array see here

list[0] = {"firstWordA","firstWordB"};
list[1] = {"secondWordA","secondWordB"};
list[2] = {"thirdWordA","thirdWordB"};
list[3] = {"fourthWordA","fourthWordB"};

for(int i = 0; i<4; i++){
    poem.append(list[i][randomindex])
}
Community
  • 1
  • 1
Robert
  • 1,286
  • 1
  • 17
  • 37