ArrayList使用.add、.size和.get方法时无法解析方法,为什么?

huangapple go评论110阅读模式
英文:

ArrayList using the .add .size and .get methods cannot resolve methods why?

问题

大家好,我是新来的,不太确定之前是否有涉及过这个问题,但我会继续查找直到获得一些答案或自己找到解决方法。如果我找到答案,我会删除这篇帖子。无论如何,我的问题是我正在按照这个在线课程的内容进行学习,在视频中所示的示例与我在IDE中记录的内容完全一致。我已经仔细检查过多次,确保没有任何差异,但是当老师运行代码时,代码可以正常执行,而当我运行时,却显示出“无法解析方法”的错误。我尝试使用 names.add("James") 等方式向 ArrayList 中添加名称,但出现问题。在我的 for 循环中使用 names.size,或在 println 命令中使用 names.get 时,我真的不知道为什么会发生这种情况,也不知道如何解决。再次说明,我对这门语言和论坛都很陌生,如果我没有提供足够清晰的解释,我深感抱歉。我将附上我的代码,以便您了解我所面临的问题。感谢任何帮助!

package com.example.arrays;

import java.util.ArrayList;

public class ArrayListDemo
{
        public static void main(String [] args)
        {
            // 标准的 Java 数组具有固定的长度。创建数组后,它们不能增长或缩小,这意味着您必须预先知道数组将包含多少元素。

            // 数组列表创建时具有初始大小。当超过此大小时,集合会自动扩大。当对象被移除时,数组可能会缩小。

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

            names.add("James");//0
            names.add("Peter");//1
            names.add("John");//2
            names.add("Jake");//3
            names.add("Paul");//4

            for (int i = 0 ; i < names.size(); i++)
            {
                System.out.println(names.get(i));
            }
        }
}
英文:

Hi everyone i'm new here and i'm not sure if this has been covered before but will be looking until i receive some answers or find them myself if i find them i will take this post down. Anyways my problem is i'm following along with this online course and in the video the example being taught is exactly as I've written down in the ide I've quadruple checked to make sure there are no differences but when the teacher goes to run the code it executes fine when I do it shows cannot resolve method errors where i'm trying to add names to an ArrayList with names.add(&quot;James&quot;) etc.. and when I try to use name.size in my for loop or names.get in my println command i'm really lost as to why this keeps happening and not sure how to go about fixing it. Again i'm new to this language and forum so if I've not given a clear enough explanation i apologize i will attach the code I've done to show you exactly what it is i'm dealing with. Thanks for any help!

package com.example.arrays;

import java.util.ArrayList

public class ArrayList
{
       public static void main(String [] args)
       {
           //Standard Java arrays are of a fixed length. After arrays are created,
           //they cannot grow or shrink, which means that you must know in advanced
           //how many elements an array will hold.

           //Array lists are vreated with an initial size. When this size is exceeded
           //the collection is automatically enlarged. When objects are removed,
           //the array may be shrunk.

           ArrayList&lt;String&gt; names = new ArrayList&lt;&gt;();

           names.add(&quot;James&quot;);//0
           names.add(&quot;Peter&quot;);//1
           names.add(&quot;John&quot;);//2
           names.add(&quot;Jake&quot;);//3
           names.add(&quot;Paul&quot;);//4

           for (int i = 0 ; i &lt; names.size(); i++)
           {
               System.out.println(names.get(i));
           }
}

答案1

得分: 4

ArrayList 指的是你的类本身,就像你将其命名为 ArrayList 一样。要么重新命名它<sub>(演示)</sub>(请注意,你需要同时更改类声明以及文件名,除非你使用能够自动处理重命名的集成开发环境),要么移除导入语句并且对 java.util.ArrayList 的所有访问进行限定<sub>(演示)</sub>,就像这样:

java.util.ArrayList&lt;String&gt; names = new java.util.ArrayList&lt;&gt;();
英文:

ArrayList refers to your class itself, as you have named it ArrayList. Either rename it<sub>(Demo)</sub> (note that you will need to change the class declaration as well as the file name unless you are using an IDE that can handle the renaming for you) or remove the import and qualify all access to java.util.ArrayList<sub>(Demo)</sub> like so:

java.util.ArrayList&lt;String&gt; names = new java.util.ArrayList&lt;&gt;();

答案2

得分: 3

问题在于您将类命名为 ArrayList,因此当您实例化 names 时,编译器无法确定您是要创建一个新的 Java ArrayList,还是要引用您自己的类。尝试将您的类重命名为其他名称,比如 "ArrayListTest"。

英文:

The issue here is that you have named your class ArrayList, so when you instantiate names the compiler isn't sure whether you want to create a new java ArrayList, or if you're referring to your own class. Try renaming your class to something else, like "ArrayListTest".

huangapple
  • 本文由 发表于 2020年7月24日 07:08:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63064398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定