Declaring a LinkedList in Java with elements of Array

I would like to declare a LinkedList in Java whose elements are a type of array and this array is has two elements of String. I do not know how to declare. For example, If you want to declare a LinkedList of String, it would be LinkedList s=new LinkedList(); For my problem, I guess such things.
LinkedList<String[2]> s = new LinkedList(); (However, It doesn’t work!).

>Solution :

When you declare an array of strings where you want 2 strings, you would do

String[] strings = new String[2];

Notice how the type is String[], not String[2].

So to declare a LinkedList that holds String arrays, you would do

LinkedList<String[]> list = new LinkedList<>();

When adding to this list, you can have an unspoken rule where you only add arrays of length 2, there is no built in way to prevent say an array of length 3 to be added. To ensure only length 2 arrays are added, you would have to check them before adding them

String[] stringArray = new String[x];
if(stringArray.length == 2)

Leave a Reply