12

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 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]);
1
  • How do you get an element at index i from an ArrayList? Commented Nov 19, 2013 at 9:40

7 Answers 7

26

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++){
Sign up to request clarification or add additional context in comments.

Comments

1

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));
        }

    }

2 Comments

Why are you rolling this back over and over again?
@BoltClock it was because there were many revisions and i got confused sorry about that..
1

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);
    }

1 Comment

If you need the index, you should not be using a for-each loop and instead use a correct version of the original loop, as in the accepted answer.
0

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

Comments

0

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

files.get(i)

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

Comments

0

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));
        }

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.