[英]problem of conversion from a String to an arrayList in java
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 :我遇到了一个问题,我正在尝试将字符串转换为 ArrayList 但它不起作用:它在控制台中显示类型不匹配:无法从列表转换为 ArrayList 我的小程序:
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;
}
} }
I guess the problem is this line liste=Arrays.asList(str);
我想问题是这一行
liste=Arrays.asList(str);
It's because you are trying to assign an interface List
to an implementation of that interface which is ArrayList
这是因为您正在尝试将接口
List
分配给该接口的实现,即ArrayList
To solve this you can do List<String> liste=new ArrayList<String>();
要解决此问题,您可以执行
List<String> liste=new ArrayList<String>();
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 )您的
liste
变量没有指定菱形运算符ArrayList
应为ArrayList<String>
以指示它将包含字符串元素(有关菱形运算符https://www.baeldung.com/java-diamond-operator的更多信息)
Arrays.asList()
does not return an ArrayList<>
, but it returns a List<>
(The super type of ArrayList). Arrays.asList()
不返回ArrayList<>
,但返回List<>
(ArrayList 的超类型)。 If you want to use Arrays.asList
, you need to change the type of liste
to List<String>
如果要使用
Arrays.asList
,则需要将liste
的类型更改为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;
}
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;
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.