java 无法从 String[] 转换为 String
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6136914/
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
Cannot convert from String[] to String
提问by Sergio
Here is my code.. I want to find just a name of city from latitude and longitude.. Now i get this :
这是我的代码..我只想从纬度和经度找到一个城市的名字..现在我明白了:
"Address[addressLines=[0:"Mustam?e tee 145",1:"12918 Tallinn",2:"Estonia"],feature=145,admin=Harju County,sub-admin=null,locality=Tallinn,thoroughfare=Mustam?e tee,postalCode=12918,countryCode=Estonia,countryName=null,hasLatitude=true,latitude=59.4123264,hasLongitude=true,longitude=24.6903851,phone=null,url=null,extras=null]"
I tried to convert it to string and split it.. i made a String array but when i try to assign "cityName" to String array "name" I get this error - "Type mismatch: cannot convert from String[] to String"
我试图将它转换为字符串并拆分它..我做了一个字符串数组,但是当我尝试将“cityName”分配给字符串数组“name”时,我收到此错误 - “类型不匹配:无法从字符串 [] 转换为字符串”
Can you tell me please where am I wrong?
你能告诉我我错在哪里吗?
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class GPSHelper {
static double latitude;
static double longitude;
static String[] name = new String[5];
public static void getCity(LocationManager lm, Context appContext) {
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
longitude = location.getLongitude();
latitude = location.getLatitude();
final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder userLocation = new Geocoder(appContext, Locale.getDefault());
List<Address> cityName;
try {
cityName = userLocation.getFromLocation(latitude, longitude, 1);
if(cityName != null && !cityName.isEmpty()) {
for(int i=0; i<5; i++){
name[i] = cityName.get(0).toString().split("");
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
回答by Boro
All the answers here are fine. But what I wonder is why would you use a split on empty character?
这里的所有答案都很好。但我想知道的是,为什么要对空字符使用拆分?
Do you know it returns array of single characters (and nothing at start)?
您知道它返回单个字符的数组吗(一开始什么也没有)?
Checkout the code:
检查代码:
String str ="dasd";
String[] strr = str.split("");
for(int i=0; i < strr.length;i++)
System.out.println("i="+i+";str="+strr[i]);
Output:
输出:
i=0;str=
i=1;str=d
i=2;str=a
i=3;str=s
i=4;str=d
I would suggest that since cityName.get(0)
returns Address object have a method there which will return you its name, like below.
我建议因为cityName.get(0)
返回 Address 对象有一个方法可以返回它的名称,如下所示。
name[i] = cityName.get(0).getName();
Hope this helps.
希望这可以帮助。
回答by amit
name[i] = cityName.get(0).toString().split("");
name[i]
is of type String
while cityName.get(0).toString().split("")
is of type String[]
name[i]
是类型String
while cityName.get(0).toString().split("")
是类型String[]
回答by Gnanz
name[i] = cityName.get(0).toString().split("");
here name[i] will hold only string, but split("") method needs String Array in left side.
这里 name[i] 将只保存字符串,但 split("") 方法需要左侧的字符串数组。
Is that for loop really needed?? Waht about removing the for loop and code it like
真的需要那个for循环吗??关于删除 for 循环并对其进行编码
if(cityName != null && !cityName.isEmpty()) {
name[] = cityName.get(0).toString().split("");
}
回答by Houcine
the Split method returns an array of String ; so when you do this :
Split 方法返回一个 String 数组;所以当你这样做时:
name[i] = cityName.get(0).toString().split("");
it will return an array of Strings which will contain all of letters of your String ,
它将返回一个字符串数组,其中包含您的 String 的所有字母,
回答by Kaj
cityName.get(0).toString().split("");
That returns a String[]
as result, but name[i]
should be a String
and not a String[]
String[]
结果返回a ,但name[i]
应该是aString
而不是aString[]
Why do you split?
你为什么分裂?
回答by JB Nizet
The following line is wrong:
以下行是错误的:
name[i] = cityName.get(0).toString().split("");
(as the exception message probably indicates). Indeed, you're trying to initialize name[i]
, which is a String, with the result of the split
method, which returns an array of Strings.
(正如异常消息可能表明的那样)。实际上,您正在尝试name[i]
使用split
方法的结果初始化一个字符串,该方法返回一个字符串数组。
回答by acostache
I do not have any experience with Android, so I don't know what those methods should return, but String.split will return a String array. And name[i] is only a String. So you are trying to assign a String array to a simple String.
我对 Android 没有任何经验,所以我不知道这些方法应该返回什么,但是 String.split 会返回一个 String 数组。而 name[i] 只是一个字符串。所以你试图将一个字符串数组分配给一个简单的字符串。