【问题标题】:retrieving values from the array of values from the server从服务器的值数组中检索值
【发布时间】:2014-01-03 13:10:36
【问题描述】:

我有一段代码,正在尝试追踪它。代码是:

if(response != null) 
        { 

            String line="";

            String strLine[]=new String[7];

            docids.clear();
            names.clear();
            specl.clear();
            qual.clear();
            exp.clear();
            loc.clear();
            cid.clear();


            passmsg="";

            //ArrayList<String> arrloc = new ArrayList<String>();

            InputStream inputstream = response.getEntity().getContent(); 

            //line[] = convertStreamToString(inputstream); 

            BufferedReader rd = new BufferedReader(new InputStreamReader(inputstream));
            try 
                {

                while ((line = rd.readLine()) != null) 

                    {

                    Toast.makeText(this,"Line:"+line , Toast.LENGTH_LONG).show();
                    strLine= line.split(",");
                    String errmsg,errno;


                    Integer a=Integer.parseInt(strLine[0]);


                    if(a==0)
                    {


                     Toast.makeText(this,"NO SESSION!!!" , Toast.LENGTH_LONG).show();
                     Intent myIntent = new Intent(getApplicationContext(),LoginActivity.class);
                     startActivityForResult(myIntent, 0);

                    }

                    else if(a==1)
                    {

                        errno=strLine[1];
                        if(errno.equals("61")||errno.equals("49"))
                            {
                            errmsg=strLine[2];
                             Toast.makeText(this,errmsg , Toast.LENGTH_LONG).show();
                            }
                        else if(errno.equals("194"))
                        {
                        errmsg=strLine[2];
                         Toast.makeText(this,errmsg , Toast.LENGTH_LONG).show();
                         e1.setText("");
                         e1.requestFocus();
                        }

                        else if(errno.equals("195"))
                        {
                        errmsg=strLine[2];
                         Toast.makeText(this,errmsg , Toast.LENGTH_LONG).show();
                         e2.setText("");
                         e2.requestFocus();
                        }

                        else if(errno.equals("62")) 
                            {
                            //whc=strLine[3];

                                whc=which.toString();

                            if(whc.equals("1"))
                            {
                                passmsg="Doctor List,Specialties";
                            }

                            else if(whc.equals("2"))
                            {
                                passmsg="Doctor List,City Locations";
                            }
                            docids.add(strLine[3]);



                            names.add("Dr."+strLine[4]);

                            String sp=replace(strLine[5]);
                            specl.add("Specialities:"+sp);

                            String q=replace(strLine[6]);
                            qual.add("Qual:"+q);

                            exp.add("Exp:"+strLine[7]+"yrs");

                            if(whc.equals("1"))
                            {
                                loc.add(replace(strLine[8]));
                            }

                            else if(whc.equals("2"))
                            {

                                cid.add(strLine[9]);
                                loc.add(strLine[8]+","+strLine[10]);
                            }

代码执行完美,但我怀疑当数组声明为 7 个值时 String strLine[]=new String[7]; 我们只能存储从 0 到第 6 个位置的值。如何存储和检索第 7 个位置以后的值?

cid.add(strLine[9]);
loc.add(strLine[8]+","+strLine[10]);

为什么我没有收到 ArrayOutOfBoundException?

【问题讨论】:

    标签: android exception arrays


    【解决方案1】:
    strLine= line.split(",");
    

    由于上述行,创建了一个新的字符串数组。 所以这个数组的大小和 split 函数返回的元素是一样的。

    strLine= new String[7];
    

    上面的行没有意义,因为当您将新数组分配给 strLine 时引用被破坏。

    你可以写 strLine=null;

    然后写上:

    strLine= line.split(",");
    

    将初始化您的字符串数组。

    【讨论】:

      【解决方案2】:

      字符串数组只能初始化一次且只能初始化一次。

      您的解决方案是:

      String a[]; 
      a = new String[no_of_strings_received];
      

      根据动态收到的响应数实例化数组!

      【讨论】:

      • 声明字符串的方式相同。但我的疑问是字符串数组何时被声明为 7 个值,如何存储超过 7 个值??
      猜你喜欢
      • 2011-12-05
      • 2012-01-28
      • 2013-09-20
      • 1970-01-01
      • 2019-02-07
      • 2011-04-24
      • 2020-02-10
      • 2021-12-14
      • 1970-01-01
      相关资源
      最近更新 更多