常量字符串的设计与实现

  1 package com.tmx.string;
  2
  3 public class MyString implements java.io.Serializable {
  4     private static final long serialVersionUID = -1597485086838057214L;
  5     /**
  6      * 字符数组,私有最终变量,只能赋值一次
  7      */
  8     private final char[] value;
  9
 10     /**
 11      * 构造空串"",串长度为0
 12      */
 13     public MyString() {
 14         this.value = new char[0];
 15     }
 16
 17     /**
 18      * 由字符串常量str构造串,复制拷贝的过程
 19      * @param str
 20      */
 21     public MyString(java.lang.String str) {
 22         this.value = new char[str.length()];
 23         for (int i = 0; i < this.value.length; i++) {
 24             this.value[i] = str.charAt(i);
 25         }
 26     }
 27
 28     /**
 29      * 以字符数组value的i开始的count个字符构造串,i>=0,count>=0,i+count<=value.length
 30      * @param value
 31      * @param i
 32      * @param count
 33      */
 34     public MyString(char[] value, int i, int count) {
 35         if (i >= 0 && count >= 0 && i + count <= value.length) {
 36             this.value = new char[count];
 37             for (int j = 0; j < count; j++) {
 38                 this.value[j] = value[i + j];
 39             }
 40         } else {
 41             throw new StringIndexOutOfBoundsException("i=" + i + ",count=" + count + ",i+count=" + (i + count));
 42         }
 43     }
 44
 45     /**
 46      * 以整个字符数组构造串
 47      * @param value
 48      */
 49     public MyString(char[] value) {
 50         this(value, 0, value.length);
 51     }
 52
 53     /**
 54      * 以byte数组bytes的一部分构造串
 55      * @param bytes
 56      * @param begin
 57      * @param length
 58      */
 59     public MyString(byte[] bytes, int begin, int length) {
 60         if (begin >= 0 && length >= 0 && begin + length <= bytes.length && begin <= length) {
 61             this.value = new char[length];
 62             for (int i = begin; i < length; i++) {
 63                 this.value[i] = (char) (bytes[i]);
 64             }
 65         } else {
 66             throw new StringIndexOutOfBoundsException( "begin=" + begin + ",length=" + length + ",begin+length=" + (begin + length));
 67         }
 68     }
 69
 70     /**
 71      * 重载构造方法
 72      * @param bytes
 73      */
 74     public MyString(byte[] bytes) {
 75         this(bytes, 0, bytes.length);
 76     }
 77
 78     /**
 79      * 深度拷贝,复制数组
 80      * @param str
 81      */
 82     public MyString(MyString str) {
 83         this(str.value);
 84     }
 85
 86     /**
 87      * 串长度即为字符数组的长度
 88      * @return
 89      */
 90     public int length() {
 91         return this.value.length;
 92     }
 93
 94     public java.lang.String toString() {
 95         return new String(this.value);
 96     }
 97
 98     /**
 99      * 返回第index个字符,0<=index<length();
100      * @param index
101      * @return
102      */
103     public char charAt(int index) {
104         if (index >= 0 && index < this.length()) {
105             char cha = this.value[index];
106             return cha;
107         } else {
108             throw new StringIndexOutOfBoundsException("index=" + index);
109         }
110     }
111
112     /**
113      * 判断两个串是否相等
114      * @param str
115      * @return
116      */
117     public boolean equals(java.lang.String str) {
118         boolean flag = false;
119         if (this.value.length != str.length()) {
120             flag = false;
121         } else {
122             for (int i = 0; i < str.length(); i++) {
123                 if (this.value[i] != str.charAt(i)) {
124                     flag = false;
125                     break;
126                 } else {
127                     flag = true;
128                 }
129             }
130         }
131         return flag;
132     }
133
134     /**
135      * 返回序号从begin--(end-1)的子串
136      * @param begin
137      * @param end
138      * @return
139      */
140     public MyString substring(int begin, int end) {
141         if (begin >= 0 && end <= this.length() && begin <= end) {
142             if (begin == 0 && end == this.length()) {
143                 return this;
144             } else {
145                 return new MyString(this.value, begin, end - begin);
146             }
147         } else {
148             throw new StringIndexOutOfBoundsException("begin=" + begin + ",end=" + end);
149         }
150     }
151
152     /**
153      * 重载
154      * @param begin
155      * @return
156      */
157     public MyString substring(int begin) {
158         return this.substring(begin, this.length());
159     }
160
161     /**
162      * 从指定位置开始查找指定的字符的位置,没有返回-1
163      * @param cha
164      * @param begin
165      * @return
166      */
167     public int indexOf(char cha, int begin) {
168         int j = 0;
169         for (int i = begin; i < this.value.length; i++) {
170             if (cha == this.value[i]) {
171                 j = i;
172                 break;
173             } else {
174                 j = (-1);
175             }
176         }
177         return j;
178     }
179
180     /**
181      * 重载indexOf方法,从头开始查找
182      * @param cha
183      * @return
184      */
185     public int indexOf(char cha) {
186         return this.indexOf(cha, 0);
187     }
188
189     /**
190      * 查找指定的字符串的位置,未找到返回-1
191      * @param str
192      * @return
193      */
194     public int indexOf(java.lang.String str) {
195         return this.indexOf(str, 0);
196     }
197
198     /**
199      * 从指定的位置开始查找指定的串位置
200      * @param str
201      * @param begin
202      * @return
203      */
204     public int indexOf(java.lang.String str, int begin) {
205         int j = 0;
206         int strLength = str.length();
207         if (begin < 0) {
208             begin = 0;
209         }
210         if (strLength > this.length() || strLength + begin > this.length() || begin > this.length()) {
211             j = (-1);
212         } else {
213             int beginInt = this.indexOf(str.charAt(0), begin);
214             if (beginInt == (-1)) {
215                 j = (-1);
216             } else {
217                 MyString str1 = this.substring(beginInt, beginInt + strLength);
218                 for (int i = 0; i < str1.length(); i++) {
219                     if (str1.charAt(i) == str.charAt(i)) {
220                         j = beginInt;
221                     } else {
222                         j = (-1);
223                     }
224                 }
225             }
226         }
227         return j;
228     }
229 }
时间: 2024-08-09 21:43:40

