3

I dont know how to do this. But what i want is to create a two array.. one for the array class then the other is for the information that i will use for the selected class using for loop. I prefer for loop that for each loop.. ^_^.. My question is. Is it posible to create an array in which it will store an array too? for example:

private final Class<?>[] cls = {class1,class2,class3};
private final String[] myFirstArray = {array1[],array2[],array[]3};
private final String selectedarray[];

for(int i=0;i<cls.lenght();i++){
if(myArrayClassParameter == cls[i]){
selectedArray[] = myFirstArray[i];
}
}

Like that?

Well If it is posible my work will be less time consuming.. thanks.

2 Answers 2

6

Absolutely - you can create arrays of arrays, or even arrays of arrays of arrays, and so on. All you need is to add more pairs of square brackets [].

private final String[][] firstArray = new String[][] {
    new String[] {"quick", "brown", "fox"}
,   new String[] {"jumps", "over", "the"}
,   new String[] {"lazy", "dog", "!"}
};

String[] selectedArray = firstArray[1];
Sign up to request clarification or add additional context in comments.

4 Comments

BTW Can i put three[]? hehe.. Awesome
@thenewbie Absolutely - as many as you need, Java does not have any specific limit.
@thenewbie You should know that these are available in almost every programming language as well. They're very useful.
@Tushar Dhoot i think visual basic dont have that
2

Yes. These are called multidimensional arrays. Try them out and mess around with them. You'll learn better that way.

You must declare them like this:

Type[] smallerArray1;
Type[] smallerArray2;
Type[] smallerArray3;
Type[][] biggerArray = new Type[][]{smallerArray1, smallerArray2, smallerArray3};

Then you can use them like regular arrays.

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.