File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays

 
Bobby Jones
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks
 
Pat Barrett
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 358
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this code :
 
Elisabeth Van
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic