Subctracting from an arrays size help

November 23, 2012 at 14:02:29
Specs: Windows 7
I'm trying ways to subtract from an arrays size. So as an example if the array were to be a length of 4, i want to be able to size it down to be a length of 3. my code chunk looks as so:
           while(true){
                      try{
				for(k=hold;; k++){	
					hold = k;
					System.out.print(">");
					a[k] = rtrunc(input.nextDouble());
					if(a[k] != 0 && a[k] != -1){
						//Copy the array to a temp.
						temp = (double[])a.clone();
						//Extend the size of the main array.
						a = new double [a.length + 1];
						//Copy the array from temp back to the main array(a).
						System.arraycopy(temp, 0, a, 0, temp.length);
					} else if(a[k] == 0 && a[k] != -1){
						//create a temp to hold the temp.
						double []holdTemp = (double[])temp.clone();
						//extend the size of the temp.
						temp = new double [temp.length - 1];
						//copy the contents of hold temp to temp.
						System.arraycopy(holdTemp, 0, temp, 0, temp.length);
						//change the size of the main array(a).
						a = new double [a.length - 1];
						//copy the contents of temp to main array(a).
						System.arraycopy(temp, 0, a, 0, temp.length);
						//go back one array element.
						k--;
					}
					
					System.out.print("[");
					for(int i=0; i<temp.length; i++){
						System.out.print(a[i] + ",");
					}
					System.out.print("x]");
					System.out.print("("+ a.length +")(" + temp.length + ")");
				}
			} catch(Exception Error){
				System.err.println("Error has been found as: " + Error.getMessage());
				k = hold;
			}
                 }

I can delete the first added amount but once i try to enter any new value it then throws an exception on the length of the array. Does anyone know how to do this without using an array list of vector?


See More: Subctracting from an arrays size help

Report •


#1
February 3, 2013 at 17:50:25
Please make your question more clear. To me, it sounds like you want to use the following:

array.size()
or
vector.size()


Report •
Related Solutions


Ask Question