如何根据用户/程序员在运行时的需要自动增加数组的大小?

时间:2021-04-03 21:16:47

I am writing code of Vector class of java and all other collection classes of Java but i am having problem that how it is possible to do that the size of array increase automatically at run time.. and program run properly without giving "Array out of bound Exception" .. Well,This is my code

我正在编写java的Vector类和java的所有其他集合类的代码,但是我有一个问题,如何能够在运行时自动增加数组的大小。并且程序运行正常,不产生“绑定异常数组”。好吧,这是我的代码

/* Developed By: Nitin_Khanna Date: 1/February/2018 Github Username:beNitinhere Twitter Username:beNitinhere */ class Vector{ private static int[] A; public static long length; //Default size of Vector is 10 Vector(){ length+=10; A= new int[10]; } //One-parameterized Costructor Vector(int n){ length+=n; A= new int[n]; } //get public void get(){ int []B=new int[5]; for(int i=0;i<length();i++){ System.out.println(A[i]); } } //lenght public static long length(){ return length; } //removeLastElement public void removeLastElement(){ length=length-1; } //removeFirstElement public void removeFirstElement(){ for(int i=0;i<length();i++){ A[i]=A[i+1]; } } //clear public void clear(){ length=0; } //add public void add(int num,int index){ if(index>length()){ set(); } A[index]=num; } //remove public void remove(int index){ for(int i=index;i<length();i++){ A[i]=A[i+1]; } length-=1; } //firstElementIs public int firstElementIs(){ return A[0]; } //lastElementIs public int lastElementIs(){ return A[(int)length()-1]; } //elementAt public int elementAt(int index){ return A[index]; } private void set(){ length*=2; A=new int[(int)length]; } public void size(){ } // public boolean isEmpty(){ // } public static void main(String args[]){ Vector v1=new Vector(5); for(int i=0;i<=10;i++){ v1.add(i,i); } v1.get(); v1.removeLastElement(); System.out.println("After calling removeLastElement function"); v1.get(); System.out.println("After calling remove function"); v1.remove(2); v1.get(); } }

Thanks in advance for help..

2 .谢谢你的帮助。

3 个解决方案

#1


3  

There is no way to do so, but you can use LinkedList or implement your own solution (Ziakad was right in the comment section when he stated this should be put into the Vector class. I was just giving a general description, but in the context of the question this is a good suggestion):

没有办法这么做,但是您可以使用LinkedList或实现您自己的解决方案(Ziakad在注释部分指出应该将其放入Vector类时是正确的)。我只是做了一个大概的描述,但是在这个问题的背景下,这是一个很好的建议。

public static int[] raiseSize(int[] input, int newSize) {
    int[] output = new int[newSize];
    for (int i = 0; i < input.length; i++) output[i] = input[i];
    return output;
}

Usage example:

使用的例子:

int foo[] = new int[3];
foo[0] = 1; foo[1] = 2; foo[2] = 0;
foo = raiseSize(foo, 5);

#2


2  

Read @notyou comment above. What you should do is MAKE another array with increased length, COPY all the elements (from old array) into this array and TELL to your code that the new array (NOW) is the data array.

阅读@notyou评论。您应该做的是创建另一个增加长度的数组,将所有元素(从旧数组)复制到这个数组中,并告诉代码新的数组(现在)是数据数组。

#3


0  

Think about how an array is implemented: you specify the size of the array "in advance," and then the runtime sets aside enough consecutive memory locations to hold an array of a size you specify. The implication of this is that arrays can't be dynamically resized - you have no idea what's in the next consecutive memory location after the end of the array. Here's an illustration of an array of Boolean values of size 6:

考虑如何实现数组:您“预先”指定数组的大小,然后运行时留出足够的连续内存位置来保存指定大小的数组。这意味着数组不能被动态调整大小——您不知道数组结束后的下一个连续的内存位置是什么。这是一个布尔值的数组,大小为6:

--------------------------------------------------------------------------------------
| true  |  false | false | true | true | true (last item in the array) | ?? | ?? | ...
--------------------------------------------------------------------------------------
|  0    |   1    |  2    |  3   |  4   | 5...

Side note: an interesting property of this is that you can figure out the memory address of any particular item with the following formula: (initial memory location) + (array index * size of each memory location). For example, if the array starts at location 100 and every location is of size 2, the first item in the array is at location 100 + (2 * 0) = 100. The item at index 3 is at location 100 + (2 * 3) = 106. That's why you can access arbitrary locations in an array in constant time - you can do pointer arithmetic to find the location of an item at any arbitrary index.

附注:这方面的一个有趣特性是,您可以使用以下公式计算出任何特定项的内存地址:(初始内存位置)+(每个内存位置的数组索引*大小)。例如,如果数组从位置100开始,并且每个位置的大小都是2,那么数组中的第一个项在位置100 +(2 * 0)= 100。索引3中的项目位于位置100 +(2 * 3)= 106。这就是为什么你可以在固定时间内访问数组中的任意位置——你可以做指针运算来在任意索引中找到一个条目的位置。

TL;DR You can't dynamically resize arrays.

你不能动态调整数组大小。

#1


3  

There is no way to do so, but you can use LinkedList or implement your own solution (Ziakad was right in the comment section when he stated this should be put into the Vector class. I was just giving a general description, but in the context of the question this is a good suggestion):

没有办法这么做,但是您可以使用LinkedList或实现您自己的解决方案(Ziakad在注释部分指出应该将其放入Vector类时是正确的)。我只是做了一个大概的描述,但是在这个问题的背景下,这是一个很好的建议。

public static int[] raiseSize(int[] input, int newSize) {
    int[] output = new int[newSize];
    for (int i = 0; i < input.length; i++) output[i] = input[i];
    return output;
}

Usage example:

使用的例子:

int foo[] = new int[3];
foo[0] = 1; foo[1] = 2; foo[2] = 0;
foo = raiseSize(foo, 5);

#2


2  

Read @notyou comment above. What you should do is MAKE another array with increased length, COPY all the elements (from old array) into this array and TELL to your code that the new array (NOW) is the data array.

阅读@notyou评论。您应该做的是创建另一个增加长度的数组,将所有元素(从旧数组)复制到这个数组中,并告诉代码新的数组(现在)是数据数组。

#3


0  

Think about how an array is implemented: you specify the size of the array "in advance," and then the runtime sets aside enough consecutive memory locations to hold an array of a size you specify. The implication of this is that arrays can't be dynamically resized - you have no idea what's in the next consecutive memory location after the end of the array. Here's an illustration of an array of Boolean values of size 6:

考虑如何实现数组:您“预先”指定数组的大小,然后运行时留出足够的连续内存位置来保存指定大小的数组。这意味着数组不能被动态调整大小——您不知道数组结束后的下一个连续的内存位置是什么。这是一个布尔值的数组,大小为6:

--------------------------------------------------------------------------------------
| true  |  false | false | true | true | true (last item in the array) | ?? | ?? | ...
--------------------------------------------------------------------------------------
|  0    |   1    |  2    |  3   |  4   | 5...

Side note: an interesting property of this is that you can figure out the memory address of any particular item with the following formula: (initial memory location) + (array index * size of each memory location). For example, if the array starts at location 100 and every location is of size 2, the first item in the array is at location 100 + (2 * 0) = 100. The item at index 3 is at location 100 + (2 * 3) = 106. That's why you can access arbitrary locations in an array in constant time - you can do pointer arithmetic to find the location of an item at any arbitrary index.

附注:这方面的一个有趣特性是,您可以使用以下公式计算出任何特定项的内存地址:(初始内存位置)+(每个内存位置的数组索引*大小)。例如,如果数组从位置100开始,并且每个位置的大小都是2,那么数组中的第一个项在位置100 +(2 * 0)= 100。索引3中的项目位于位置100 +(2 * 3)= 106。这就是为什么你可以在固定时间内访问数组中的任意位置——你可以做指针运算来在任意索引中找到一个条目的位置。

TL;DR You can't dynamically resize arrays.

你不能动态调整数组大小。