• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java using methods

bdario1

n00b
Joined
Mar 24, 2010
Messages
5
I am writing a method that needs to do this.

/**
* This method reverses 'in place' the elements in
* the array. Reversing is defined as moving the
* elements in the array such that the first element
* becomes the last element, the second element
* becomes the second to last element, etc.
* <p>

here is what i have so far
Code:
public static void reverseOrder (Character[] symbols)
    {
    	for(int reverse = (symbols.length - 1); reverse >= 0; reverse--)
        {
    		
    		Character c = symbols[reverse];
            System.out.print(c);
       }
    	
    }

Here is my tester class.

Code:
Character [] newlist = new Character[]{'a','b','c','f','n','q','s'};
        System.out.println("");
        System.out.print ("The array [");
        separator = "";
        for (Character s : newlist)
        {
            System.out.print(separator + s);
            separator = ", ";
        }
        System.out.print ("] entered ");
        
        ArrayExercises.reverseOrder(newlist);
        System.out.print ("looks like this in reverse order[");
        separator = "";
        for (Character s : newlist)
        {
            System.out.print(separator + s);
            separator = ", ";
        }
        System.out.print ("] ");

Which prints the result like this
The array [a, b, c, f, n, q, s] entered sqnfcbalooks like this in reverse order[a, b, c, f, n, q, s]

But it needs to print it like this.

The array [a, b, c, f, n, q, s] entered looks like this in reverse order[s, q, n, f, c, b, a]

and i can not use library classes to manipulate or search through arrays
 
Are you supposed to print them out in reverse order or actually reverse the array?

I've never used/looked at Java before but have been using C / C++ and it looks pretty similar.. and to me it just looks like you are trying to print it out in reverse order, not actually move the elements.

And yea I just noticed it... right after "entered" it printed the characters in reverse order but you never modified the array.
 
What blazestorm said.

You're not propogating the changes to the array.

What I recommend you do is change the return type of the method to be the type of list you're reversing. Use the original list as an argument, and then make a new list in the method that will hold the changes and return the object. That way it's nice and easy.
 
What blazestorm said.

You're not propogating the changes to the array.

What I recommend you do is change the return type of the method to be the type of list you're reversing. Use the original list as an argument, and then make a new list in the method that will hold the changes and return the object. That way it's nice and easy.

I may be off here, but to me 'in place' means without allocating another array. So I'm not sure that solution is valid for the exercise. That being said, this can be done with a void return method.

There are methods as part of the normal JavaSE API that can do this operation for you but I'm assuming that would also defeat the point of the exercise.
 
Code:
public static void reverseOrder (Character[] symbols)
This method should reverse the order of elements in the input array.

For reverseOrder, the input array {'a', 'b', 'c'} should be changed to be {'c', 'b', 'a'}. Note that the type is Character, and not char, but that Java will convert between these two types automatically.
And i can not change the method stays public static void reverseOrder (Character[] symbols), so i also need to test them to the fullest extent
So i guess it will be changing the input array. Also they will test all the possible variations including unusual parameter values, such as arrays with no elements in them or parameters with illegal values. (Note that with a properly coded solution, you don't need special checks for these kinds of things.)
 
you may also want to think about how to do an inplace reversing.

if the first needs to go to the last position, the last goes to the first position
and the second goes to the second to last position, and vice versa
you should be able to turn that into a for loop. You'll need one temporary variable to hold the contents of what you are overwriting, until you can put it back where the thing you overwrote it came from.

bonus hint, your loop wont be 0 to the length of the array, you'll be done before that.
 
double bonus hint. Check to make sure the input array is not null before you do anything with it!
 
Could you give me an example so i can see how you have it imagined cause i am now probably more confused than before.
 
Example:

You have an array of 'a', 'b', 'c' and you want to transpose them so the output is 'c', 'b', 'a.' You need to loop through the array (from 0 to array.length-1 [-1 because 0 is the first element]) and reverse them. So index 0 becomes array.length-1, index 1 becomes array.length-2, index 2 becomes array.length-3. Now you just need to make a loop that increases the current position by 1 each time and voila. Of course, you don't want to use the same array because then you'll be overwriting values, so you need to use a temporary array. And also look out for things like empty arrays, null references, etc.
 
It's been 7 years since I used Java so here is some pseudo code that should help clear some things up. I'm pretty sure this is what you are looking for.

Code:
function reverseOrder( inputArray ) {
  outputArray [] = new Array();
 
  arrayLen = inputArray.length-1;

  x = 0;

  for( i=arrayLen; i >= 0; i-- ) {
    outputArray[x] = inputArray[i];
    x++;
  }

  return outputArray;
}
 
Example:

You have an array of 'a', 'b', 'c' and you want to transpose them so the output is 'c', 'b', 'a.' You need to loop through the array (from 0 to array.length-1 [-1 because 0 is the first element]) and reverse them. So index 0 becomes array.length-1, index 1 becomes array.length-2, index 2 becomes array.length-3. Now you just need to make a loop that increases the current position by 1 each time and voila. Of course, you don't want to use the same array because then you'll be overwriting values, so you need to use a temporary array. And also look out for things like empty arrays, null references, etc.

If you do a full swap per loop (temp=end element, beginning element to end, end element(temp) to beginning):

you can do it without a temporary array, you just need a temp var to hold one element in the array.

You also dont need to go to length-1, you'll be done long before that.

Also consider in an array of odd length, versus even length: which elements needs to be swapped, and which dont?
 
Last edited:
Back
Top