JAVA - 错误类型不兼容:字符串不能转换为字符串[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23003874/
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
JAVA - Error incompatible types: String cannot be converted to String[ ]
提问by Preeyah
I'm working on Java project - Playing Cards.
我正在研究 Java 项目 - 扑克牌。
And, in this Card.java class, I've declared and initialized variables and array list. I've come across this error. These errors are under each getter and setter methods.
而且,在这个 Card.java 类中,我已经声明并初始化了变量和数组列表。我遇到过这个错误。这些错误在每个 getter 和 setter 方法下。
error: incompatible types: String cannot be converted to String[]
error: incompatible types: int[] cannot be converted to int
public class Card {
// private String suit;
//private String name;
//private int value;
private String[] suit = {"spades","hearts","clubs","diamonds"};
private String[] name = {"Ace","Hyman","Queen","King"};
private int[] value = {1,2,3,4,5,6,7,8,9,10,11,12,13};
public Card(String s, String n, int v)
{
suit = s;
name = n;
value = v;
}
public String getSuit()
{
return suit;
}
public String getName()
{
return name;
}
public int getValue()
{
return value;
}
public String toString()
{
return "<"+suit+" "+name+">";
}
This is the whole class.
这是全班。
Hope anyone knows and can help me out, thanks! (:
希望知道的朋友帮帮我,谢谢!(:
If you don't understand what i'm trying to get at, let me know, try my best to explain
如果你不明白我想说什么,让我知道,尽我所能解释
采纳答案by SudoRahul
I bet you generated the getters and setters and the constructor for the initial set of fields which are these.
我敢打赌,您为这些初始字段集生成了 getter 和 setter 以及构造函数。
// private String suit;
// private String name;
// private int value;
But after changing them to
但是在将它们更改为
private String[] suit = { "spades", "hearts", "clubs", "diamonds" };
private String[] name = { "Ace", "Hyman", "Queen", "King" };
private int[] value = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
you forgot to modify them accordingly. You need to change your getters and setters and constructors to something like this. The same goes with the toString()
method.
你忘了相应地修改它们。您需要将 getter、setter 和构造函数更改为这样的内容。这同样与toString()
方法。
public class Card {
private String[] suit = { "spades", "hearts", "clubs", "diamonds" };
private String[] name = { "Ace", "Hyman", "Queen", "King" };
private int[] value = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
public Card(String[] suit, String[] name, int[] value) {
super();
this.suit = suit;
this.name = name;
this.value = value;
}
public String[] getSuit() {
return suit;
}
public void setSuit(String[] suit) {
this.suit = suit;
}
public String[] getName() {
return name;
}
public void setName(String[] name) {
this.name = name;
}
public int[] getValue() {
return value;
}
public void setValue(int[] value) {
this.value = value;
}
@Override
public String toString() {
return "Card [suit=" + Arrays.toString(suit) + ", name="
+ Arrays.toString(name) + ", value=" + Arrays.toString(value)
+ "]";
}
}
Always remember to generate fresh getters, setters, constructor, toString()
methods, if you happen to change the fields in the class.
toString()
如果您碰巧更改了类中的字段,请始终记住生成新的 getter、setter、构造函数、方法。
回答by raj
Use
用
for(String s:suit){
System.out.println(s);
}
to get string from array of string
从字符串数组中获取字符串
回答by Tanu Garg
Use String[] instead of String and Int[] instead of int
使用 String[] 代替 String 和 Int[] 代替 int
in your all getter setter methods.
在你所有的 getter setter 方法中。
回答by Devavrata
You are returning the array in get methods and you have declared its return type String, it should be array of Strings.
您正在 get 方法中返回数组,并且您已经声明了它的返回类型 String,它应该是Strings 数组。
public String[] getName()
{
return name;
}
You can do it for others similarly.
你可以为其他人做类似的事情。
回答by Henry
They are incompatible because your get methods are of type String and int, and you are asking these methods to return an array.
它们不兼容,因为您的 get 方法是 String 和 int 类型,并且您要求这些方法返回一个数组。
回答by Jawad Zeb
Your variables suit
, name
and value
are arrays:
你的变量suit
,name
并且value
是数组:
private String[] suit = {"spades","hearts","clubs","diamonds"};
private String[] name = {"Ace","Hyman","Queen","King"};
private int[] value = {1,2,3,4,5,6,7,8,9,10,11,12,13};
In the Constructor you are passing String s , String n, int v
as a single data type and assigning it to an array suit
, name
and value
.
在构造函数中,您将String s , String n, int v
作为单个数据类型传递并将其分配给数组suit
,name
和value
。
public Card(String s, String n, int v)
{
suit = s;
name = n;
value = v;
}
Make them String[] s, String[] n , int[] v
your error will be gone:
让他们String[] s, String[] n , int[] v
你的错误将消失:
public Card(String[] s, String[] n, int[] v)
{
suit = s;
name = n;
value = v;
}
In the below code you are getting an array not a single string. Make their return type String[]
在下面的代码中,您得到的是一个数组而不是单个字符串。使他们的返回类型String[]
public String getSuit()
{
return suit;
}
public String getName()
{
return name;
}
回答by AJJ
The assignment of variables suit, name and value in the Card constructor is wrong. The variables suit , name and value are of array type and you are assigning string and int type to it., which is wrong. In Java, if the size of array is fixed, it cannot be modified. You can use array list instead as,
Card 构造函数中变量suit、name 和value 的赋值是错误的。变量 fit 、 name 和 value 是数组类型,您正在为其分配 string 和 int 类型,这是错误的。在 Java 中,如果数组的大小是固定的,则不能对其进行修改。您可以使用数组列表代替,
import java.util.ArrayList;
import java.util.Arrays;
public class Card {
private static ArrayList<String> suitArrayList = new ArrayList<String>();
private static ArrayList<String> nameArrayList = new ArrayList<String>();
private static ArrayList<Integer> valueArrayList = new ArrayList<Integer>();
private static String[] suit = {"spades","hearts","clubs","diamonds"};
private static String[] name = {"Ace","Hyman","Queen","King"};
private static Integer[] value = {1,2,3,4,5,6,7,8,9,10,11,12,13};
public Card(String s, String n, int v)
{
suitArrayList.add(s);
nameArrayList.add(n);
valueArrayList.add(v);
}
public static void main(String[] args) {
suitArrayList.addAll(Arrays.asList(suit));
nameArrayList.addAll(Arrays.asList(name));
valueArrayList.addAll(Arrays.asList(value));
Card card = new Card("ADDED SUITE", "ADDED NAME", 222222);
for (String s : suitArrayList) {
System.out.println("suit element :: " + s);
}
for (String s : nameArrayList) {
System.out.println("name element :: " + s);
}
for (Integer i : valueArrayList) {
System.out.println("value element :: " + i);
}
}
}