Results 1 to 4 of 4
  1. #1
    kris09 is offline Member
    Join Date
    Aug 2008
    Posts
    6
    Rep Power
    0

    Default What Excatly is this code doing????

    I am confused from the for loop part its getting really confusing the program is working but i dont understand it that much why is there two for loops

    public class LeastToGreatestPlaceHolder {

    public static void main(String []args) {
    int [] myArray;
    myArray = new int[3];
    myArray[0] = 6;
    myArray[1] = 7;
    myArray[2] = 9;

    for (int i = 0; i < 2 ; i++){ <------ Confused at here
    for(int j = 1; j < 3; j++) {
    int placeholder = myArray[j];
    if (myArray[i] > myArray[j]) {
    myArray[j] = myArray[i];
    myArray[i] = placeholder;
    }
    }
    }
    System.out.print(myArray[0]);
    System.out.print(myArray[1]);
    System.out.print(myArray[2]);


    }
    }

  2. #2
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    The code inside the two for loops is a "sorting algorithm." Google bubble sort to get more info. BTW, it's much easier to read your code if you post it inside code tags and format it.

    Java Code:
    public class LeastToGreatestPlaceHolder {
    
    	public static void main(String[] args) {
    		int[] myArray;
    		myArray = new int[3];
    		myArray[0] = 9;
    		myArray[1] = 7;
    		myArray[2] = 6;
    
    		for (int i = 0; i < 2; i++) { // <------ Confused at here
    			for (int j = 1; j < 3; j++) {
    				int placeholder = myArray[j];
    				if (myArray[i] > myArray[j]) {
    					myArray[j] = myArray[i];
    					myArray[i] = placeholder;
    				}
    			}
    		}
    		System.out.print(myArray[0]);
    		System.out.print(myArray[1]);
    		System.out.print(myArray[2]);
    
    	}
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    cross-posted in the Sun forums and lord knows where else.

  4. #4
    Juggler's Avatar
    Juggler is offline Member
    Join Date
    Jul 2008
    Location
    Chennai,India
    Posts
    16
    Rep Power
    0

    Default hai

    That loop is for help to geting the array values for sorting....... :):):)

Similar Threads

  1. code for 20;-)
    By sparky2003752 in forum JCreator
    Replies: 0
    Last Post: 04-17-2008, 06:31 PM
  2. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 11:12 PM
  3. Pls some one to help mi wit this code
    By _nik_ in forum New To Java
    Replies: 3
    Last Post: 02-10-2008, 03:02 AM
  4. I need help on my code
    By jason27131 in forum New To Java
    Replies: 4
    Last Post: 07-28-2007, 05:23 AM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •