「java数组添加数组」java数组添加元素方法
今天给各位分享java数组添加数组的知识,其中也会对java数组添加元素方法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java中如何插入数组
按照你的要求编写的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=0;iarray2.length;i++){
System.out.print(array2[i]+" ");
}
}
}
运行结果
10 20 1 2 3 4 5 6 7 8 9 30 40 50
在java中怎么在数组里增加一组数组
ArrayList list=new ArrayListList();//声明一个泛型为List的ArrayList,调用ArrayList的addAll方法将现有数组或者List添加进该list即可
java数组插入数组
import java.util.*;
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 数组如何添加数据?
public class ListInsert {
public static long[] insert(long[] arr,int i,long l){
//新建数组,对原数组扩容
long[] arr1 = new long[arr.length+1];
//将原数组数据赋值给新数组
for(int j = 0;jarr.length;j++){
arr1[j] = arr[j];
}
//将大于i的数据向后移动一位
for(int j = arr1.length-2;ji;j--){
arr1[j+1] = arr1[j];
}
//赋值到i位置
arr1[i+1] = l;
return arr1;
}
//测试
public static void main(String[] args){
long[] arr = {12,25,11,36,14};
long[] arr1 = insert(arr, 2, 100);
for (long l : arr1) {
System.out.print(l+" ");
}
}
数组的扩容方式:arr = Arrays.copyOf(arr, arr.length+1);
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。
Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等
java数组添加数组的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java数组添加元素方法、java数组添加数组的信息别忘了在本站进行查找喔。