Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

help with Revesing a simple Array  RSS feed

 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given these declarations whats the easiest way to reverse this array and print the output

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please tell me what you have tried so far?
 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Can you please tell me what you have tried so far?


not much im new to java I tried searching online for a method

This is what ive done so far, but I wasn't able to use the method I found online becuase I could not understand it. Maybe you can help with a simpler method.


 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you to stop coding for some time.. Take paper and pen.. write in plane language how will you do it.. then start coding.
 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would move array 1 to array 6 so on and so fourth
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or you just started from backwards..
 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK ill try working on a method maybe you can tell me if im on the right track
 
salvin francis
Bartender
Posts: 1604
35
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this an assignment to check your coding skills ?

In practical, there are direct solutions for your problem: ArrayUtils.reverse() method exists in apache's commons.Lang. I personally would rarely do a reverse since its equally easy to loop an array in the reverse direction. Alternately, if its really required, I'll just push everything into a Stack and then just pop everything back to get a reverse.

 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry couldnt think of anything I thought of this but it will only work for this array

public static viod reverseArray(int [] number,)
int numbersTemp
numbers [0] = numbersTemp [4]
numbers [1] = numbersTemp [3]
numbers [3] = numbersTemp [1]
numbers [4] = numbersTemp [0]

System.out.println
 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Is this an assignment to check your coding skills ?

In practical, there are direct solutions for your problem: ArrayUtils.reverse() method exists in apache's commons.Lang. I personally would rarely do a reverse since its equally easy to loop an array in the reverse direction. Alternately, if its really required, I'll just push everything into a Stack and then just pop everything back to get a reverse.



well thanks for the ideas but im pretty new and this is my first course that is more focused on logic than java. can you show me an example.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
say you have array length 4 then you can do something like this:

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meki Banda wrote:well thanks for the ideas but im pretty new and this is my first course that is more focused on logic than java. can you show me an example.

It's very difficult to show you an example without writing the solution for you - and that's something we try not to do here.

My advice: Get some children's letter blocks, or 13 different playing cards from a deck and arrange them in a specific order on a table. Now try and reverse them, and note down every step you take.
Now sit back and think about what you're doing, and what it has to do with indexes and lengths.

THAT will explain the logic to you far better than we can.

And when you've done that, come back and show us what you've written; not what we've told you. We'll be very happy to help you out with any problems, but unless YOU understand the problem, you will never write a decent Java program to solve it - or if you do, it will have been by accident.

I will give you one tip though: A for loop (or indeed a program) can define more than just one variable.

HIH

Winston
 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Meki Banda wrote:well thanks for the ideas but im pretty new and this is my first course that is more focused on logic than java. can you show me an example.

It's very difficult to show you an example without writing the solution for you - and that's something we try not to do here.

My advice: Get some children's letter blocks, or 13 different playing cards from a deck and arrange them in a specific order on a table. Now try and reverse them, and note down every step you take.
Now sit back and think about what you're doing, and what it has to do with indexes and lengths.

THAT will explain the logic to you far better than we can.

And when you've done that, come back and show us what you've written; not what we've told you. We'll be very happy to help you out with any problems, but unless YOU understand the problem, you will never write a decent Java program to solve it - or if you do, it will have been by accident.

I will give you one tip though: A for loop (or indeed a program) can define more than just one variable.

HIH

Winston


thanks ill try that
 
Elim Banda
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:say you have array length 4 then you can do something like this:



Hey please explain this to me. what does the I stand for?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i is the variable you use in the for loop..

This is the pseudo code.. You have to implement it your own but it gives you some idea..
 
Campbell Ritchie
Marshal
Posts: 55734
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:. . .
And what does <- stand for, too?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i is the variable whose starting value is length and in each loop it decreases by 1 till i equals to 1. It equivalent to for loop

ex:
for (int i = length; i >= 1; i--) {.....}
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<- is like value assigning
 
Campbell Ritchie
Marshal
Posts: 55734
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case please use an assignment operator which people recognise.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i saw this notation several places during my data-structure classes when writing pseudo code.. So i just got in habit too..
 
Campbell Ritchie
Marshal
Posts: 55734
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use it to mean appending to the end of a sequence (=list).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!