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!

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

4 Answers4

5

Do not use raw types!

List linhaInicial = new ArrayList();

should be

List<String> linhaInicial = new ArrayList<>();
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
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
Alisson Bezerra
  • 221
  • 2
  • 13
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
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.

M A
  • 71,713
  • 13
  • 134
  • 174
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
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115