使用 ArrayList 时出现类型错误?

发布于 2025-01-03 14:48:11 字数 1794 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挽梦忆笙歌 2025-01-10 14:48:12

列表包含对象实例。 get() 方法返回对象。如果想要类型安全的 List,则必须使用泛型类型信息:

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

如果使用原始类型(没有 泛型类型信息),则编译器不知道 List仅包含 String 实例,因此您必须转换结果:

String s = (String) list.get(i);

旁注:学习缩进代码并尊重 Java 命名约定。它是不可读的。

A List contains Object instances. The get() method returns Object. If you want a typesafe List, you must use the generic type information:

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

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:

String s = (String) list.get(i);

Side note: learn to indent your code and to respect Java naming conventions. It's unreadable as is.

め七分饶幸 2025-01-10 14:48:12

我认为问题在于您使用的是原始 ArrayList 类型,而不是使用参数化 ArrayList 类型。因此,ArrayList 上的所有操作都将假定参数和返回类型为 Object 而不是 String,因为您尚未向 Java 指示您希望 ArrayList 保存 String。如果没有您提供的这些额外信息,Java 就无法知道其中存储的对象完全是 String,而不是 Integer 或其他 ArrayLists、JPanel 等。

要解决此问题,请将行更改

private static ArrayList arrayList;

private static ArrayList<String> arrayList;

“This 明确向 Java 表明您的 ArrayList 应该只保存 Strings,因此可以知道返回类型arrayList.get(i) 将是一个 String 而不是包罗万象的 Object。同样,更改

arrayList = new ArrayList();

arrayList = new ArrayList<String>();

要了解更多信息,请阅读 Java 泛型。它们是一个非常强大的工具,但很容易使用不当(就像您的情况一样)。 Oracle 的这篇文章是一个不错的来源。

希望这有帮助!

I think the problem is that you're using the raw ArrayList type rather than using a parameterized ArrayList. Consequently, all operations on the ArrayList will assume that the parameter and returns types are Object rather than String, since you haven't indicated to Java that you want the ArrayList to hold Strings. Without this extra information from you, Java can't know that the objects stored within are exclusively Strings and not, say, Integers, other ArrayLists, JPanels, etc.

To fix this, change the line

private static ArrayList arrayList;

to read

private static ArrayList<String> arrayList;

This explicitly indicates to Java that your ArrayList should only hold Strings, so it can know that the return type of arrayList.get(i) is going to be a String rather than the catch-all Object. Similarly, change

arrayList = new ArrayList();

to

arrayList = new ArrayList<String>();

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!

无声无音无过去 2025-01-10 14:48:12
ArrayList<String> arrayList= null;

除非您指定类型,否则 ArrayList get 调用将返回一个对象。

ArrayList<String> arrayList= null;

ArrayList get call returns an Object unless you specify the type.

爱殇璃 2025-01-10 14:48:12

Java 7 中添加了 Diamond:

ArrayList<String> arrayList = new ArrayList<>();

Diamond was added in Java 7 :

ArrayList<String> arrayList = new ArrayList<>();
尹雨沫 2025-01-10 14:48:11

将:更改

ArrayList arrayList = null;
...
arrayList =new ArrayList();

为:

ArrayList<String> arrayList = null;
arrayList = new ArrayList<String>();

或仅:

ArrayList<String> arrayList = new ArrayList<String>();

Change:

ArrayList arrayList = null;
...
arrayList =new ArrayList();

to:

ArrayList<String> arrayList = null;
arrayList = new ArrayList<String>();

or just:

ArrayList<String> arrayList = new ArrayList<String>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文