gpt4 book ai didi

Java 数组列表错误?

转载 作者:行者123 更新时间:2023-12-01 18:39:52 25 4
gpt4 key购买 nike

// ArrayList
import java.io.*;
import java.util.*;

public class ArrayList
{
public static void main (String [] args)
{
Integer obj1 = new Integer (97);
String obj2 = "Lama";
CD obj3 = new CD("BlahBlah", "Justin Bieber", 25.0, 13);

ArrayList objects = new ArrayList();

objects.add(obj1);
objects.add(obj2);
objects.add(obj3);
}
}

我正在尝试使用这三个对象创建一个数组列表,为什么会收到此错误?

ArrayList.java:15: cannot find symbol
symbol : method add(java.lang.Integer)
location: class ArrayList
objects.add(obj1);
^
ArrayList.java:16: cannot find symbol
symbol : method add(java.lang.String)
location: class ArrayList
objects.add(obj2);
^
ArrayList.java:17: cannot find symbol
symbol : method add(CD)
location: class ArrayList
objects.add(obj3);
^
3 errors

最佳答案

因为,您的类名称是ArrayList。更改您的类(class)名称,例如,

 public class ArrayListTest{
.....
}

或者,使用完全限定类名java.util.ArrayList

public class ArrayList{


public static void main (String [] args)
{
....
java.util.ArrayList objects = new java.util.ArrayList();
...
}

关于Java 数组列表错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20392851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com