2

I did not understand other answers in other threads, so here goes this question:

How do I import an array of strings from one class to another? The list is quite long, which makes it look bad all in one class with the methods.

This is how it looks:

List<String> countriesList = new ArrayList<>(
    Arrays.asList("Japan", 
                  "Sweden", //Etc...

I also have another that is connected to it (it's a guess country and city game and the country is randomized by math.random).

List<String> huvudList = new ArrayList<>(
    Arrays.asList(
        "Tokyo",
        "Stockholm", //Etc...

So how do I put these lists in another class, import them to my main class and use them normally (So I am still able to randomize a random value to the list, etc..). Are there any pretty ways to do this or should I stick with what I got?

This is how I began the class in which I will put the lists:

Why do I get an error when doing this? In what way is it wrong? (ListaLandQuiz is the class and countriesList is the list). It says The method countriesList(int) is undefined for the type ListaLandQuiz

  package Games;
    import java.util.List;
    import java.util.ArrayList;
    import java.util.Collections;


public class Experiments {

    public static void main(String[] args) {
        // Importing classes

        System.out.println(ListaLandQuiz.countriesList(3));

    }

}

Thanks in advance.

1
  • Where do you have the method to generate random country name? Commented Sep 12, 2015 at 10:07

2 Answers 2

5

Assuming you want the lists to be shared and immutable, just make them public and static:

public final class MyLists {

    public static final List<String> COUNTRIES = Collections.unmodifiableList(Arrays.asList(
            "Japan", 
            "Sweden",
            //Etc..
    ));

    public static final List<String> CAPITALS = Collections.unmodifiableList(Arrays.asList(
            "Tokyo",
            "Stockholm",
            //Etc...
    ));

}

You can now access them anywhere as:

MyLists.COUNTRIES
Sign up to request clarification or add additional context in comments.

5 Comments

I get an error saying that I must have public static void main(String[] args). And there is a red line under "Collections". Any ideas?
@Ainvox You need to add an import statement for the appropriate class (java.util.Collections). This is basic Java, you might want to follow some tutorials or buy a good book for Java beginners.
@MarkRotteveel I'm sorry, I have never used lists nor collections. Please refer to my latest edit and tell me if you can find what is wrong with my code at this point.
@Ainvox Please look at the API doc of List and the Java collections tutorial. You seem to want the fourth item from the list, that means you need to use the get(int index) method, as in : ListaLandQuiz.countriesList.get(3) (assuming countriesList is both public and static).
@MarkRotteveel Thank you, sir! Problem is now solved!
0

You can use a dependency injection:

public class Demo{
  List<String> list;

  public Demo(List<String> list){
   this.list = list;
  }
}

1 Comment

Or use static lists as fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.