能力值:
( LV2,RANK:10 )
2 楼
java没有指针,可以用String类中的构造函数实现。
String(byte[] bytes,int offset,int length);通过byte数组,从offset开始,总共length长的字节构造字符串对象。
String(byte[] char,int offset,int length);通过char数组,从offset开始,总共length长的字节构造字符串对象
能力值:
( LV3,RANK:20 )
3 楼
[QUOTE=qqsunqiang;1417445]java没有指针,可以用String类中的构造函数实现。
String(byte[] bytes,int offset,int length);通过byte数组,从offset开始,总共length长的字节构造字符串对象。
String(byte[] char,int offset,int leng...[/QUOTE]
请问可否具体一点?帮忙写一个简单的demo?麻烦了,对java这些东西不熟悉,之前都用C++的。
能力值:
( LV2,RANK:10 )
4 楼
public class MutableString {
private String str;
public MutableString(String str) {
this.str=str;
}
char charAt(int i)
{
byte[] ff=str.getBytes();
return (char)ff[i];
}
String substring(int start, int end)
{
return new String(str.getBytes(),start-1,end-1);
}
String setCharAt(int i,char c)
{
byte[] ff=str.getBytes();
ff[i-1]=(byte) c;
//str=new String(ff);
return new String(ff);
}
} 测试代码:
public class ggg {
/**
* @param args
*/
public static void main(String[] args) {
String ddd="abcdefghi";
MutableString ff=new MutableString(ddd);
System.out.print(ff.substring(2, 3)+" ");
System.out.print(ff.charAt(3));
System.out.print(" "+ff.setCharAt(3,'l'));
}
}
测试结果:
bc d abldefghi
能力值:
( LV3,RANK:20 )
5 楼
[QUOTE=qqsunqiang;1417458]public class MutableString {
private String str;
public MutableString(String str) {
this.str=str;
}
char charAt(int i)
{
byte[] ff=str.get...[/QUOTE]
感谢你的回复,谢谢了。但是不知道你在子程序中byte[] ff=str.getBytes()之后,算法空间复杂度还算不算是O(1)了?这块不是太明白。
能力值:
( LV2,RANK:10 )
6 楼
如果实现中没有循环就是 O(1)
能力值:
( LV3,RANK:20 )
7 楼
怎么把看雪币给你呢?
能力值:
( LV2,RANK:10 )
8 楼
标题处不是有一个问题已经解决,点击那个。