How to put String arrays into an array of String 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 them in an array of arrays that will look something like this:

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

And then he will be able to access the element in it like this:

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

Hope this makes sense, how can I do this?

I tried to make an ArrayList this way and then add them, but it doesn't work

 ArrayList<String[]> myArrayList = new ArrayList<String[]>(); myArrayList.add(myArray); 
+5
source share
7 answers

It's simple. Check the link for more information on creating, initializing, and accessing the 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 }; 
+3
source

I adjusted your definition so that it really compiles:

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

The peferred method for adding to the list is built into one, since changes in the array will be displayed in the list.

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

one sidenote: always use an interface if you define an ie variable

 List<String[]> x 

instead

 ArrayList<String[]> x 
+2
source

Try the following:

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

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]); 

Prints "hi2"

if you do

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

Arrays from String to String[][]

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

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

You can make it shorter (which I recommend):

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

Now the answer to your question:

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

Or, again, shorter (and recommended):

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

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


Arrays from String to ArrayList<String[]>

Closest to how you tried:

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

Shortest 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 binding initialization :

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

You can do the following:

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

and access 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 } } 

Please note that there is a difference than

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

in the sense that in the second approach, changes to, for example, array1 will be applied to myArray , so its combination of static and dynamic initialization, choose what suits your needs

+1
source

Adding arrays to the 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. As you do this, you call the constructor of the String object, rather than initializing the array so that the compiler gives you an error.

Edit:

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

To (add [] ):

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

Or declare an array directly as follows:

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

The same applies to other arrays.

0
source

Source: https://habr.com/ru/post/1208849/


All Articles