1

I have checked the following stackoverflow source to solve my problem:

StackOverflow link

but, when applying the top solution, I still get an error, as follows:

My list is defined here:

List linhaInicial = new ArrayList();

My convertion is made here:

String[] convertido = linhaInicial.toArray(new String[linhaInicial.size()]);

The problem is I still get the following error:

Error:(86, 59) java: incompatible types
required: java.lang.String[]
found:    java.lang.Object[]

My conversion is somehow still returning Object[] when it should now be returning String[].

Any solutions?

Thanks!

4
  • 4
    You know that compiler warning you ignored? The one about raw types? Maybe you shouldn't have ignored it... Commented Jul 8, 2014 at 12:25
  • Also do not use linhaInicial.toArray(new String[linhaInicial.size()]); just linhaInicial.toArray(new String[]); Commented Jul 8, 2014 at 12:57
  • Why this, Eypros? On the other topic, somebody said that sending the correct size saves one instantiation, isnt's it correct? Commented Jul 8, 2014 at 13:01
  • @Allison: You are completely right. Eypros' code doesn't even compile and is not a good suggestion. Commented Jul 8, 2014 at 13:39

4 Answers 4

5

Do not use raw types!

List linhaInicial = new ArrayList();

should be

List<String> linhaInicial = new ArrayList<>();
2

Change :

List linhaInicial = new ArrayList(); // can contain any Object. 
             //So compiler throws an error while converting an Object to String.

to

List<String> linhaInicial = new ArrayList<String>(); // using Generics ensures
                          //that your List can contain only Strings, so, compiler
                            //will not throw "incompatible types" error
1

Always try to include the type of the list element in your declaration:

List<String> linhaInicial = new ArrayList<String>();

Avoid raw types as much as possible.

1

You have to change

List linhaInicial = new ArrayList(); // you are using raw type List

To

List<String> linhaInicial = new ArrayList();// you have to use String type List

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.