Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-27-2004, 02:51 AM   PM User | #1
Antoniohawk
Senior Coder

 
Join Date: Aug 2002
Location: Kansas City, Kansas
Posts: 1,518
Thanks: 0
Thanked 2 Times in 2 Posts
Antoniohawk will become famous soon enough
Global Variables In Java

How would I declare the variable numbers as global? I want to be able to access the same variable from any method so that the value of the variable is the same.
Code:
import TerminalIO.KeyboardReader;

public class SortAndDestroy{
	
	public static void main (String[] args){
		int[] numbers = {42,37,5,33,6};
		KeyboardReader reader = new KeyboardReader();
		int choice;
		
		for(int i = 0; i < Names.numbers.length; i++){
			System.out.print(numbers[i] + "  ");
		}
		
		choice = reader.readInt("\nSelection[1] or Bubble[2] Sort?  ");
		
		if (choice == 1)
			selectionSort(numbers);
		else if (choice == 2)
			bubbleSort(numbers);
		else
			System.out.println("That is not a choice.");
	}
	
	public static void selectionSort(int[] numbers){
		for(int i = 0; i < numbers.length; i++){
			int minIndex = findMinimum(numbers, i);
			if(minIndex != 1)
				swap(numbers, i, minIndex);
			
			System.out.print(numbers[i] + "  ");
		}
	}
	
	public static int findMinimum(int[] numbers, int first){
		int minIndex = first;
		
		for(int i = first + 1; i < numbers.length; i++)
			if(numbers[i] < numbers[minIndex])
				minIndex = i;
			
			return minIndex;
	}
	
	public static void swap(int[] numbers, int x, int y){
		int temp = numbers[x];
		numbers[x] = numbers[y];
		numbers[y] = temp;
	}
	
	public static void bubbleSort(int[] numbers){
		int k = 0;
		boolean exchangeMade = true;
		
		while((k < numbers.length - 1) && exchangeMade){
			exchangeMade = false;
			k++;
			for(int j = 0; j < numbers.length - k; j++)
				if(numbers[j] > numbers[j + 1]){
					swap(numbers,j,j + 1);
					exchangeMade = true;
				}
		}
	}
}
Antoniohawk is offline   Reply With Quote
Old 05-27-2004, 04:47 AM   PM User | #2
hkucsis
New Coder

 
Join Date: Mar 2003
Location: Hong Kong
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
hkucsis is an unknown quantity at this point
public class SortAndDestroy{
int[] numbers = {42,37,5,33,6};
public static void main (String[] args){
...
hkucsis is offline   Reply With Quote
Old 05-27-2004, 06:00 AM   PM User | #3
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
That's usually a bad idea. You don't want to give everyone access to a local variable. Instead make the variable private but create a public method within that class that returns the value.

Edit: Never mind. I misread your question. I thought you wanted to give other classes access to the variables in that class. As was posted already just declare the variable outside of your methods. That will put it into the scope of all your methods.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!

Last edited by Spookster; 05-27-2004 at 06:03 AM..
Spookster is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:39 AM.


Advertisement
Log in to turn off these ads.