简体   繁体   English

java.lang.Object []无法转换为java.lang.String []

[英]java.lang.Object[] cannot be converted to java.lang.String[]

public class Mass_Reading
{
   public static void main(String args[]) throws Exception
   {
     CSVReader reader = new CSVReader(new FileReader("Test_Graphs.csv"));

     List myEntries = reader.readAll();
     //System.out.println(myEntries);

     myEntries = new ArrayList<String>(myEntries);
     //System.out.println(myEntries);

     Object[] data = myEntries.toArray();
     System.out.println(data[0] + "");
     }
}

I'm trying to read a .csv file in and get the data into a workable data type. 我正在尝试读取.csv文件,并将数据转换为可行的数据类型。 When read in it starts as a List of String Objects. 当读入时,它作为字符串对象列表开始。 In my code above I'm trying to get it to an array of Strings. 在上面的代码中,我试图将其添加到字符串数组中。 My main hangup is getting the String Object to a String Literal. 我的主要困扰是将String对象转换为String Literal。 I keep getting the error java.lang.Object[] cannot be converted to java.lang.String[]. 我不断收到错误java.lang.Object []无法转换为java.lang.String []。 The code above compiles, but still does not achieve my goal. 上面的代码可以编译,但仍达不到我的目标。 When it prints I still get a String Object. 当它打印时,我仍然得到一个字符串对象。 Thanks for any suggestions. 感谢您的任何建议。

Use this code: 使用此代码:

String[] data = myEntries.toArray(new String[myEntries.size()]);

The method that you call, myEntries.toArray() , always returns an Object[] , which cannot be cast to a String[] . 您调用的方法myEntries.toArray()始终返回Object[] ,而该Object[]不能转换为String[]

But there is an overloaded method that I call in the example above, in which you can pass in the right type of array as an argument. 但是在上面的示例中有一个重载的方法,您可以在其中传递正确类型的数组作为参数。 The toArray() method will then fill the passed in array if it is of sufficient size. 然后,如果toArray()方法的大小足够大,它将填充它。

If it is not of sufficient size, it will create a new array of the right type (the same type as the argument that you passed in) 如果没有足够的大小,它将创建一个正确类型的新数组(与您传入的参数类型相同)

Because of that, you can also write: 因此,您还可以编写:

String[] data = myEntries.toArray(new String[0]);

Some people prefer that, but I don't like the fact that this creates one wasted object, although in a real application this won't make a difference in performance. 有些人喜欢这样做,但是我不喜欢这样一个事实,即会创建一个浪费的对象,尽管在实际的应用程序中这不会影响性能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不兼容的类型:java.lang.Object无法转换为java.lang.String - incompatible types: java.lang.Object cannot be converted to java.lang.String 不兼容的类型:java.lang.Object无法转换为java.lang.String吗? - incompatible types: java.lang.Object cannot be converted to java.lang.String? (java.lang.String)不能应用于(java.lang.Object) - (java.lang.String) cannot be applied to (java.lang.Object) Java explicit casting on list.get(0) (incompatible types: java.lang.Object cannot be converted to java.lang.String) - Java explicit casting on list.get(0) (incompatible types: java.lang.Object cannot be converted to java.lang.String) java.lang.Object无法转换为int - java.lang.Object cannot be converted to int 如何解决此错误java.lang.Object []无法转换为java.lang.String []? - How would i fix this Error java.lang.Object[] cannot be cast to java.lang.String[]? ClassCastException:java.lang.Object []无法强制转换为java.lang.String [] android - ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[] android ClassCastException:java.lang.Object []无法转换为java.lang.String [] [] - ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[][] Java 和 object 中的堆栈无法转换为 java.lang.String - Stack in Java and object cannot be converted to java.lang.String AsyncTask:ClassCastException:java.lang.Object []无法强制转换为java.lang.String [] - AsyncTask: ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM