Hello, Once an array has been declared it could not be resized. You would need to create a new array and move the values over. I'd use pseudo code such as; 1) Determine the length of the current array (n = array.length) 2) Create a new array of length n + 2 (int newArray = new int[array.length];. 3) For i = 0 to n-2, newarray[i+2] = array[i]. 4) newarray = array[n-1] 5) newarray = array[n] Of course, there may be a more efficient way of doing this but this would be a good way to start. Pat B.
The System.arraycopy method would come in quite handy here. After you create a new array that has the length you desire, you would use System.arraycopy( origArray , 0 , n , newArray , 2 , n ) where n is the length of the original array. See the API for more info.
posted 19 years ago
Oh wait, just realized that I misinterpreted the question. :O Still, arraycopy could be used: System.arraycopy( origArray , 0 , newArray , 2 , n-2 ); newArray = origArray[n-2]; newArray = origArray[n-1]; Again, where n = number of elements in origArray (And now I see yet another error in my original posting :O :O )
Cool! I like how that works (hadn't tested it when I suggested it) Parmeet, did you hard-code the length of the array for convenience? I'd have used int len = arrayFrom.length Also surprised that the System.out.println(arrayFrom) and System.out.println(arrayTo) methods worked. Thought you would have simply gotten the references to the arrays. Perhaps this is because of the array being of type char, so a toString method could be invoked on it? Anyone know?
println is overloaded to take a multitude of different inputs. From the API:
println public void println() Terminate the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n'). println public void println(boolean x) println public void println(char x)
println public void println(int x) println public void println(long x)
println public void println(float x) println public void println(double x) println public void println(char x) println public void println(String x) println public void println(Object x)
[This message has been edited by Cindy Glass (edited February 14, 2001).]
"JavaRanch, where the deer and the Certified play" - David O'Meara
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!