Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put String arrays into an array of String arrays?

Tags:

java

arrays

I have 4 string arrays like this:

String[] array1  = new String{"there",  "there2", "there3", "there4"};
String[] array2  = new String{"here","here2","here3"};
String[] array3  = new String{"hi","hi2"};
String[] array4  = new String{"blah","blah2","blah3"};

And I want to put these into an array of arrays that would look something like this:

   Array myArray =  [{"there",  "there2", "there3", "there4"},
                     {"here","here2","here3"},{"hi","hi2"},
                     {"blah","blah2","blah3"}];

And then would be able to access element in it somewhat like this:

myArray[0][1] would equal "there2"
myArray[1][2] would equal "here3"

Hope that makes sense, how could I go about doing this?

I have tried making an ArrayList like this and then adding them but it doesn't seem to work

ArrayList<String[]> myArrayList = new ArrayList<String[]>();
myArrayList.add(myArray);
like image 247
iqueqiorio Avatar asked Dec 11 '14 21:12

iqueqiorio


1 Answers

It's simple. Check this link for more information about Creating, Initializing, and Accessing an Array.

String[] array1 = new String[]{"there", "there2", "there3", "there4"};
        String[] array2 = new String[]{"here", "here2", "here3"};
        String[] array3 = new String[]{"hi", "hi2"};
        String[] array4 = new String[]{"blah", "blah2", "blah3"};

        String[][] allArray = {
                array1,
                array2,
                array3,
                array4
        };
like image 169
Ronnie Kumar Avatar answered Oct 04 '22 03:10

Ronnie Kumar