如何扩大二维数组的大小(任何类型的通用版)
2012-10-21 15:10
281 查看
下面给大家介绍一种人为扩大二维数组大小的一种方法。
第一个函数是扩大一维数组的,第二个是扩大二维数组的:
运算结果:
第一个函数是扩大一维数组的,第二个是扩大二维数组的:
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(); } } }
运算结果:
相关文章推荐
- 如何修改上传文件大小及类型的限制 - [MOSS 2007应用日记]
- discuz如何设置上传附件大小及类型
- 关于Delphi中二维数组的声明和大小调整(对非基本类型数据,小心内存泄漏)
- 如何扩大VMware虚拟机中Linux硬盘大小- VM7.1.2 & FC6
- 如何将任何文件类型数据保存到数据库中
- C语言如何获取某个数据类型或变量的大小?
- 关于 “如何传递一个大小不定的二维数组给函数” 的一个小想法
- 如何扩大虚拟机硬盘容量大小
- .NET : 如何查看值类型的大小
- C# 如何获取某个类型或类型实例对象的大小
- 两个Long类型如何比较大小
- 如何更改NUMBER类型为指定的大小
- sql server2000 中字符串类型的日期如何比较大小
- js中相等、大小 不同类型之间是如何进行对比的。
- 我给女朋友讲编程CSS系列(3) CSS如何设置字体的类型、大小、颜色,如何使用火狐浏览器的Firebug插件查看网页的字体
- C#中如何比较2个string类型的日期大小?
- double类型如何比较大小
- 如何扩大重做日志(redolog)文件的大小
- 如何扩大VMware虚拟机中Linux硬盘大小- VM7.1.2 & FC6
- C#中如何比较2个string类型的日期大小?