0

we have a case study to accept user input or should i say... INPUTS from user
input :: must not exceed 5 digits.
when the user inputs 0 (zero) .. it will reverse his inputs from before.
how can i reverse them??

can this could do the job?

public static void reverse(int[] b) {
   int left  = 0;          // index of leftmost element
   int right = b.length-1; // index of rightmost element
  
   while (left < right) {
      // exchange the left and right elements
      int temp = b[left]; 
      b[left]  = b[right]; 
      b[right] = temp;
     
      // move the bounds toward the center
      left++;
      right--;
   }
}//endmethod reverse
2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by JamesCherrill
0

no, i got that over the internet...
that code is for reversing the array..

will i use twoDim array?

0

It may be easier to read the numbers into a simple array them read them back out using a loop that starts with the last entry and iterates downwards to the first.

0

wait...
what if the inputs are: 12324, 9182, 21, 2134
then the reverse: 42321, 2819, 12, 4312

0

Yes, you could use a simple array for each number, and put these into an array.
Java arrays are not "2D" in the sense of being a rectangular grid. They are an array of arrays, in which any array can be a different size from all the others. It's a small shift in thinking, but important.

int[][] a = new int[5][];
      a[0]= new int[2];
      a[1] = new int[99];
      // etc
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.