Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

How do i represent a String object/variable as an array variable name in java?

I have a function with one argument in it of type int and it includes the position, and I have to return an array matching to that position. E.G. position = 3 I have to return array arr4.

Ok simple I can use a switch statement to easily handle this situation, but the thing is there are a total of 1000 arrays and which all will be called once in a run time, so writing switch will a crazy idea

 public String Convert(String s, int position) {
        //s=s.toLowerCase();
        String result = "";
        String array[]=null;
        String array2[]=null;
        String array3[]=null;
        String space = null;

        switch (position) {
            case 0:
                array = arr1;
                array2 = arr1a;
                array3 = arr1b;
                space = arr1c;
                break;

            case 1:
                array = arr2;
                array2 = arr2a;
                array3 = arr2b;
                space = arr2c;
                break; 
....

so I tried something like this to overcome this problem

 array = "arr"+1;

but it didn't work as I thought and I don't know can I cast a string into an array name or something else.

IS THERE ANY SOLUTION TO SOLVE THIS PROBLEM.

question from:https://stackoverflow.com/questions/65912818/how-do-i-represent-a-string-object-variable-as-an-array-variable-name-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...