-1

How might I convert an 'ArrayList' object to a 'String[][]' array in Java? I need a two-dimensional String array and not a simple Array

I Have this file

word1 word1.1 word1.2
word2 word2.1 word2.2

And I need to have each word to compare with another file

I tried

public void Scanne(File file) throws Exception {

             Scanner scanner = new Scanner(file);
             ArrayList<String> list = new ArrayList<String>();
             while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    String[] parts = line.split("\t");
                    String part2 = parts[2];
                    String part3 = parts[3];
                    String part4 = parts[4];
                    String part5 = parts[5];
                    String part6 = parts[6];
                    String part7 = parts[7];
                    String part8 = parts[8];
                    String part9 = parts[9];
                    String part10 = parts[10];
                    list.add(line);
                    String resultSet[][]= new String[list.size()][parts.length];
                    for (int i = 0; i<list.size(); i++) {
                    I am stuck here       
                    }
}
'''
Thaliana
  • 3
  • 3
  • 1
    What is the logic? – Youcef LAIDANI Jun 20 '19 at 06:59
  • 2
    @GiampaoloGabba - Except this OP wants a `String[][]`. – T.J. Crowder Jun 20 '19 at 07:00
  • 1
    Why String[][]? – claasic Jun 20 '19 at 07:00
  • Welcome to SO! Please read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Your best bet here is to do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. We also need to understand your goal, which isn't clear from your question. You can help by providing example source data and your expected result, and how you want to calculate it. – T.J. Crowder Jun 20 '19 at 07:00
  • I want to parse each String of the list and put each element in two-dimensional String array – Thaliana Jun 20 '19 at 07:01
  • @Thaliana - That doesn't clarify much, if anything. – T.J. Crowder Jun 20 '19 at 07:02
  • So basically a char[][]? Where each subarray is a String's characters? – claasic Jun 20 '19 at 07:03
  • Almost but i need to parse each word and not char – Thaliana Jun 20 '19 at 07:06
  • Write a proper description. – claasic Jun 20 '19 at 07:08
  • If you need a two-dimensional String array and not a simple Array then add some sample code that you tried or show example of input value and desired output value. – Java-Dev Jun 20 '19 at 07:14
  • if your input is "[word1, word1.1, word1.2, word2, word2.1, word2.2]" as arraylist then what is your desired output should be. provide full description then we may help you. your question doesnot have enough info to get help from us. – Java-Dev Jun 20 '19 at 07:19

2 Answers2

0

Try this code may help you.

   public class ArrayListToStringArray {

    public static void main(String args[]){

        //ArrayList containing string objects
        ArrayList<String> nameList = new ArrayList<String>();
        nameList.add("Max");
        nameList.add("Tom");
        nameList.add("John");

        /*
         * To convert ArrayList containing String elements to String array, use
         * Object[] toArray() method of ArrayList class.
         *
         * Please note that toArray method returns Object array, not String array.
         */

        //First Step: convert ArrayList to an Object array.
        Object[] objNames = nameList.toArray();

        //Second Step: convert Object array to String array

        String[] strNames = Arrays.copyOf(objNames, objNames.length, String[].class);

        System.out.println("ArrayList converted to String array");

        //print elements of String array
        for(int i=0; i < strNames.length; i++){
            System.out.println(strNames[i]);
        }
    }
}
Java-Dev
  • 438
  • 4
  • 20
0

As per OP's comment, the requirement is to split by words wherein each cell of 2d array will store a word and each row will correspond to a sentence.

List<String> l = new ArrayList<>();
l.add("Hello world");
l.add("StackOverflow is awesome");

String[][] s = new String[l.size()][];

for (int i=0; i<l.size(); i++) {
  String[] words = l.get(i).split(" ");
  s[i] = words;
}

for (int i=0; i<s.length; i++) {
  for (int j=0; j<s[i].length; j++) {
    System.out.print(s[i][j] + " ");
  }
  System.out.println();
}
Dhruvil Vaghela
  • 580
  • 6
  • 13