JAVA - 错误不兼容类型:字符串无法转换为String []

时间:2014-04-11 05:01:45

标签: java arrays string arraylist int

我正在从事Java项目 - 扑克牌。

而且,在这个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","Jack","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+">";
}

这是全班。

希望有人知道,可以帮助我,谢谢! (:

如果您不了解我想要了解的内容,请告诉我,尽量解释

7 个答案:

答案 0 :(得分:2)

我敢打赌你为这些初始字段集生成了getter和setter以及构造函数。

// private String suit;
// private String name;
// private int value;

但是在将它们改为

之后
private String[] suit = { "spades", "hearts", "clubs", "diamonds" };
private String[] name = { "Ace", "Jack", "Queen", "King" };
private int[] value = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
你没有相应地修改它们。你需要将你的getter和setter以及构造函数更改为这样的东西。 toString()方法也是如此。

public class Card {

    private String[] suit = { "spades", "hearts", "clubs", "diamonds" };
    private String[] name = { "Ace", "Jack", "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)
            + "]";
    }
}

如果您碰巧更改了班级中的字段,请务必记住生成新的getter,setter,构造函数,toString()方法。

答案 1 :(得分:1)

您的变量suitnamevalue是数组:

private String[] suit = {"spades","hearts","clubs","diamonds"};
private String[] name = {"Ace","Jack","Queen","King"};
private int[] value = {1,2,3,4,5,6,7,8,9,10,11,12,13};

在构造函数中,您将String s , String n, int v作为单一数据类型传递,并将其分配给数组suitnamevalue

public Card(String s, String n, int v)
{
    suit = s;
    name = n;
    value = v;
}

让他们String[] s, String[] n , int[] v你的错误将会消失:

public Card(String[] s, String[] n, int[] v)
{
    suit = s;
    name = n;
    value = v;
}

在下面的代码中,您获得的数组不是单个字符串。返回类型String[]

public String getSuit()
{
    return suit;
}

public String getName()
{
    return name;
}

答案 2 :(得分:0)

使用

for(String s:suit){
System.out.println(s);
}

从字符串数组中获取字符串

答案 3 :(得分:0)

使用String []而不是String 和Int []而不是int

在你的所有getter setter方法中。

答案 4 :(得分:0)

您在get方法中返回数组并且已声明其返回类型为String,它应为字符串数组

 public String[] getName()
 {
     return name;
 }

你也可以为其他人做同样的事情。

答案 5 :(得分:0)

它们是不兼容的,因为你的get方法是String和int类型,并且你要求这些方法返回一个数组。

答案 6 :(得分:0)

Card构造函数中的变量suit,name和value的赋值是错误的。变量suit,name和value是数组类型,你要为它分配字符串和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","Jack","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);
        }
    }
}