本文目錄一覽:
java中如何添加數組元素
1、定義2個數組
String[] arr1 = {“1″,”2”};//定義一個字符串數組,把arr1的元素加入到arr2中
String[] arr2 = new String[2];//什麼一個字符串數組
2、循環方式把arr1的數組元素加入到arr2
for(int i=0;i2;i++){
arr2[i] = arr1[i];//把arr1的元素添加到arr2中
//在數組中增加一個元素
public static void testC() {
String [] str = {“Java”, “C++”, “Php”, “C#”, “Python”};
for (String elementA:str ) {
System.out.print(elementA + ” “);
}
//增加ruby
ListString list = new ArrayListString();
for (int i=0; istr.length; i++) {
list.add(str[i]);
}
list.add(2, “ruby”); //list.add(“ruby”)
System.out.println();
String[] newStr = list.toArray(new String[1]); //返回一個包含所有對象的指定類型的數組
for (String elementB:newStr ) {
System.out.print(elementB + ” “);
System.out.println();
Java往數組中插入新元素
往數組中添加數據有如下幾種方式:
1,int[] a={1,2,3,4,5};//已知數組內容
2,int[] b = new int[]{1,2,3,4,5};
3,int[] c=new int[5];
for(int i=1;i=5;i++){
c[i]=i;
}
希望能幫到你
JAVA 程序中如何在一個數組中添加元素???
//先聲明一個長度為10的數組
int[] a = new int[10];
//向數組中填值
a[0]=xx;
a[1]=xx;
a[2]=xx;
…
若是已經定義好的數據,例如int num[] = { 1,4,5,8,2,6 }; 形式的,只能新建立一個數組,利用循環把原數據中的值加入你新創建的數據,在添加你自己想要加入的值!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/246814.html