class Tester{
public static void main (String[] args) {
char[] cs = {'a','b','c','e','f','p','u','z'}
System.out.format("插入前的序列是:%s%n", Arrays.toString(cs).replaceAll("[\\[\\],]",""))
Scanner sc = new Scanner(System.in)
System.out.print("待插入的字符是:")
String m = sc.next()
System.out.println(m)
System.out.print("插入字符的下标是:")
int index = sc.nextInt()
System.out.println(index)
sc.close()
char[] dest = new char[cs.length+1]
System.arraycopy(cs,0,dest,0,index)
dest[index] = m.charAt(0)
System.arraycopy(cs,index,dest,index+1,dest.length-index-1)
cs = dest
System.out.format("插入后的字符序列是:%s", Arrays.toString(cs).replaceAll("[\\[\\],]",""))
}
}
按照你的要求编写的Java程序如下(用array3做临时数组,组合完再拷贝回数组2)
public class GGG {public static void main(String[] args) {
int[] array1={1,2,3,4,5,6,7,8,9}
int[] array2={10,20,30,40,50}
int[] array3=new int[array1.length+array2.length]
System.arraycopy(array2, 0, array3, 0, 2)
System.arraycopy(array1, 0, array3, 2, array1.length)
System.arraycopy(array2, 2, array3, 2+array1.length, 3)
array2=new int[array3.length]
System.arraycopy(array3, 0, array2, 0, array3.length)
for(int i=0i<array2.lengthi++){
System.out.print(array2[i]+" ")
}
}
}
运行结果
10 20 1 2 3 4 5 6 7 8 9 30 40 50
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)