0

Hello and sorry for the noobish question. I have done a lot of search about my problem but i found nothing related to my problem.

So i have something like this:

public static String something [][] = 
{
    {"1","100"},
    {"2","1000"},
    {"3","10000"}
};

public static String somethingelse [][] = 
{
    {"1","100"},
    {"2","1000"},
    {"3","10000"}
};

public static String CATEGORIES[][][] = 
{
    something,
    somethingelse
};

How and if it is possible ofcourse can i return from the CATEGORIES[something].get(1st line) line?

I dont want to use something[0] I want to use something like this CATEGORIES.something[0] i dont know how else to explain it.

Thanks in advance!

3
  • If you tag the language, you might get help a little quicker. Commented Apr 9, 2016 at 21:51
  • Sorry forgot to mention the language is Java Commented Apr 9, 2016 at 22:21
  • 1
    Define a class with two fields: something and somethingElse. docs.oracle.com/javase/tutorial/java/javaOO Commented Apr 9, 2016 at 22:24

2 Answers 2

1

You can (but it is an awful practice) with this:

public static class Categories {
    public String something [][] = {
        {"1","100"},
        {"2","1000"},
        {"3","10000"}
    };

    public String somethingelse [][] = {
        {"1","100"},
        {"2","1000"},
        {"3","10000"}
    };
}

public static final Categories CATEGORIES = new Categories();
Sign up to request clarification or add additional context in comments.

8 Comments

You create a new instance from a static class?
What is awful is to make the fields public, not to create a class with fields holding state. The class itself could be private to make is less awful.
@Bálint are you thinking in abstract class? It's a public nested static class. Of course you can create a new instance.
@JBNizet can you call CATEGORIES.something[0] in other class?
No, you can't. I made the incorrect assumption that the constant was private.
|
1

You could use a HashMap with the key-value pairs as the name of the list and an arraylist like this:

HashMap<String, ArrayList<String>> c = new HashMap<>();
ArrayList<String> s = new ArrayList<>();
s.put("HI");
s.put("HELLO");
ArrayList<String> s2 = nee ArrayList<>();
s2.put("HI Again!");
s2.put("Hello Again");
c.put("something", s);
c.put("something2", s2);

Then you can access them as

c.get("something").get(0);

Note: Sorry for the inconvenient variable names, I'm on a phone.

3 Comments

Thanks for your answer but it looks a bit confusing
@HellDev check out oracle's tutorial on collections docs.oracle.com/javase/tutorial/collections
Thanks for the explanation mate i will check your code tommorow to see if it does what i want and will reply you. Thanks again!

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.