array required, but ArrayList found

后端 未结 7 1555
执念已碎
执念已碎 2021-02-06 07:47

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:

import java.util.ArrayList;

public class Musi         


        
相关标签:
7条回答
  • 2021-02-06 08:25

    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.

    0 讨论(0)
  • 2021-02-06 08:26

    files is an ArrayList and not an Array instead of doing files[i] you must do this-

            for(int i = 0; files.size() <= i; i++){
                System.out.println( i + ". Ist: " + files.get(i));
            }
    
    0 讨论(0)
  • 2021-02-06 08:29

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

    0 讨论(0)
  • 2021-02-06 08:34

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

       public void returnFiles(){
            for(int i = 0;i< files.size() ; i++){
                System.out.println( i + ". Ist: " + files.get(i));
            }
    
        }
    
    0 讨论(0)
  • 2021-02-06 08:39

    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 files is not an array, but an ArrayList.

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

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

    Change it to something like this.

    for(int i = 0; i < files.size() ; i++){
    
    0 讨论(0)
  • 2021-02-06 08:40

    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);
        }
    
    0 讨论(0)
提交回复
热议问题