Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Arrays

 
Greenhorn
Posts: 2
  • 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
 
Ranch Hand
Posts: 63
  • 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.
 
Ranch Hand
Posts: 42
  • 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
  • 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 )
 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this code :
 
Elisabeth Van
Ranch Hand
Posts: 42
  • 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?
 
"The Hood"
Posts: 8521
  • 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).]
 
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!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic