使用 ArrayList 时出现类型错误?
package mp1similar;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import EarthquakeRecord.Earthquakerecd;
public class MP1Similar
{
private static ArrayList arrayList ;
public static void main(String[] args)
{
ArrayList arrayList= null;
try
{
BufferedReader br = new BufferedReader(new FileReader("data/Catalog.txt"));
String line="";
arrayList =new ArrayList();
while((line = br.readLine())!=null)
{
// System.out.println(line);
StringTokenizer st = new StringTokenizer(line);
while(st.hasMoreTokens())
{
//System.out.println(st.nextToken());
arrayList.add(st.nextToken());
//System.out.println(br.readLine());
}
}
}
catch (FileNotFoundException ex)
{
System.out.println(ex.getMessage());
ex.printStackTrace();
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
ex.printStackTrace();
}
int j=0;
Earthquakerecd E[]= new Earthquakerecd[2000];
for(int i=0;i< arrayList.size();i++)
{
System.out.println(arrayList.get(i));
E[j] = new Earthquakerecd();
E[j].setDate(arrayList.get(i));
if (j>35 )
{
j=0;
}
j++;
}
}
}
我在行 E[j].setDate(arrayList.get(i));
中收到错误,它表示实际参数无法转换为 java.lang.String
code> 通过方法调用。
对象中的所有字段都是String
类型。 arrayList包含从TXT文件中提取的所有数据。我正在尝试将所有数据从 arrayList 传输到对象数组。 txt 文件有 35 列和 1500 行。数据由空格分隔
package mp1similar;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import EarthquakeRecord.Earthquakerecd;
public class MP1Similar
{
private static ArrayList arrayList ;
public static void main(String[] args)
{
ArrayList arrayList= null;
try
{
BufferedReader br = new BufferedReader(new FileReader("data/Catalog.txt"));
String line="";
arrayList =new ArrayList();
while((line = br.readLine())!=null)
{
// System.out.println(line);
StringTokenizer st = new StringTokenizer(line);
while(st.hasMoreTokens())
{
//System.out.println(st.nextToken());
arrayList.add(st.nextToken());
//System.out.println(br.readLine());
}
}
}
catch (FileNotFoundException ex)
{
System.out.println(ex.getMessage());
ex.printStackTrace();
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
ex.printStackTrace();
}
int j=0;
Earthquakerecd E[]= new Earthquakerecd[2000];
for(int i=0;i< arrayList.size();i++)
{
System.out.println(arrayList.get(i));
E[j] = new Earthquakerecd();
E[j].setDate(arrayList.get(i));
if (j>35 )
{
j=0;
}
j++;
}
}
}
I am getting an error in the line E[j].setDate(arrayList.get(i));
It says that the actual argument cannot be converted to java.lang.String
by method invocation.
All the fields in the object are String
Types. The arrayList contains all the data extracted from the TXT file. I am trying to transfer all the data from the arrayList to the object array. The txt file has 35 columns and 1500 rows. The data being seperated by whitespace
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
列表包含对象实例。 get() 方法返回对象。如果想要类型安全的 List,则必须使用泛型类型信息:
如果使用原始类型(没有
泛型类型信息),则编译器不知道 List仅包含 String 实例,因此您必须转换结果:旁注:学习缩进代码并尊重 Java 命名约定。它是不可读的。
A List contains Object instances. The get() method returns Object. If you want a typesafe List, you must use the generic type information:
If you use the raw type (without the
<String>
generic type information), then the compiler doesn't know that the List only contains String instances, and you thus have to cast the result:Side note: learn to indent your code and to respect Java naming conventions. It's unreadable as is.
我认为问题在于您使用的是原始 ArrayList 类型,而不是使用参数化 ArrayList 类型。因此,
ArrayList
上的所有操作都将假定参数和返回类型为Object
而不是String
,因为您尚未向 Java 指示您希望 ArrayList 保存 String。如果没有您提供的这些额外信息,Java 就无法知道其中存储的对象完全是String
,而不是Integer
或其他ArrayLists、
JPanel
等。要解决此问题,请将行更改
为
“This 明确向 Java 表明您的
ArrayList
应该只保存Strings,因此可以知道返回类型
arrayList.get(i)
将是一个String
而不是包罗万象的Object
。同样,更改为
要了解更多信息,请阅读 Java 泛型。它们是一个非常强大的工具,但很容易使用不当(就像您的情况一样)。 Oracle 的这篇文章是一个不错的来源。
希望这有帮助!
I think the problem is that you're using the raw
ArrayList
type rather than using a parameterizedArrayList
. Consequently, all operations on theArrayList
will assume that the parameter and returns types areObject
rather thanString
, since you haven't indicated to Java that you want theArrayList
to holdString
s. Without this extra information from you, Java can't know that the objects stored within are exclusivelyString
s and not, say,Integer
s, otherArrayList
s,JPanel
s, etc.To fix this, change the line
to read
This explicitly indicates to Java that your
ArrayList
should only holdString
s, so it can know that the return type ofarrayList.get(i)
is going to be aString
rather than the catch-allObject
. Similarly, changeto
For more information, read up on Java Generics. They're a very powerful tool, but can easily be used improperly (as in your case). One nice source is this article by Oracle.
Hope this helps!
除非您指定类型,否则 ArrayList get 调用将返回一个对象。
ArrayList get call returns an Object unless you specify the type.
Java 7 中添加了 Diamond:
Diamond was added in Java 7 :
将:更改
为:
或仅:
Change:
to:
or just: