Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
    list.add(stringArray);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading