我和其他所有人都说要使用类似 an 的东西ArrayList
,但是如果您别无选择,您可以使用System.arraycopy
将内容从原始数组复制到一个新的临时数组并将结果分配回原始数组 ( word
)。
这将减小数组的大小并删除字符...
public class ArrayDelete {
// This is because I'm to lazy to build the character
// array by hand myself...
private static String text = "This is an example of text";
private static char word[] = text.toCharArray();
public static void main(String[] args) {
System.out.println(new String(word));
delete(9);
System.out.println(new String(word));
}
public static void delete(int charAt) {
if (word.length > 0 && charAt >= 0 && charAt < word.length) {
char[] fix = new char[word.length - 1];
System.arraycopy(word, 0, fix, 0, charAt);
System.arraycopy(word, charAt + 1, fix, charAt, word.length - charAt - 1);
word = fix;
}
}
}
此示例输出...
This is an example of text
This is a example of text