java第五章异常处理_异常处理系列教材 (五)- Java 自定义异常

代码比较

复制代码

package character;

import exception.IndexIsNagetiveException;

import exception.IndexIsOutofRangeException;

public class MyStringBuffer implements IStringBuffer {

int capacity = 16;

int length = 0;

char[] value;

public MyStringBuffer() {

value = new char[capacity];

}

// 有参构造方法

public MyStringBuffer(String str) {

this();

if (null == str)

return;

if (capacity < str.length()) {

capacity = value.length * 2;

value = new char[capacity];

}

if (capacity >= str.length())

System.arraycopy(str.toCharArray(), 0, value, 0, str.length());

length = str.length();

}

@Override

public void append(String str) throws IndexIsNagetiveException, IndexIsOutofRangeException {

insert(length, str);

}

@Override

public void append(char c) throws IndexIsNagetiveException, IndexIsOutofRangeException {

append(String.valueOf(c));

}

@Override

public void insert(int pos, char b) throws IndexIsNagetiveException, IndexIsOutofRangeException {

insert(pos, String.valueOf(b));

}

@Override

public void delete(int start) throws IndexIsNagetiveException, IndexIsOutofRangeException {

delete(start, length);

}

@Override

public void delete(int start, int end) throws IndexIsNagetiveException, IndexIsOutofRangeException {

// 边界条件判断

if (start < 0)

throw new IndexIsNagetiveException();

if (start > length)

throw new IndexIsOutofRangeException();

if (end < 0)

throw new IndexIsNagetiveException();

if (end > length)

throw new IndexIsOutofRangeException();

if (start >= end)

throw new IndexIsOutofRangeException();

System.arraycopy(value, end, value, start, length - end);

length -= end - start;

}

@Override

public void reverse() {

for (int i = 0; i < length / 2; i++) {

char temp = value[i];

value[i] = value[length - i - 1];

value[length - i - 1] = temp;

}

}

@Override

public int length() {

// TODO Auto-generated method stub

return length;

}

@Override

public void insert(int pos, String b) throws IndexIsNagetiveException, IndexIsOutofRangeException {

// 边界条件判断

if (pos < 0)

throw new IndexIsNagetiveException();

if (pos > length)

throw new IndexIsOutofRangeException();

if (null == b)

throw new NullPointerException();

// 扩容

if (length + b.length() > capacity) {

capacity = (int) ((length + b.length()) * 2.5f);

char[] newValue = new char[capacity];

System.arraycopy(value, 0, newValue, 0, length);

value = newValue;

}

char[] cs = b.toCharArray();

// 先把已经存在的数据往后移

System.arraycopy(value, pos, value, pos + cs.length, length - pos);

// 把要插入的数据插入到指定位置

System.arraycopy(cs, 0, value, pos, cs.length);

length = length + cs.length;

}

public String toString() {

char[] realValue = new char[length];

System.arraycopy(value, 0, realValue, 0, length);

return new String(realValue);

}

public static void main(String[] args) {

try {

MyStringBuffer sb = new MyStringBuffer("there light");

System.out.println(sb);

sb.insert(0, "let ");

System.out.println(sb);

sb.insert(10, "be ");

System.out.println(sb);

sb.insert(0, "God Say:");

System.out.println(sb);

sb.append("!");

System.out.println(sb);

sb.append('?');

System.out.println(sb);

sb.reverse();

System.out.println(sb);

sb.reverse();

System.out.println(sb);

sb.delete(0, 4);

System.out.println(sb);

sb.delete(4);

System.out.println(sb);

} catch (IndexIsNagetiveException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IndexIsOutofRangeException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

package character;

import exception.IndexIsNagetiveException;

import exception.IndexIsOutofRangeException;

public class MyStringBuffer implements IStringBuffer {

int capacity = 16;

int length = 0;

char[] value;

public MyStringBuffer() {

value = new char[capacity];

}

// 有参构造方法

public MyStringBuffer(String str) {

this();

if (null == str)

return;

if (capacity < str.length()) {

capacity = value.length * 2;

value = new char[capacity];

}

if (capacity >= str.length())

System.arraycopy(str.toCharArray(), 0, value, 0, str.length());

length = str.length();

}

@Override

public void append(String str) throws IndexIsNagetiveException, IndexIsOutofRangeException {

insert(length, str);

}

@Override

public void append(char c) throws IndexIsNagetiveException, IndexIsOutofRangeException {

append(String.valueOf(c));

}

@Override

public void insert(int pos, char b) throws IndexIsNagetiveException, IndexIsOutofRangeException {

insert(pos, String.valueOf(b));

}

@Override

public void delete(int start) throws IndexIsNagetiveException, IndexIsOutofRangeException {

delete(start, length);

}

@Override

public void delete(int start, int end) throws IndexIsNagetiveException, IndexIsOutofRangeException {

// 边界条件判断

if (start < 0)

throw new IndexIsNagetiveException();

if (start > length)

throw new IndexIsOutofRangeException();

if (end < 0)

throw new IndexIsNagetiveException();

if (end > length)

throw new IndexIsOutofRangeException();

if (start >= end)

throw new IndexIsOutofRangeException();

System.arraycopy(value, end, value, start, length - end);

length -= end - start;

}

@Override

public void reverse() {

for (int i = 0; i < length / 2; i++) {

char temp = value[i];

value[i] = value[length - i - 1];

value[length - i - 1] = temp;

}

}

@Override

public int length() {

// TODO Auto-generated method stub

return length;

}

@Override

public void insert(int pos, String b) throws IndexIsNagetiveException, IndexIsOutofRangeException {

// 边界条件判断

if (pos < 0)

throw new IndexIsNagetiveException();

if (pos > length)

throw new IndexIsOutofRangeException();

if (null == b)

throw new NullPointerException();

// 扩容

if (length + b.length() > capacity) {

capacity = (int) ((length + b.length()) * 2.5f);

char[] newValue = new char[capacity];

System.arraycopy(value, 0, newValue, 0, length);

value = newValue;

}

char[] cs = b.toCharArray();

// 先把已经存在的数据往后移

System.arraycopy(value, pos, value, pos + cs.length, length - pos);

// 把要插入的数据插入到指定位置

System.arraycopy(cs, 0, value, pos, cs.length);

length = length + cs.length;

}

public String toString() {

char[] realValue = new char[length];

System.arraycopy(value, 0, realValue, 0, length);

return new String(realValue);

}

public static void main(String[] args) {

try {

MyStringBuffer sb = new MyStringBuffer("there light");

System.out.println(sb);

sb.insert(0, "let ");

System.out.println(sb);

sb.insert(10, "be ");

System.out.println(sb);

sb.insert(0, "God Say:");

System.out.println(sb);

sb.append("!");

System.out.println(sb);

sb.append('?');

System.out.println(sb);

sb.reverse();

System.out.println(sb);

sb.reverse();

System.out.println(sb);

sb.delete(0, 4);

System.out.println(sb);

sb.delete(4);

System.out.println(sb);

} catch (IndexIsNagetiveException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IndexIsOutofRangeException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值