Java 需要数组,但找到了 ArrayList<String>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20067956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:49:57  来源:igfitidea点击:

array required, but ArrayList<String> found

java

提问by John Smith

I have a ruby backround and im new to java i wrote a basic programm but somehow i get a error i cant fix! My code:

我有一个 ruby​​ 背景,我是 Java 新手,我写了一个基本程序,但不知何故我遇到了一个无法修复的错误!我的代码:

import java.util.ArrayList;

public class Music {

    private ArrayList<String> files;


    public static void main(String args[]){

        Music a = new Music();
        a.addFile("Chasen Paper");
        a.addFile("Mama");
        a.addFile("Hell Yes");
        a.removeFile("Hell Yes");
    }
    public Music(){
      files = new ArrayList<String>();
    }

    public void addFile(String filename){
        files.add(filename);
    }

    public void returnFiles(){
        for(int i = 0; files.size() <= i; i++){
            System.out.println( i + ". Ist: " + files[i]);
        }

    }

    public void removeFile(String filename){
        System.out.println("Vorher gab es " + files.size() + " Dateien");
        files.remove(filename);
        System.out.println("Jetzt gibt es " + files.size() + " Dateien");
    }
}

When i try to compile it i get somehow this error: What did i wrong? Thanks!

当我尝试编译它时,我以某种方式得到这个错误:我做错了什么?谢谢!

Music.java:26: error: array required, but ArrayList<String> found
            System.out.println( i + ". Ist: " + files[i]);

采纳答案by SudoRahul

You need to use the get()method to get the element at a particular index from an ArrayList. You can't use []to get the element at a particular index, in an arraylist. Its possible only for arrays and your filesis not an array, but an ArrayList.

您需要使用该get()方法从ArrayList. 您不能用于[]在数组列表中获取特定索引处的元素。它仅适用于数组,而您files的不是数组,而是 ArrayList。

System.out.println( i + ". Ist: " + files.get(i));

Also, the condition in your forloop is a bit off. files.size() <= iis false, and therefore, it doesn't enter the forloop at all.

此外,for循环中的条件有点偏离。files.size() <= iis false,因此,它根本不进入for循环。

Change it to something like this.

把它改成这样。

for(int i = 0; i < files.size() ; i++){

回答by Sandiip Patil

files[i] is used for arrays. While working with lists you need to use indexing. Try files.get(i)

files[i] 用于数组。在处理列表时,您需要使用索引。试试 files.get(i)

回答by Lakshmi

You cannot access the arraylist like an array you have to use the method get(index) in order to get the i th element.

您不能像数组一样访问数组列表,您必须使用 get(index) 方法才能获取第 i 个元素。

   public void returnFiles(){
        for(int i = 0;i< files.size() ; i++){
            System.out.println( i + ". Ist: " + files.get(i));
        }

    }

回答by Chayemor

Hey the problem is in this method

嘿,问题出在这个方法上

public void returnFiles(){
        for(int i = 0; files.size() <= i; i++){
            System.out.println( i + ". Ist: " + files[i]);
        }

 }

Precisely, on the

准确地说,在

files[i]

You are trying to access your ArrayList instance variable as if it were a an Array. Just change that for

您正在尝试访问您的 ArrayList 实例变量,就好像它是一个数组一样。只需将其更改为

files.get(i)

You have to use the method get(int index)from the ArrayList<> class.

您必须使用get(int index)ArrayList<> 类中的方法。

回答by Rakesh KR

Change this

改变这个

for(int i = 0; files.size() <= i; i++){
    System.out.println( i + ". Ist: " + files[i]);
}

As

作为

for(String i:files){
    System.out.println(i);
}

If you need index

如果你需要索引

int index = 0;
for(String i:files){
        System.out.println((index++) + ".Ist: " +i);
    }

回答by Prasad

files is an ArrayListand not an Arrayinstead of doing files[i]you must do this-

文件是一个ArrayList而不是一个Array而不是files[i]你必须这样做 -

        for(int i = 0; files.size() <= i; i++){
            System.out.println( i + ". Ist: " + files.get(i));
        }

回答by Anup

You have to use files.get(i) as your are using ArrayList and not Array. When you are using array at that time you will require its index location to fetch values from it.ArrayList provides get(i) method to fetch values from init.

您必须使用 files.get(i),因为您使用的是 ArrayList 而不是 Array。当你在那个时候使用数组时,你将需要它的索引位置从中获取值。ArrayList 提供了 get(i) 方法来从 init 中获取值。