java中将数组的长度扩大1的方法,有吗
答案:5 悬赏:80
解决时间 2021-03-01 00:05
- 提问者网友:贪了杯
- 2021-02-28 07:29
java中将数组的长度扩大1的方法,有吗
最佳答案
- 二级知识专家网友:長槍戰八方
- 2021-02-28 07:57
java中没有关于修改数组长度的api,在此提供了修改数组长度的两个函数:arrayAddLength()和arrayReduceLength()。
import java.lang.reflect.Array;
public class ArrayTest {
public static void main(String[] args) {
int a[]= new int[]{0,1,2,3,4,5};
int b[]= new int[]{0,1,2,3,4,5};
a = (int[]) arrayAddLength(a,2);
b = (int[]) arrayReduceLength(b,2);
//out print array lenght
System.out.println(a.length);
for (int i=0;iSystem.out.print(a[i]);
}
System.out.println();
System.out.println(b.length);
for (int i=0;i System.out.print(b[i]);
}
}
public static Object arrayAddLength(Object oldArray,int addLength) {
Class c = oldArray.getClass();
if(!c.isArray())return null;
Class componentType = c.getComponentType();
int length = Array.getLength(oldArray);
int newLength = length + addLength;
Object newArray = Array.newInstance(componentType,newLength);
System.arraycopy(oldArray,0,newArray,0,length);
return newArray;
}
public static Object arrayReduceLength(Object oldArray,int reduceLength) {
Class c = oldArray.getClass();
if(!c.isArray())return null;
Class componentType = c.getComponentType();
int length = Array.getLength(oldArray);
int newLength = length - reduceLength;
Object newArray = Array.newInstance(componentType,newLength);
System.arraycopy(oldArray,0,newArray,0,newLength);
return newArray;
}
}
import java.lang.reflect.Array;
public class ArrayTest {
public static void main(String[] args) {
int a[]= new int[]{0,1,2,3,4,5};
int b[]= new int[]{0,1,2,3,4,5};
a = (int[]) arrayAddLength(a,2);
b = (int[]) arrayReduceLength(b,2);
//out print array lenght
System.out.println(a.length);
for (int i=0;iSystem.out.print(a[i]);
}
System.out.println();
System.out.println(b.length);
for (int i=0;i
}
}
public static Object arrayAddLength(Object oldArray,int addLength) {
Class c = oldArray.getClass();
if(!c.isArray())return null;
Class componentType = c.getComponentType();
int length = Array.getLength(oldArray);
int newLength = length + addLength;
Object newArray = Array.newInstance(componentType,newLength);
System.arraycopy(oldArray,0,newArray,0,length);
return newArray;
}
public static Object arrayReduceLength(Object oldArray,int reduceLength) {
Class c = oldArray.getClass();
if(!c.isArray())return null;
Class componentType = c.getComponentType();
int length = Array.getLength(oldArray);
int newLength = length - reduceLength;
Object newArray = Array.newInstance(componentType,newLength);
System.arraycopy(oldArray,0,newArray,0,newLength);
return newArray;
}
}
全部回答
- 1楼网友:猎心人
- 2021-02-28 11:02
只能用arraylist吧,数组的长度是确定的。
- 2楼网友:渡鹤影
- 2021-02-28 09:42
你往数组里面插入一个对象就ok了,长度就扩大1
- 3楼网友:几近狂妄
- 2021-02-28 09:28
java中数组的长度是固定的,不能够改变。如果说有需求要经常改变长度,建议你使用list集合,将数组转换为集合的方法: Lista=Arrays.asList(数组名);注意:asList的方法参数必须是对象,所以如果你的数组是int类型的,那么需要改变为Integer类型!!
- 4楼网友:七十二街
- 2021-02-28 08:59
java中数组在声明的时候就需要给出长度或者存储的对象,这个时候数组的长度已经固定了,对于这个数组来说,它的长度是不能再改变的,除非你重新创建一个数组对象。
如果长度需要改变,建议用集合
如果长度需要改变,建议用集合
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