gtludwig
gtludwig

Reputation: 5611

how to return different arrays?

I have in my Java code these arrays:

private String[] arrayT1V1;
private String[] arrayT1V2;
private String[] arrayT1V3;
private String[] arrayT2V1;
private String[] arrayT2V2;
private String[] arrayT2V3;

Is there a way to change the String[] I'm addressing programatically? I tried something like the code bellow, which I know it's wrong! I can't have two different objects with the same name. I posted it just to better explain what I want.

private String[] createStringArrays(int type, int value, int size) {
        for (int i = 1; i == type; i++) {
            for (int j = 1; j == value; j++) {
                String arrayName = "arrayT" + type + "V" + value;
                String[] arrayName = new String[size];
            }
        }
    }

Upvotes: 1

Views: 78

Answers (3)

Gondim
Gondim

Reputation: 3048

You could maybe use a Map to store your Arrays, and get them with their name, like:

Map<String, String[]> mapArrays = new HashMap<String, String[]>();


String arrayName = "arrayT" + type + "V" + value;
mapArrays.put(arrayName, new String[size]);

To get them you'd use a

String[] someArray = mapArrays.get(arrayName);

Upvotes: 3

user284291
user284291

Reputation:

Use :

private String[][] arrayTV;

Upvotes: 0

Sjoerd
Sjoerd

Reputation: 75588

Use an array of arrays, so that you can use arrayTV[2][3] instead of arrayT2V3.

Upvotes: 4

Related Questions