数组中的字符串操作

时间:2012-08-31 12:41:46

标签: android arrays

如何解决此错误 The operator += is undefined for the argument type(s) String[][], String

  SmsMessage[] msgs = null;
   String [][]str = null;  

    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            str += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";     

1 个答案:

答案 0 :(得分:2)

错误几乎说明了一切 - 你不能在Java中使用+=数组。为什么不使用List来构建String值,然后在完成后将其转换为数组?

    List<String> str = new ArrayList<String>();  

    for (int i=0; i<msgs.length; i++){
        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
        str.add("SMS from " + msgs[i].getOriginatingAddress());                     
        str.add(" :");
        ...

    String[] strArray = str.toArray(new String[str.size()]);