日期:2014-05-20  浏览次数:20640 次

★〓一个看似相当简单的问题,可惜我没搞定,请高手指点迷津!!!〓★
原题:public   class   TestString{
                  private   String   strRight= " ";
                  public   String   getStrRight(int   strIndex){
                        //需要实现的部分
                  }
                  public   void   setStrRight(int   strIndex,boolean   isOk){
                      //需要实现的部分
                  }
            }
说明:此类的作用是设置权限问题,如果有权限,则将字符串strRight的某个位(由setStrRight方法参数strIndex指定)的值(值是由isOk指定,true   为1,false   为 " ")修改为1;如果没有权限,则该位就为空(“”)。
呵呵,限时10分钟,超时可不算哦!

------解决方案--------------------
public static void setStrRight(String strRight,int index,boolean isOk)
{
String newStrRight;
char []arry=strRight.toCharArray();
if(isOk)
{
arry[index]= '1 ';
newStrRight= new String(arry);
}
else
{

arry[index]= ' ';
newStrRight= new String(arry);
newStrRight=newStrRight.replace( " ", " ");
}

System.out.println(newStrRight);
}
------解决方案--------------------
class TestString{
private String strRight= "test ";

public String getStrRight(int strIndex){
if(strRight.length()> strIndex)
return String.valueOf(strRight.charAt(strIndex));
else
return "error ";
}
public void setStrRight(int strIndex,boolean isOk){
if(strRight.length()> strIndex){
if(isOk)
strRight = replaceStr(strRight,strIndex, "1 ");
else
strRight = replaceStr(strRight,strIndex, " ");//空还是空格
}
}
private String replaceStr(String str,int index,String charC)
{
StringBuffer sb = new StringBuffer();
sb.append(str.substring(0,index));
sb.append(charC);
sb.append(str.substring(index+1));
return sb.toString();
}
}