1

I've got a problem! I'm trying to convert a String to an ArrayList but it does'nt work, it says in the console that Type mismatch : cannot convert from list to ArrayList my little program :

package nfa032.application;
import java.util.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
public class ArrayListUtils {

public static void AfficherArrayListString(ArrayList<String>a) 
{
    for(int i=1; i<=a.size(); i++)
    { 
    System.out.println(a.get(i));
    }
}

public static ArrayList<String> lireArrayListString(int n) throws IOException
{
    InputStreamReader isr=new InputStreamReader(System.in);
    BufferedReader in=new BufferedReader(isr);
    String s;
    String [] str;
    ArrayList liste=new ArrayList<String>();
    for(int i=1; i<=n; i++)

    {
        System.out.println("Entrer une chaîne.");
        s=in.readLine();
        str=s.split("\\s+");
        liste=Arrays.asList(str);       
     }
     return liste;
}

}

3 Answers 3

1

I guess the problem is this line liste=Arrays.asList(str);

It's because you are trying to assign an interface List to an implementation of that interface which is ArrayList

To solve this you can do List<String> liste=new ArrayList<String>();

Sign up to request clarification or add additional context in comments.

Comments

1

There are several problems with the code you have.

  • Your liste variable doesn't have a diamond operator specified ArrayList should be ArrayList<String> to indicate that it will contain String elements (for more info on diamond operators https://www.baeldung.com/java-diamond-operator)

  • Arrays.asList() does not return an ArrayList<>, but it returns a List<> (The super type of ArrayList). If you want to use Arrays.asList, you need to change the type of liste to List<String>

Code becomes something like this

public static void AfficherArrayListString(final ArrayList<String> a) {
    for (int i = 1; i <= a.size(); i++) {
        System.out.println(a.get(i));
    }
}

public static List<String> lireArrayListString(final int n) throws IOException {
    final InputStreamReader isr = new InputStreamReader(System.in);
    final BufferedReader in = new BufferedReader(isr);
    String s;
    String[] str;
    List<String> liste = new ArrayList<>();
    for (int i = 1; i <= n; i++) {
        System.out.println("Entrer une chaîne.");
        s = in.readLine();
        str = s.split("\\s+");
        liste = Arrays.asList(str);
    }
    return liste;
}

Comments

0
public static ArrayList<String> lireArrayListString(int n) throws IOException
{
    InputStreamReader isr=new InputStreamReader(System.in);
    BufferedReader in=new BufferedReader(isr);
    String s;
    String [] str;
    ArrayList<String> liste=new ArrayList<String>();
    for(int i=1; i<=n; i++)
    {
        System.out.println("Entrer une chaîne.");
        s=in.readLine();
        str=s.split("\\s+");
        liste=(ArrayList<String>) Arrays.asList(str);
        
    }
    return liste;
}

}

5 Comments

here, i've put an ArrayList<String> after liste=
and before Arrays.asList(str);
but there's an exception when i enter a blockchain
Exception in thread "main" java.lang.ClassCastException: class java.util.Arrays$ArrayList cannot be cast to class java.util.ArrayList (java.util.Arrays$ArrayList and java.util.ArrayList are in module java.base of loader 'bootstrap') at nfa032.application.ArrayListUtils.lireArrayListString(ArrayListUtils.java:26) at nfa032.application.Main.main(Main.java:27)
Arrays.asList() does not return an ArrayList<>, but it returns a List<> (The super type of ArrayList). If you want to use Arrays.asList, you need to change the type of liste to List<String>

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.