数据结构使用一:字符串操作

字符串:由char[]组成,及建立在字符数组上的一系列操作

字符串对象 String/StringBuilder/StringBuffer 区别

import java.lang.String;
import java.lang.StringBuilder;
import java.lang.StringBuffer;

/*java.lang是提供利用 Java 编程语言进行程序设计的基础类。
最重要的类是Object(它是类层次结构的根)和 Class(它的实例表示正在运行的应用程序中的类)。
把基本类型的值当成一个对象来表示通常很有必要。
包装器类 Boolean、Character、Integer、Long、Float 和 Double 就是用于这个目的。
例如,一个 Double 类型的对象包含了一个类型为 double 的字段,这表示如果引用某个值,则可以将该值存储在引用类型的变量中。
这些类还提供了大量用于转换基值的方法,并支持一些标准方法,比如 equals 和 hashCode。
Void 类是一个非实例化的类,它保持一个对表示基本类型 void 的 Class 对象的引用。*/
  • “String对象初始化时char[]数组大小固定,提供字符串查找、替换、拼接、分割等方法,如果方法返回String对象,则new出新的String对象,而不是改变char[]数组,变更原始String对象”;常用方法
  • “对String对象的任何改变都不影响到原对象,相关的任何change操作都会生成新的对象”。
  • “StringBuilder对象初始化时char[]数组初始为16,后续增加char[]数组容量,在原char[]对象上增加字符串”;
  • “StringBuffer对象跟StringBuilder差别是线程安全的,当多线程append时保证按照字符串顺序增加”;
  • “StringBuilder对象支持的方法有:追加、插入、删除、逆序等操作方法”;

1、String源码

String:值表示为char数组

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence
{
    /** The value is used for character storage. */
    private final char value[];    //String值表示为char数组
 
    /** The offset is the first index of the storage that is used. */
    private final int offset;
 
    /** The count is the number of characters in the String. */
    private final int count;
 
    /** Cache the hash code for the string */
    private int hash; // Default to 0
 
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -6849794470754667710L;
 
    ......
 
}

String:方法

public String substring(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > count) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    if (beginIndex > endIndex) {
        throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    }
    return ((beginIndex == 0) && (endIndex == count)) ? this :
        new String(offset + beginIndex, endIndex - beginIndex, value);
    }
 
 public String concat(String str) {
    int otherLen = str.length();
    if (otherLen == 0) {
        return this;
    }
    char buf[] = new char[count + otherLen];
    getChars(0, count, buf, 0);
    str.getChars(0, otherLen, buf, count);
    return new String(0, count + otherLen, buf);
    }
 
 public String replace(char oldChar, char newChar) {
    if (oldChar != newChar) {
        int len = count;
        int i = -1;
        char[] val = value; /* avoid getfield opcode */
        int off = offset;   /* avoid getfield opcode */
 
        while (++i < len) {
        if (val[off + i] == oldChar) {
            break;
        }
        }
        if (i < len) {
        char buf[] = new char[len];
        for (int j = 0 ; j < i ; j++) {
            buf[j] = val[off+j];
        }
        while (i < len) {
            char c = val[off + i];
            buf[i] = (c == oldChar) ? newChar : c;
            i++;
        }
        return new String(0, len, buf);
        }
    }
    return this;

从上面的三个方法可以看出,无论是sub操、concat还是replace操作都不是在原有的字符串上进行的,而是重新生成了一个新的字符串对象。也就是说进行这些操作后,最原始的字符串并没有被改变。

2、StringBuilder源码

char[] value;

@Override
public AbstractStringBuilder append(char c) {
    ensureCapacityInternal(count + 1);
    value[count++] = c;
    return this;
}

jvm编译器对字符串的优化操作,见引用 海子 的文章

 

线性数据结构:字符串作为数据结构,特点极其鲜明,这可归纳为:结构简单,规模庞大,元素重复率高;

操作:字符串String、子串substring、前缀startsWith(特殊子串)、后缀endWith(特殊子串)、匹配

字符串模式匹配是一个经典的问题,有名字的算法已不下三十种。鉴于串结构自身的特点,在设计和分析串模式匹配算法时也必须做特殊的考虑。其中首先需要回答的一个问题就是,如何对任一串匹配算法的性能作出客观的测量和评估。

匹配分类:

  • 模式检测:仅判断是否存在匹配,不关心具体位置,如垃圾邮件检测;
  • 模式定位:若存在匹配,确定具体的匹配位置;
  • 模式计数:统计出匹配子串的总数,比如网络热词排行
  • 模式枚举:匹配出所有匹配的具体位置,比如网络搜索引擎,diff工具

匹配算法:蛮力计算、KMP算法、BM算法

字符串

时间

浮点数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值