常量字符串的设计与实现的相关文章

Java常量字符串String理解 String理解

以前关于String的理解仅限于三点:1.String 是final类,不可继承2.String 类比较字符串相等时时不能用“ == ”,只能用  "equals" 3.String  类不可更改 String 使用非常方便,因此一般涉及字符串时都用该类进行字符串处理至于String类的类在机制,则极少去探究. 直到读到下面这个例子. class X{     public static String strX="hello";}class Y{   public 

常量字符串和指针

为了节省内存,C++把常量字符串单独放在一个内存区域,如果有几个指针指向相同的常量字符串时,它们实际上指向的是相同的内存地址. 而数组是要每一个数组单独占用一块内存的 1 #include "stdafx.h" 2 #include <iostream> 3 using namespace std; 4 5 int _tmain(int argc, _TCHAR* argv[]) 6 { 7 char str1[]="hello world"; 8 ch

C中常量字符串使用以及g++的一个bug

const char*用于定义一个指向常量字符串的指针,通常形式为: const char* ptr = "hello"; 但可能很多粗心的朋友们没有注意到下面一种定义: const char* ptr2 = "hello"; ptr和ptr2指向的是同一片内存,即定义const char* ptr = "hello"并不等于创建新的常量字符串,而是试图返回一个常量字符串的访问地址,如果常量字符串不存在就创建常量字符串并返回常量字符串的地址,这种

ES6变量常量字符串数值

[转]ES6之变量常量字符串数值 ECMAScript 6 是 JavaScript 语言的最新一代标准,当前标准已于 2015 年 6 月正式发布,故又称 ECMAScript 2015. ES6对数据类型进行了一些扩展 在js中使用ES6必须先声明 严格模式 "use strict" let变量 let特性: 1.不允许重复声明 2.没有预解析. 3.块级作用域 一对{}包括的区域称为代码块 块级作用域指一个变量或者函数只在该区域才起作用. 例: "use strict&

IntelliJ IDEA Error:(24, 35) java: 常量字符串过长

在转换一个JSON转Java对象是 idea 编译不通过  提示:Error:(24, 35) java: 常量字符串过长 File -> Settings -> Build,Execution,Deployment -> Compiler -> Java Compiler ,  Use Compiler, 选择Eclipse , 点击 Apply 原文地址:https://www.cnblogs.com/qinxu/p/10840454.html

IDEA编译时报“常量字符串过长”问题

关于IDEA编译时报“常量字符串过长”的问题: 解决办法:File >> Settings >> Build,Execution,Deployment >> Compiler >>Java Compiler  将 Use compiler 改为 Eclipse 即可: 点击OK,重启Tomcat即可! 原文地址:https://www.cnblogs.com/abcdjava/p/12151544.html

day46homework常量字符串拼接结构赋值扩展运算符for-of循环map函数默认值

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>01定义常量.html</title> <!--常量--> <script> //常量:1.一旦定义 不能更改 const PI = 3.14;  // console.log(PI); // PI = 3.1415; error 

常量字符串为什么位于静态存储区?

char *c="zhaobei";书上说: "zhaobei"这个字符串被当作常量而且被放置在此程序的内存静态区.那一般的int i=1;1也是常量,为什么1就不被放置在此程序的内存静态区了呢?请高手指点! 所有的字符窜常量都被放在静态内存区因为字符串常量很少需要修改,放在静态内存区会提高效率 例: char str1[] = "abc";char str2[] = "abc"; const char str3[] = &q

Swift学习笔记(十四)——字符,常量字符串与变量字符串

在学习Java过程中,字符串碰到过String和StringBuffer,其中前者是不可变的,不能对字符串进行修改:后者是可变的,可以不断修改.来到Swift中,对字符串的定义变的更加简单. (1)概述 Swift中,用let 声明的是字符串常量,不能进行修改.用var声明的是字符串变量,可以修改.通过代码来演示. let str1 = "Hello1" var str2 = "Hello2" str1 = "world1"//报错:cannot