0

I want to know if it possible to handle an array in another array, i never heard of something like that and i'm curious if it is possible, because i'm writing a little programm rigth now and i would need something like this! An example(maybe this can be solved other than using an array in an array): i have two String arrays like this:

public String[] stringArray1 = { "0", "1", "2", "3" }, stringArray2 = { "0", "1", "2", "3" };

now i would need something like this:

public /*type?*/[] allArrays = { stringArray1, stringArray2 };

because i need to access it using an for loop and i dont want to use thousands of if statements to get to the stringArrays(this is just an example in my real programm there are like 100 arrays):

for(int i = 0; i < numberOfArrays/*100*/; i+=2)
test/*a rondom void*/(allArrays[i], allArrays[i+1]);

in my programm there are always two arrays connected that's why i call one with i and one with i+1... so now this is how my test looks like:

public void test(/*type?*/ test1, /*type?*/ test2)
{
//now i need to use test1 & test2 as string to f.e. like this:
if(test1[2].contains("1"))
//do something
}
2
  • 3
    String[][], a two dimensional array, is actually an array containing arrays. Commented Mar 1, 2016 at 14:41
  • 2
    Do you mean String[][], i.e. a 2D array? Commented Mar 1, 2016 at 14:41

2 Answers 2

2
String[][] arrays = new String[][] { stringarray1, stringarray2, stringarray3 };
Sign up to request clarification or add additional context in comments.

2 Comments

if you want your code to be dynamic try using arraylist ArrayList<ArrayList<String>> list1 = new ArrayList<ArrayList<String>>(); ArrayList<String> listoflists = new ArrayList<String>(); nodes.add(listoflists);
exactly what i've looked for! thanks :)
2

This is a duplicate. You need an array of arrays.

public String[][] allArrays = { stringArray1, stringArray2 };

4 Comments

You admit it's a duplicate, so why answer and not just mark as dup?
@Idos I don't think I have enough SO reputation to mark it as duplicate.
flag as duplicate :) (I didn't downvote, just to be clear)
@Idos I just did, thank you :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.