我正在android环境中工作,并尝试了以下代码,但它似乎没有工作.
String [] stockArr = (String[]) stock_list.toArray();
Run Code Online (Sandbox Code Playgroud)
如果我定义如下:
String [] stockArr = {"hello", "world"};
Run Code Online (Sandbox Code Playgroud)
有用.有什么东西我不见了吗?
没错
Object[] a = new String[]{"12","34","56"};
String[] b = (String[]) a;
Run Code Online (Sandbox Code Playgroud)没错
Object a = new String[]{"12","34","56"};
String[] b = (String[]) a;
Run Code Online (Sandbox Code Playgroud)运行时错误:ClassCastException
Object[] a = new Object[3];
a[0] = "12";
a[1] = "34";
a[2] = "56";
String[] b = (String[]) a;
Run Code Online (Sandbox Code Playgroud)运行时错误:ClassCastException
Object[] a = {"12","34","56"};
String[] b = (String[]) a;
Run Code Online (Sandbox Code Playgroud)当然,如果将Object[]
变量String[]
创建为一个变量,我们可以将变量转发回String[]
.
我的问题是,为什么我们不能投Object[]
来String[]
,当它被作为创建的Object[]
,但它的所有成员都是字符串?是因为安全原因还是没有那么有用呢?
在我的应用程序中,我为gcm ccs(xmpp)运行这些代码,代码显示以下错误执行时发生错误doinbackground.excute()
这是代码:
sendTask = new AsyncTask<String, String, String>() {
protected String doInBackground(String... title) {
Bundle data = new Bundle();
data.putString("ACTION", action);
data.putString("CLIENT_MESSAGE", "Hello GCM CCS XMPP!");
String id = Integer.toString(ccsMsgId.incrementAndGet());
try {
Log.d("RegisterActivity", "messageid: " + id);
gcm.send(SENDER_ID + "@gcm.googleapis.com", id,
data);
Log.d("RegisterActivity", "After gcm.send successful.");
} catch (IOException e) {
Log.d("RegisterActivity", "Exception: " + e);
e.printStackTrace();
}
return "Sent message.";
}
protected void onPostExecute(String result) {
sendTask = null;
// tosat about the success in return
} …
Run Code Online (Sandbox Code Playgroud)