Reputation: 47
Is it possible to add objects into an Arraylist through a for-loop?
I would like to shorten down the code! or do you have any other tips on how to create a word quiz with different classes and methods?
I have three classes, Main
that drives the game, Quiz
where the game is constructed, and a class with the Dictionary with all the words.
ArrayList<Sweng> Wordlist = new ArrayList<Sweng>();
Sweng s1 = new Sweng("bil", "car");
Sweng s2 = new Sweng("blå", "blue");
Sweng s3 = new Sweng("baka", "bake");
Sweng s4 = new Sweng("hoppa", "jump");
Sweng s5 = new Sweng("simma", "swim");
Sweng s6 = new Sweng("måne", "moon");
Sweng s7 = new Sweng("väg", "road");
Sweng s8 = new Sweng("snäll", "kind");
Sweng s9 = new Sweng("springa", "run");
Sweng s10 = new Sweng("hus", "house");
Wordlist.add(s1);
Wordlist.add(s2);
Wordlist.add(s3);
Wordlist.add(s4);
Wordlist.add(s5);
Wordlist.add(s6);
Wordlist.add(s7);
Wordlist.add(s8);
Wordlist.add(s9);
Wordlist.add(s10);
Upvotes: 3
Views: 134
Reputation: 16140
Here's an example of how to do this with java streams, though, given your current level in java I wouldn't recommend using this answer unless you're doing this professionally because it's a bit on the advanced side.
import java.util.Arrays;
import static java.util.stream.Collectors.toList;
import java.util.List;
class Sweng {
Sweng(String a, String b) {}
}
public class Main{
public static void main(String[] args){
List<Sweng> swengs = Arrays.stream(new String [][]{
{"bil", "car"},
{"blå", "blue"},
...
}).map(t -> new Sweng(t[0], t[1])).collect(toList());
}
}
You can add new elements to the list where the ellipsis are ('...') and that can be of any length.
Upvotes: 2
Reputation: 18255
Snippet 1
List<Sweng> Wordlist = Arrays.asList(
new Sweng("bil", "car"),
new Sweng("blå", "blue"),
new Sweng("baka", "bake"),
new Sweng("hoppa", "jump"),
new Sweng("simma", "swim")
new Sweng("måne", "moon")
new Sweng("väg", "road")
new Sweng("snäll", "kind")
new Sweng("springa", "run")
new Sweng("hus", "house")
);
Snippet 2
List<Sweng> Wordlist = List.of(
new Sweng("bil", "car"),
// ...
);
Snippet 3 (antipattern) Double Brace initialization
List<Sweng> list = new ArrayList<Sweng>() {{
add(new Sweng("bil", "car"));
// ...
}};
Upvotes: 0
Reputation: 11136
Let's reconsider and contemplate on what you're asking for.
Definitely, it is possible to contract and shorten several lines of the instantiation and adding code, per se, that is:
Sweng s1 = new Sweng("whatever..", "whatever..");
...
Sweng sk = new Sweng("whatever..", "whatever..");
instance.add(s1);
...
instance.add(sk);
but wait a moment.. and pay close attention to how you instantiate objects that you later add into your list.
Where that data comes from? any systematic read-process?
No.
You just have hard-coded whatever comes to your mind (or is asked in your task) and there is no way, any program, in the world, can guess what that data should be.
Alternatively, you can create a simple text file, and write in it your data, with some conventional formatting (let's say, one entry at a line). Then, sure enough, you can read that file and populate respective objects - hence, add them into your list - in just a simple loop construct.
Upvotes: 1
Reputation: 909
You can define two different string arrays, each containing the string data for each field of your Object class:
String [] array1 = {"bil", "blå", "baka", "hoppa", ... };
String [] array2 = {"car", "blue", "bake", "jump", ... };
And then add the Objects to the Wordlist
as follows:
ArrayList<Sweng> Wordlist = new ArrayList<Sweng>();
for (int i=0; i<array1.length; i++)
Wordlist.add(new Sweng(array1[i], array2[i]));
Upvotes: 2
Reputation: 310
Not using a for-loop but this may help you nonetheless.
ArrayList<Sweng> Wordlist = new ArrayList<Sweng>();
Wordlist.add(new Sweng("bil", "car"));
Wordlist.add(new Sweng("blå", "blue"));
Wordlist.add(new Sweng("baka", "bake"));
Wordlist.add(new Sweng("hoppa", "jump"));
Wordlist.add(new Sweng("simma", "swim"));
Wordlist.add(new Sweng("måne", "moon"));
Wordlist.add(new Sweng("väg", "road"));
Wordlist.add(new Sweng("snäll", "kind"));
Wordlist.add(new Sweng("springa", "run"));
Wordlist.add(new Sweng("hus", "house"));
Upvotes: 1