How to Convert/add String Array[] elements into List<String> [] Array

JavaLearn :

I have three arrays of String

String array1 [] = {"A", "B", "C"};
String array2 [] = {"D", "E"};
String array3 [] = {"F", "G", "H", "I"};

I want to store all this arrays into List of String array.

List<String> allArrays[]

My output List allArrays[] should contain

allArrays[0]= [A,B,C]
allArrays[1]= [D,E]
allArrays[2]= [F,G,H,I]

I have tried below piece of code but getting null pointer exception

String array1 [] = {"A", "B", "C"};
String array2 [] = {"D", "E"};
String array3 [] = {"F", "G", "H", "I"};

List<String> allArrays[] =null;

allArrays[0].addAll(Arrays.asList(array1));
//Null pointer exception here
allArrays[1].addAll(Arrays.asList(array2));
allArrays[2].addAll(Arrays.asList(array3));

System.out.println(b[0]);
System.out.println(b[1]);
System.out.println(b[2]);
Majed Badawi :

You are not creating and adding to the list correctly. Since you need an array of List<String>, you can do this:

String array1 [] = {"A", "B", "C"};
String array2 [] = {"D", "E"};
String array3 [] = {"F", "G", "H", "I"};
List<String>[] allArrays = new ArrayList[3]; 

List<String> list = new ArrayList<String>();
Collections.addAll(list, array1);
allArrays[0] = list;
list = new ArrayList<String>();
Collections.addAll(list, array2);
allArrays[1] = list;
list = new ArrayList<String>();
Collections.addAll(list, array3);
allArrays[2] = list;
        
System.out.println(allArrays[0]);
System.out.println(allArrays[1]);
System.out.println(allArrays[2]);

Output:

[A, B, C]
[D, E]
[F, G, H, I]

A better way would be using List<List<String>>:

String array1 [] = {"A", "B", "C"};
String array2 [] = {"D", "E"};
String array3 [] = {"F", "G", "H", "I"};
List<List<String>> allArrays = new ArrayList<>(); 

allArrays.add(Arrays.asList(array1));
allArrays.add(Arrays.asList(array2));
allArrays.add(Arrays.asList(array3));
                
System.out.println(allArrays);

Output:

[[A, B, C], [D, E], [F, G, H, I]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to compare elements inside List of strings with Array List of string?

How to put List elements to String Array in Java

How to dynamically add elements to String array?

Concatenating elements in an array to a string

How to add elements of a string array to a string array list?

How to remove all elements in String array in java?

How to Determine Frequency of String Array List Elements Using Classes in Java

How to move array elements to top if compared with string?

How to concatenate array of string elements into a string

How to tell if a mixed array contains string elements?

Replace string with array elements

Comparing the list of elements and string array

How To Extract elements from a list of objects and store in String array

How to match a String in array of elements in MongoDB

Python - adding string elements from a list into an array

Split string into Elements of Array

php how to split a string into array elements?

C# How to best add elements of string array to generic List

How to transform Array[(String,List[String])] to Array[(String,String)]

How to fetch array of string elements with SwiftyJSON?

how to sort a string array based on elements of another List

How to convert string into list of array

Join elements of array into a string

How to put elements into String array?

How to make a list a string and then an array

How to replace occurrences of a string by elements of an array in javascript?

How to access the string array elements for search?

how to check if a string includes all array elements

How to create a new string array out of the elements of given string array?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive