java实现数组字符插入问题

/*
 * 题目:
 * 给出一串按顺序排列的字符,将另一个字符按顺序插入其中
 * 
 */


public class charInsert {
public static void main(String[] args) {


// 这是要插入的字符
char c = 'w';
// 这是字符数组
char[] ch = new char[9];
ch[0] = 'a';
ch[1] = 'b';
ch[2] = 'c';
ch[3] = 'f';
ch[4] = 'g';
ch[5] = 'i';
ch[6] = 'l';
ch[7] = 'z';


// 打印插入之前字符排列顺序
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i] + " ");
}


// index为假设字符要插入的位置
int index = ch.length;


// 通过for循环找到要插入的位置
for (int j = 0; j < ch.length; j++) {
if (c < ch[j]) {
index = j;
break;
}
}


// 打印要插入的位置看一下是否有问题
// System.out.println(index);


// 这是很重要的一步将数组的数据进行下移
for (int k = ch.length - 1; k > 0; k--) {
ch[k] = ch[k - 1];
}


// 将数据下移后空出的位置就可以插入数据了
ch[index] = c;


// 查看插入后的数组数据
for (int l = 0; l < ch.length; l++) {
System.out.print(ch[l] + " ");
}


}

}

如果你想把最后一个字母换成y插入字符z 那么这个代码是不对的应该这样写

public class charInsert {
public static void main(String[] args) {


// 这是要插入的字符
char c = 'z';
// 这是字符数组
char[] ch = new char[9];
ch[0] = 'a';
ch[1] = 'b';
ch[2] = 'c';
ch[3] = 'f';
ch[4] = 'g';
ch[5] = 'i';
ch[6] = 'l';
ch[7] = 'w';


// 打印插入之前字符排列顺序
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i] + " ");
}


// index为假设字符要插入的位置
int index = ch.length-1;


// 通过for循环找到要插入的位置
for (int j = 0; j < ch.length; j++) {
if (c < ch[j]) {
index = j;
break;
}
}


// 打印要插入的位置看一下是否有问题
System.out.println("index:"+index);
 
// 这是很重要的一步将数组的数据进行下移 如果要插入的位置在最后一个就不需要进行数据下移了
if(index != ch.length-1){
for (int k = ch.length - 1; k > 0; k--) {
ch[k] = ch[k - 1];
}
}


// 将数据下移后空出的位置就可以插入数据了
ch[index] = c;


// 查看插入后的数组数据
for (int l = 0; l < ch.length; l++) {
System.out.print(ch[l] + " ");
}


}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中,字符数组是一种固定长度的数据结构,无法像 ArrayList 一样直接插入数据。但可以通过以下两种方式实现类似的效果: 1. 使用 System.arraycopy 方法 通过 System.arraycopy 方法可以将原数组中的数据复制到一个新的数组中,并在指定位置插入新的数据。示例如下: ``` String[] array = {"A", "B", "C", "D", "E"}; String[] newArray = new String[array.length + 1]; int insertIndex = 2; String insertValue = "X"; System.arraycopy(array, 0, newArray, 0, insertIndex); newArray[insertIndex] = insertValue; System.arraycopy(array, insertIndex, newArray, insertIndex + 1, array.length - insertIndex); ``` 这定义了原数组 array,新数组 newArray 和要插入的数据 insertValue,以及插入位置 insertIndex。首先使用 System.arraycopy 方法将原数组中 0 到 insertIndex 的数据复制到新数组中,然后在 insertIndex 处插入新数据,最后将原数组中 insertIndex 之后的数据复制到新数组中 insertIndex + 1 的位置之后。 2. 使用 ArrayList 转换 可以将字符数组转换为 ArrayList,使用 ArrayList 的 add 方法插入数据,最后再将 ArrayList 转换回字符数组。示例如下: ``` String[] array = {"A", "B", "C", "D", "E"}; List<String> list = new ArrayList<>(Arrays.asList(array)); int insertIndex = 2; String insertValue = "X"; list.add(insertIndex, insertValue); String[] newArray = list.toArray(new String[0]); ``` 这先将字符数组 array 转换为 ArrayList,然后使用 add 方法在指定位置插入数据,最后再将 ArrayList 转换回字符数组 newArray。 注意:以上两种方法均会创建新的数组或集合对象,而不是在原数组中直接插入数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值