Not important at this point. You need to work out the logic. Forget about writing code until you have the logic
See my last post for my suggestion.


I think i might of got it now. here is what i was thinking you have an array [abcd] want to shift the D to the first place.
Step 1 copy the last element to temp variable
temp=array[array.length-1]; // temp know hold ths D
Step 2 Shift all the elements of the array up by one
array=array[i+1] // not sure if that will do the shift still trying to figure that code out. I can use some help on that please.
Step 3 copy the temp variable in to the first element of the array.

Step 4. print out array.

I think this should work but i am not sure. Is my logic correct ?


greenman - I notice that you often reverse the lvalue and the rvalue in your assignment statements. Your English-language description is correct, but your code has you assigning things the wrong way around. You do this in step 2 and step 3 here, and I saw this in previous code segments. You'll want to watch this - I don't know whether this is due to linguistic origin (you might have grown up speaking a language which is written right to left) or to some other cause, but in any case it'll be a source of bugs for you.

In step 2, you say "shift all the elements up by one". This suggests to me that you want a loop, with i as the control variable, but this isn't in the code. Is this what you meant? If so, what values does i take, and in what order?

Also in step 2, the code as you write it would shift a variable to the left - from a higher position in the array to a lower position. As I say above, this makes more sense if you reverse the assignment to read
Is this what you meant?
(imagine that i == 2. What happens to the array if you execute that line as you have it?)

Step 3, also, you would need to reverse the assignment to agree with your statement of the logic - your statement is correct, your code is wrong.

Step 4, however, suggests that you're not clear on how arrays work. What do you expect that println statement to put out?

Edited by jon.kiparsky: n/a


Thanks for the help i will give that all a try and let you know how it works out.
As for step 4 i do not now much about array but that print statement at the end was kinda hoping it would print out the correct output.
System.out.printf("%d",array) // i am hoping it prints the array as
"DABC" might be incorrect. As for Step 2 i would like i to be my control variable that is what i ment. My code should be to get the right output
int [] array = new array[4] // that will be ABCD for example
int i;

     array[i]=i;   // intilize elements to ABCD
     temp=array[array.length-1];  // copy D to temp
     array[i+1]=array[i]  // it should shift elements up by one  is that correct ? 
     array[0]=temp        //copys D in to first element of the array so we get DABC
  System.out.printf("%d",array[i]);   // i was thinking out side loop prints elements

Question ?
i want to print the elements DABC can i do it in the loop or should i do it outside the loop ?
Another Question
for the above example you get input from a user and they type in 2 so the output would be CDAB I should put lines 6,7,8 from above code in a while loop but what condition should i be testing against
x=2 // user entered 2 for example to get that output seen earlier
While(x> do i test it against the array length ? ) am i correct on that ?
or do i test it agains something else because i want the loop throw till you get the correct output.

Edited by greenman626: n/a


Is there a way to print the array with a print statment ? To shift the other way would this code be correct


output before shift example
After shift

What i am tring to do is reverse the order so it shift all the elements back.


Printing arrays - this is important because you have to understand what an array is. Imagine a bottle of beer - it's a container ("variable"), with some beer ("data") inside it. Now think of a case of beer. It's a container ("array") with a bunch of bottles in it. In order to get some beer out of the case, you first have to get a bottle, then you have to open the bottle and access the data - sorry, the beer.
If you open the case and you forget where you left your bottle opener (these aren't twist-off, sorry) then you're out of luck.

It's not a great analogy, but it'll help.

Typing "s.o.println array[0]" is, in this analogy, like saying "drink the first beer". "s.o.println array" would be "Whatever number i is, count that many beers and drink that one". (if i is more than 24, you don't get any beer - that's an array out of bounds exception). What you're trying to do is to come up with a way to drink all the beers in the case.
So, since you can't just say "drink all the beers", how do you do it? Don't do it in code, do it in terms of beers.

(okay, array.toString() is sort of like "drink all the beers", but it's a little limited. Try it, see if it does what you want)


Put comments on each statement in your code to show what it does:

temp=array.length-1;  // put the [B]index of[/B] the last element of the array in temp
  array[i+1]=array[i];  // copy contents of array at i to i+1 (ie one slot to up)
  array[4]=temp;        // set the 5th element of the array to the[B] index of[/B] the last element

Doesn't make any sense?
What was this code supposed to do?

I think you are trying to write code before you use a paper and pencil to design what needs to be done.

Edited by NormR1: n/a


My last post was about printing the array. Now, to shift the contents of the array, take a six-pack out of the case. Number the bottles. What steps will you take to arrange things so that the six-pack starts as
1 2 3
4 5 6

and ends up as

3 4 5
6 1 2

You can use one hand to hold a bottle and one hand to move a bottle, otherwise each bottle must be in the six-pack at all times.
How are you going to do it? Not in code, in words.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.