5

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);
2
  • That is odd ... you already know how to access your desired array, but you haven't tried to define it that way? Commented Dec 11, 2014 at 21:24
  • It seems that you have a typo. String[] array1 = new String{"there", "there2", "there3", "there4"}; will not create array. Either remove new String[] leaving only String[] array1 = {your, elemements}; or add type of array explicitly by adding [] like new String[]{"there", "there2", "there3", "there4"};. Commented Dec 11, 2014 at 21:30

7 Answers 7

3

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
        };
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

String[][] arraysTogether = new String[][]{
    array1,
    array2,
    array3,
    array4
};

Comments

2

You can simply do String[][] finalArray = new String[][] {array1, array2, array3, array4};

as below

    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[][] myArray= new String[][] {array1, array2, array3, array4};
    System.out.println(myArray[2][1]);

This prints "hi2"

if you do

        myArray[0][1] --> It would be "there2"
        myArray[1][2] --> It would be "here3"

Comments

2

I corrected your definition so it actually compiles:

String[] array1  = {"there",  "there", "there", "there"};
String[] array2  = {"here","here","here"};
String[] array3  = {"hi","hi"};
String[] array4  = {"blah","blah","blah"};

The peferred method to add the to a list is the built in one, as changes to the array will be reflected by the list.

List<String[]> y = Arrays.asList(array1, array2, array3,array4);

one sidenote: always prfere to use the Interface if you define a variable i.e.

List<String[]> x

instead of

ArrayList<String[]> x

1 Comment

good job please from now then post up your future answer like this so it can be helpful for other who may see your answer.
2

Putting arrays of String to String[][]

First of all, String[] array1 = new String{"there", "there2", "there3", "there4"} will not compile. You were probably thinking about:

String[] array1 = new String[]{"there", "there2", "there3", "there4"};

You can do it shorter way (which I will recommend):

String[] array1 = {"there", "there2", "there3", "there4"};

Now, the answer to your question is:

String[][] arrays = new String[][]{array1, array2, array3, array4};

Or, again - shorter (and recommended):

String[][] arrays = {array1, array2, array3, array4};

According to Java Language Specification 10.3 Array Creation, the longer syntax is an array creation expression and the shorter one is an array initializer. They're not equivalent. (If they were, the designers would probably get rid of one of them - Ockham's razor.) An example where one can use array creation expression, but not array initializer.


Putting arrays of String to ArrayList<String[]>

The closest one to the way you tried:

List<String[]> myArrayList = new ArrayList<String[]>();
myArrayList.add(array1);
myArrayList.add(array2);
myArrayList.add(array3);
myArrayList.add(array4);

The shortest one using Arrays.asList():

List<String[]> myArrayList = Arrays.asList(array1, array2, array3, array4);

And, if you declare array1, array2, array3, array4 as final references, you can use double brace initialization:

List<String[]> myArrayList = new ArrayList<String[]>() {{
    add(array1);
    add(array2);
    add(array3);
    add(array4);
}};

Comments

1

You can do as follows

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

and access just as you said

    public class Test
{
    public static void main(String[] args) {
        String[][] myArray =  new String[][]{{"there",  "there2", "there3", "there4"},
                {"here","here2","here3"},{"hi","hi2"},
                {"blah","blah2","blah3"}};
        System.out.println(myArray[0][1]); // there2
        System.out.println(myArray[1][2]); // here3
    }
}

Note that there's a difference than to

String[][] myArray = new String[][] {
    array1,
    array2,
    array3,
    array4,
};

in that that in the second approach, the changes to an e.g. array1 will apply to myArray as well, so its a mix of static and dynamic initialization, choose what suits your needs

Comments

0

Adding arrays into a list:

List<String[]> list = Arrays.asList(array1, array2, array3, array4);

Adding arrays to a 2D Array:

String[][] array = new String[][] {
    array1,
    array2,
    array3,
    array4,
};

Also, you are not initializing your arrays properly. The way you have it, you are calling the constructor of the String object, not initializing an array so the compiler will give you an error.

Change:

String[] array1  = new String{"there",  "there2", "there3", "there4"};

To (add []):

String[] array1  = new String[]{"there",  "there2", "there3", "there4"};

Or declare the array directly like:

String[] array1  = {"there",  "there2", "there3", "there4"};

Same goes for the other 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.