4 Replies - 107 Views - Last Post: 12 September 2012 - 03:58 PM Rate Topic: -----

#1 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 103
  • Joined: 27-January 10

Calling a static Method in Java

Posted 12 September 2012 - 03:38 PM

I have not worked with static methods very much, and I am confused why code does not appear to be working properly. Basically, I want to take in an array, then print the array in reverse order. My method is required to be static. Here is my code:

public class VariousMethods {

private VariousMethods () {	
}

public static void reverseArray (int[] array) {
	int temp;
	int length = array.length;
	
	for (int i = 0; i < length; i++) {
		temp = array[length - 1];
		array[i] = temp;
		--length;
	}
	for (int j = 0; j < length; j++) {
		System.out.print (array[j] + " ");
	}
	System.out.println ("");
}

public static void main (String[] args) {
	int[] array1 = {1, 2, 3};
	int[] array2 = {6, 7, 8, 9, 6, 5};
	int[] array3 = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
	
	VariousMethods.reverseArray(array1);
	VariousMethods.reverseArray(array2);
	VariousMethods.reverseArray(array3);
}
}

/* 
OUTPUT:
3 
5 6 9 
0 1 2 3 4
*/



However, the code only prints half of the reversed array and I am unsure why. Any explanation as to why this is happening is very appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Calling a static Method in Java

#2 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Calling a static Method in Java

Posted 12 September 2012 - 03:45 PM

Because you are changing your length variable. its going to be half the size as you iterate through half of it.

get rid of the --length;

you want to do something like this: array[(length - 1)- i]
Was This Post Helpful? 2
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,555
  • Joined: 21-June 11

Re: Calling a static Method in Java

Posted 12 September 2012 - 03:50 PM

The fact that your method is static has nothing to do with your problem.

Your problem is that your second loop uses the length variable which has been changed by the first loop and is now equal to array.length/2 because the first loop decremented length and incremented i until both met in the middle.
Was This Post Helpful? 1
  • +
  • -

#4 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 103
  • Joined: 27-January 10

Re: Calling a static Method in Java

Posted 12 September 2012 - 03:53 PM

Oh wow, I had a derp moment right there. Problem fixed. I have literally been banging my head on my desk trying to figure this out when the solution was right there in my face. Thank you!
Was This Post Helpful? 0
  • +
  • -

#5 WannaJava?  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 27
  • Joined: 11-September 12

Re: Calling a static Method in Java

Posted 12 September 2012 - 03:58 PM

Have you tried using a recursion to iterate through the array backwards instead? It might be easier to set i to arrayName.length - 1 and and decrement i each time and say print arrayName[i] while i > -1

That is my suggestion, I took your code and rewrote it how I said and it works well
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1