博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
运用反射类改变数组长度
阅读量:6935 次
发布时间:2019-06-27

本文共 787 字,大约阅读时间需要 2 分钟。

hot3.png

import java.lang.reflect.Array;public class ArrayExpand {public static void main(String[] args) {	int[] Test = { 1, 2, 3, 4, 5, 6,7 };	Test = (int[]) expand(Test);	for (int i = 0; i < Test.length; i++) {		System.out.println(Test[i]);	}}public static Object expand(Object array) {	int newLength=1;	if (array == null) {		return null;	}	Class c = array.getClass();	if (c.isArray()) {		int len = Array.getLength(array);// get array length		if (len >= newLength) {			return array;		} else {			Class cc = c.getComponentType();			Object newArray = Array.newInstance(cc, newLength);			System.arraycopy(array, 0, newArray, 0, len);			return newArray;		}	} else {		System.out.println("is not array");		throw new ClassCastException("need array");	}}}

转载于:https://my.oschina.net/u/435726/blog/219914

你可能感兴趣的文章
栈原理演示
查看>>
使用MASM04 - Win32汇编语言012
查看>>
B/S,C/S架构
查看>>
js实现冒泡排序
查看>>
【原】Linux find 命令整理
查看>>
Memcache
查看>>
Python 3下Matplotlib画图中文显示乱码的解决方法
查看>>
html中内联元素和块元素的区别、用法以及联系
查看>>
How to Restore “TrustedInstaller” as Default Owner of a File
查看>>
DynamicPropertyAccessor Expression lambda
查看>>
ReaderWriterLockSlimHelper
查看>>
【转载】ReadingOnDeepNetworks
查看>>
【转帖】noisex92噪声库各种噪声介绍
查看>>
Python教程5
查看>>
反射__获取delegate的信息
查看>>
个人网站搭建---godaddy域名+freewebhostingarea免费空间
查看>>
eclipse 使用lombok 精简java bean
查看>>
近日反思
查看>>
HDU - 6305 RMQ Similar Sequence(笛卡尔树)
查看>>
Be a new gentleman
查看>>