0
String[] strsub0={"out","err","in","console()"};
String[] strsub1={"print","println","append","close","flush"};

i want to get array elements and i know the name of the array.but i know aray name in String

int i=0; //this i get through a loop ,so i can't code strsub0 in my code.

String arrayname="strsub"+i;  //this is array name i want ,but it is a String.

//so i want get all elements inside array which name is strsub+i ; //arrayname.length not return strsub0.length but i want it.

for(int z=0;z<arrayname.length;z++){  

   System.out.println(arrayname[z]);

}
5
  • 1
    What is high level requirement? Something like what you are asking is rarely ever needed Commented May 3, 2014 at 12:42
  • Try making it into array of arrays like: String[][] a = {substr0, substr1,...}; for (int i = 0; i < a.length; ++i) { /* your code */ } Commented May 3, 2014 at 12:42
  • Do you know which arrays you will be using beforehand? Commented May 3, 2014 at 12:43
  • @geoand may be.but i only have this solution . Commented May 3, 2014 at 12:43
  • 1
    It's the XY problem. You probably want to rethink your approach. Commented May 3, 2014 at 12:46

2 Answers 2

2

You want an array of arrays.

String[][] strsub={{"out","err","in","console()"},
                   {"print","println","append","close","flush"};

Then you can get

String[] arr = strsub[i];
Sign up to request clarification or add additional context in comments.

1 Comment

This was the only viable solution I could think of as well - that made any sense at least.
0

You could use a Map whose key is what you want and whose value is the array. Like the following:

Map<String, String[]> map = new HashMap<>(); //(assuming you have Java 7+ for the <> operator)
map.put("strsub0 ", new String[]{"out", "err", "in", "console()"});
map.put("strsub1 ", new String[]{"print", "println", "append", "close", "flush"});

Then retrieve the values like: map.get(arrayname)[i]. Of course you would have to check whether map.get(arrayname) actually exists or is null

Comments

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.