DESKTOP-D9IGLU5_20200827-赵继豪
1、问题
package com.itcainiao.cs.chars;
public class CharSequenceTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
char[] chars = { '不', '破', '楼', '兰', '终', '不', '还' };
CharSequence cs = new Characters(chars);
System.out.println(cs); // cs.toString()
chars[2] = '隆';
System.out.println(cs); // cs.toString()
System.out.println(cs.length());
for (int x = 0, n = cs.length(); x < n; x++) {
char ch = cs.charAt(x);
System.out.println(ch);
}
CharSequence x = cs.subSequence(4, 4);
System.out.println(x);
}
}
很疑惑,当start 和 end相等的时候,数组下标指向同一个字符,是不是应该输出的是他们同时指向的这个字符,而不是null。
public CharSequence subSequence(int start, int end) {
if(start >= 0 && end > start && end <= chars.length) {
int n = end - start;
char[] array = new char[n];
System.arraycopy(this.chars, start, array, 0, n);
Characters c =new Characters(array);
return c;
}
return null;
}
如果start = end ,按照这个方法就会输出null。
然后,跟别人交流了一下,稍微改动
public CharSequence subSequence(int start, int end) {
if(start >= 0 && end >= start && end <= chars.length) {
int n;
if(start == end) {
n = 1;
}
else {
n = end - start;
}
char[] array = new char[n];
System.arraycopy(this.chars, start, array, 0, n);
Characters c =new Characters(array);
return c;
}
return null;
}
这样就合适了
2、吐槽
今天换了座位,或许能改变改变之前迷的状态
点赞