java 类型不匹配无法从 String 转换为 String[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32027859/
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
Type mismatch cannot convert from String to String[]
提问by Parker Broz
When I run:
当我运行时:
import java.util.Scanner;
public class War {
public static void main(String[] args) {
String[] deckcards=deck();
String[] player1cards=player1Cards(deckcards);
}
public static String[] deck(){
String[] Cards = {"spades1","spades2","spades3", "spades4", "spades5", "spades6", "spades7", "spades8", "spades9", "spades10", "spadesJ", "spadesQ", "spadesK", "spadesA", "clubs1", "clubs2", "clubs3", "clubs4", "clubs5", "clubs6", "clubs7", "clubs8", "clubs9", "clubs10", "clubsJ", "clubsQ", "clubsK", "clubsA", };
return Cards;
}
public static String [] player1Cards(String[] deckcards){
String[] player1cards = deckcards[0];
return player1cards;
}
I get two errors.
我有两个错误。
One error tells me
一个错误告诉我
Type mismatch cannot convert from String to String[]
The other error tells me there is a line breakpoint error on:
另一个错误告诉我有一个行断点错误:
String[] player1cards = deckcards[0];
How can I fix this?
我怎样才能解决这个问题?
采纳答案by Bohemian
An element of a String[]
is a String
, not a String[]
, but saying that doesn't fix your problem.
a 的元素String[]
是 a String
,而不是 a String[]
,但说这不能解决您的问题。
Issues of design and style aside, I think your intention is:
撇开设计和风格的问题不谈,我认为你的意图是:
public class War {
public static void main(String[] args) {
String[] deckcards=deck();
// shuffle deck
List<String> list = new ArrayList<>(Arrays.asList(deckcards));
Collections.shuffle(list);
deckcards = list.toArray(new String[]{});
// deal from deck
String[] player1cards=player1Cards(deckcards, 0, 5);
}
public static String[] deck(){
String[] Cards = {"spades1","spades2","spades3", "spades4", "spades5", "spades6", "spades7", "spades8", "spades9", "spades10", "spadesJ", "spadesQ", "spadesK", "spadesA", "clubs1", "clubs2", "clubs3", "clubs4", "clubs5", "clubs6", "clubs7", "clubs8", "clubs9", "clubs10", "clubsJ", "clubsQ", "clubsK", "clubsA", };
return Cards;
}
public static String [] player1Cards(String[] deckcards, int from, int quantity){
String[] player1cards = Arrays.copyOfRange(deckcards, from, from + quantity);
return player1cards;
}
}
Disclaimer: untested code (thumbed on iPhone) and nothing done to fix design problems, but you will have something that works.
免责声明:未经测试的代码(在 iPhone 上使用)并且没有采取任何措施来解决设计问题,但您将获得一些有用的东西。
回答by hermit
Your problem is here:
你的问题在这里:
String[] player1cards = deckcards[0];
deckcards[0]
is an element of an array of type String
. So it is a String variable. But you are trying to assign it to String[] player1cards
which is an array of strings which is not allowed in Java.
deckcards[0]
是类型为 的数组的元素String
。所以它是一个字符串变量。但是您试图将它分配给String[] player1cards
Java 中不允许的字符串数组。
回答by Mark Dwayne
This is your error:
这是你的错误:
String[] player1cards = deckcards[0];
If you want to copy the first value of deckscards
into the first value of player1cards
, then one alternative is:
如果要将 的第一个值复制deckscards
到 的第一个值中player1cards
,则另一种选择是:
String[] player1cards = deckcards;
player1cards[0] = deckcards[0];
According to your code, you are saying that the whole String[]
is equal to the first value of the other array. That cannot be. If you want to equal two arrays, then:
根据您的代码,您是说整体String[]
等于另一个数组的第一个值。那不可能。如果要等于两个数组,则:
String [] automobile = {"toyota","zion"};
String[] car = {"nissan"};
automobile = car;
Hope this helps.
希望这可以帮助。