如何扩大二维数组的大小(任何类型的通用版)

下面给大家介绍一种人为扩大二维数组大小的一种方法。

第一个函数是扩大一维数组的,第二个是扩大二维数组的:

import java.lang.reflect.Array;

public class ResizeArray{
	/**
	 * 为一个新数组分配新空间,并将原来数组的内容拷贝到新数组中去
	 * @param oldArray  the old array, to be reallocated. 原来的数组,将被重新分配空间
	 * @param newSize   the new array size.新数组的大小
	 * @return  A new array with the same contents. 存放原来数组内容的新数组
	 */
	private static Object resizeArray (Object oldArray, int newSize) {
		int oldSize =Array.getLength(oldArray);
		Class elementType = oldArray.getClass().getComponentType();
		Object newArray = Array.newInstance(
				elementType,newSize);
		int preserveLength = Math.min(oldSize,newSize);
		if (preserveLength > 0)
			System.arraycopy (oldArray,0,newArray,0,preserveLength);
		return newArray; 
	}

	/**
	 * 为一个二维数组分配新空间,并将原来数组的内容拷贝到新数组中去
	 * @param oldArray  the old array, to be reallocated. 
	 * @param newRowSize   row size of the new array 
	 * @param newColumnSize  column size of the new array. 
	 * @return    A new array	 */
	private Object resize2DArray(Object oldArray, int newRowSize,
            int newColumnSize) {
		Object[][] old2DArray = (Object[][]) oldArray;
        Object[][] new2DArray = (Object[][]) java.lang.reflect.Array
                .newInstance(old2DArray.getClass().getComponentType(),
                        newRowSize);
        String className = new2DArray.getClass().getName();
        className = className.substring(className.indexOf("[L")+2, className
                .length() - 1);
        Class elementType = null;
        try {
        	elementType = Class.forName(className);
        } catch (ClassNotFoundException e) {
            // 打开注释部分兼容性更强 适合复杂情况的非基础数据类型的加载
//            try {
//                elementType = Class.forName(className, false, Thread.currentThread()
//                        .getContextClassLoader());
//            } catch (ClassNotFoundException e1) {
//                try {
//                    elementType = Class.forName(className, false, ResizeArray.class
//                            .getClassLoader());
//                } catch (ClassNotFoundException e2) {
//                    e2.printStackTrace();
//                }
//            }
            e.printStackTrace();
        }
        for (int i = 0; i < Math.max(old2DArray.length, new2DArray.length); i++) {
            new2DArray[i] = (Object[]) java.lang.reflect.Array.newInstance(
            		elementType, newColumnSize);
            /*System.arraycopy(old2DArray[i], 0, new2DArray[i], 0, Math.min(
                    old2DArray[i].length, newColumnSize));*/
        }
        return new2DArray;

    }


	public static void main (String[] args) {
		int[] testArray = {1,2,3};
		testArray = (int[])resizeArray(testArray,5);
		testArray [3] = 4;
		testArray [4] = 5;
		for (int element : testArray)
			System.out.print (element + " "); 
		System.out.println("\n");

		Integer test2DArray[][] = {{1,2,3},{4,5,6}};
		test2DArray = (Integer[][])new ResizeArray().resize2DArray(test2DArray, 5, 10);
		int i,j;
		for (i = 0; i < test2DArray.length; i++) {
            for (j = 0; test2DArray[i] != null && j < test2DArray[i].length; j++) {
                test2DArray[i][j]  = i*j;
            }
		}
		for (i = 0; i < test2DArray.length; i++) {
            for (j = 0; test2DArray[i] != null && j < test2DArray[i].length; j++) {
                System.out.print(test2DArray[i][j] + "\t");
            }
            System.out.println();
		}

	}
}

 

运算结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值