How can I increase size of array automatically as

2019-07-19 07:03发布

问题:

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

/* 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..

回答1:

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):

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:

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.



回答3:

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:

--------------------------------------------------------------------------------------
| 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.

TL;DR You can't dynamically resize arrays.