This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays" Watch "Arrays" New topic


Bobby Jones

Joined: Feb 12, 2001
Posts: 2
How do I an array of size 'n' and move all values two to the right and store the last two values in the first two? What's the code for this?
Pat Barrett
Ranch Hand

Joined: Jan 03, 2001
Posts: 63
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[0] = array[n-1]
5) newarray[1] = 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.
Elisabeth Van
Ranch Hand

Joined: Feb 09, 2001
Posts: 42
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.
Elisabeth Van
Ranch Hand

Joined: Feb 09, 2001
Posts: 42
Oh wait, just realized that I misinterpreted the question. :O
Still, arraycopy could be used:
System.arraycopy( origArray , 0 , newArray , 2 , n-2 );
newArray[0] = origArray[n-2];
newArray[1] = origArray[n-1];
Again, where n = number of elements in origArray
(And now I see yet another error in my original posting :O :O )
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
check this code :

Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Elisabeth Van
Ranch Hand

Joined: Feb 09, 2001
Posts: 42
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?
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
println is overloaded to take a multitude of different inputs. From the API:

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').
public void println(boolean x)
public void println(char x)

public void println(int x)
public void println(long x)

public void println(float x)
public void println(double x)
public void println(char[] x)
public void println(String x)
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
It is sorta covered in the JavaRanch Style Guide.
subject: Arrays
Similar Threads
Difference between '==' & equals()
range of datatype
question on if loop
finally clause cannot complete normally ???
Parameter Passing