Java ArrayList of Arrays?

后端 未结 7 838
情书的邮戳
情书的邮戳 2020-12-05 01:43

I want to create a mutli dimensional array without a fixed size.

I need to be able to add items of String[2] to it.

I have tried looking at:

相关标签:
7条回答
  • 2020-12-05 02:37

    As already answered, you can create an ArrayList of String Arrays as @Péter Török written;

        //Declaration of an ArrayList of String Arrays
        ArrayList<String[]> listOfArrayList = new ArrayList<String[]>();
    

    When assigning different String Arrays to this ArrayList, each String Array's length will be different.

    In the following example, 4 different Array of String added, their lengths are varying.

    String Array #1: len: 3
    String Array #2: len: 1
    String Array #3: len: 4
    String Array #4: len: 2
    

    The Demonstration code is as below;

    import java.util.ArrayList;
    
    public class TestMultiArray {
    
        public static void main(String[] args) {
            //Declaration of an ArrayList of String Arrays
            ArrayList<String[]> listOfArrayList = new ArrayList<String[]>();
    
            //Assignment of 4 different String Arrays with different lengths
            listOfArrayList.add( new String[]{"line1: test String 1","line1: test String 2","line1: test String 3"}  );
            listOfArrayList.add( new String[]{"line2: test String 1"}  );
            listOfArrayList.add( new String[]{"line3: test String 1","line3: test String 2","line3: test String 3", "line3: test String 4"}  );
            listOfArrayList.add( new String[]{"line4: test String 1","line4: test String 2"}  );
    
            // Printing out the ArrayList Contents of String Arrays
            // '$' is used to indicate the String elements of String Arrays
            for( int i = 0; i < listOfArrayList.size(); i++ ) {
                for( int j = 0; j < listOfArrayList.get(i).length; j++ )
                    System.out.printf(" $ " + listOfArrayList.get(i)[j]);
    
                System.out.println();
            }
    
        }
    }
    

    And the output is as follows;

     $ line1: test String 1 $ line1: test String 2 $ line1: test String 3
     $ line2: test String 1
     $ line3: test String 1 $ line3: test String 2 $ line3: test String 3 $ line3: test String 4
     $ line4: test String 1 $ line4: test String 2
    

    Also notify that you can initialize a new Array of Sting as below;

    new String[]{ str1, str2, str3,... }; // Assuming str's are String objects
    

    So this is same with;

    String[] newStringArray = { str1, str2, str3 }; // Assuming str's are String objects
    

    I've written this demonstration just to show that no theArrayList object, all the elements are references to different instantiations of String Arrays, thus the length of each String Arrays are not have to be the same, neither it is important.

    One last note: It will be best practice to use the ArrayList within a List interface, instead of which that you've used in your question.

    It will be better to use the List interface as below;

        //Declaration of an ArrayList of String Arrays
        List<String[]> listOfArrayList = new ArrayList<String[]>();
    
    0 讨论(0)
提交回复
热议问题