Java – print a string from ArrayList of string []?

I have an ArrayList of a complete string array. I build it as follows:

String[] words = new String[tokens.length];

My ArrayList has three arrays as shown above:

surroundingWords.add(words);
surroundingWords.add(words1);
surroundingWords.add(words2);

wait

Now, if I want to print out the elements in the string array in aroundwords... I can't The closest thing I can show the contents of the string [] array is their addresses:

[Ljava.lang.String;@1888759
[Ljava.lang.String;@6e1408
[Ljava.lang.String;@e53108

I tried many different versions of things that seem to be the same. My last attempt was:

for (int i = 0; i < surroudingWords.size(); i++) {
        String[] strings = surroundingWords.get(i);
        for (int j = 0; j < strings.length; j++) {
            System.out.print(strings[j] + " ");
        }
        System.out.println();
    }

Due to incompatible types, I can't understand:

found   : java.lang.Object
required: java.lang.String[]
                String[] strings = surroundingWords.get(i);
                                                       ^

help!

I have tried the solution here: print and access list

Solution

Convert object to string []:

String [] strings =(String [])aroundWords. get(i);

Or use parameterized ArrayList:

ArrayList < string [] gt; surroundingWords = new ArrayList< String []>();

Then you don't have to convert the return value from get ()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>