1

I want to convert lists of lists into two separate string arrays.

I want to convert taglist into string array TagArray[] and tokenlist into array called TokenArray.

I tried many ways but there are many ways to convert Converting an ArrayList into a 2D Array but I cannot find any method to convert Lists of lists to String array. Can anyone help me to do this.

taglist

 [[NON, NON], [NON, NON ], [NON, NON, NON], [NON, NON] ,[ENTITY, ENTITY]]

tokenlist

[[John, ran], [John, jumped], [The, dog, jumped], [Mary, sat], [Finland, India]]

I tried the following way

 for(int i=0;i<tokenlist.size();i++)
    {
        String[] words = tokenlist.get(i);


    }

I am getting the output when i use above way. but the problem is that i have to take i th value from tokenlist and taglist at the same time

OR

I have to convert this into 3 D array which has the following format

  static final String[][][] WORDS_TAGS = new String[][][]

 {

    { { "John", "ran" },                { "NON", "NON" } },

    { { "John", "jumped"},              { "NON", "NON "} },
    { { "The", "dog", "jumped"},        { "NON", "NON", "NON" } },

    { { "Mary", "sat"},                 { "NON", "NON"} },
    { { "Finland","India" },           { "ENTITY","ENTITY" } },

};

2
  • 4
    Can we see one way of your try out of may ways? Commented Aug 1, 2014 at 4:51
  • ok what's the array string you expect? {"John, ran", "John, jumped", "The, dog, jumped", "Mary, sat", "Finland, India"} or what? Commented Aug 1, 2014 at 5:07

3 Answers 3

1

try this

    List<List<String>> l1 = Arrays.asList(Arrays.asList("NON", "NON"),
            Arrays.asList("NON", "NON"),
            Arrays.asList("NON", "NON", "NON"),
            Arrays.asList("NON", "NON"), Arrays.asList("ENTITY", "ENTITY"));
    List<List<String>> l2 = Arrays
            .asList(Arrays.asList("John", "ran"),
                    Arrays.asList("John", "jumped"),
                    Arrays.asList("The", "dog", "jumped"),
                    Arrays.asList("Mary", "sat"),
                    Arrays.asList("Finland", "India"));
    String[][][] a = new String[l1.size()][][];
    for (int i = 0; i < l1.size(); i++) {
        a[i] = new String[][] {
                l2.get(i).toArray(new String[l2.get(i).size()]),
                l1.get(i).toArray(new String[l1.get(i).size()]) };
    }
    System.out.println(Arrays.deepToString(a));

output

[[[John, ran], [NON, NON]], [[John, jumped], [NON, NON]], [[The, dog, jumped], [NON, NON, NON]], [[Mary, sat], [NON, NON]], [[Finland, India], [ENTITY, ENTITY]]]
1
  • what should i mention inside "X" and "Y" Commented Aug 1, 2014 at 5:22
0
   String[] words = new String[20];
   int index = 0;
   for(int i=0;i<L2.size();i++)
   {
      List l5 = (List)L2.get(i);
      for(int j=0;j<l5.size();j++){
      words[index] = (String)l5.get(j);
      index++;
      }
   }

you need two for loop. one for external and another for internal

4
  • it shows error. mt tokenlist is of type List<List<String>> tokenList Commented Aug 1, 2014 at 5:00
  • it shows error in String[] word = tokenlist.get(i); Commented Aug 1, 2014 at 5:07
  • @Rose Sorry for inconvenience, now above code is working fine. I checked It. Commented Aug 1, 2014 at 5:23
  • Let us continue this discussion in chat. Commented Aug 1, 2014 at 5:25
0

Try this

    String[][][] newArray = new String[taglist.size()][2][];
    for(int i=0;i<taglist.size();i++){
       String[] arr=tokenlist.get(i).toArray(new String[tokenlist.get(i).size()]);
       newArray[i][0]= arr;
       arr= taglist.get(i).toArray(new String[taglist.get(i).size()]);
       newArray[i][1]= arr;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.