Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 19th, 2013, 01:48 AM
Authorized User
 
Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Difficulty Understanding Code

The following is a block of code from a book. It is part of an alphabetization program for a string of words in a paragraph earlier in the program. I get how the program works but I don't understand the following:

// Sort the array of words
String temp = null;
boolean exchange = true;
while(exchange) {
exchange = false;
for(int i = 1 ; i < words.length ; ++i) {
if(words[i-1].compareTo(words[i]) > 0) {
temp = words[i];
words[i] = words[i-1];
words[i-1] = temp;
exchange = true;
}
}
}

// If exchange is set to false then does the rest of the code block execute?

Thank you for your time.

Last edited by alex001; April 20th, 2013 at 04:02 PM..
 
Old April 21st, 2013, 02:59 PM
Registered User
 
Join Date: Apr 2013
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by alex001 View Post
The following is a block of code from a book. It is part of an alphabetization program for a string of words in a paragraph earlier in the program. I get how the program works but I don't understand the following:

// Sort the array of words
String temp = null;
boolean exchange = true;
while(exchange) {
exchange = false;
for(int i = 1 ; i < words.length ; ++i) {
if(words[i-1].compareTo(words[i]) > 0) {
temp = words[i];
words[i] = words[i-1];
words[i-1] = temp;
exchange = true;
}
}
}

// If exchange is set to false then does the rest of the code block execute?

Thank you for your time.
================================================== ======

I'm a beginner too, but i think is like this:

String temp = null; //this is an auxiliar variable, that helps to interchange the words, once they are found not in order
boolean exchange = true; // this bool variable assume that the whole phrase is not in alphabetical order
while(exchange) { // while the phrase is not in alphabetical order...
exchange = false; //change the value of exchange
for(int i = 1 ; i < words.length ; ++i) { // loop through phrase
if(words[i-1].compareTo(words[i]) > 0) { //starts with the first word("words[i-1] - which we found him at index 0), and compare him with the next one(the one at index 1). If the integer value returned is positive, means that the first word is greater alphabeticaly than the second word, so we must interchange the words between them. If the integer value returned is negative, means that the first word is less alphabeticaly than the second word. If the integer is 0, means that the two words are equal.
temp = words[i]; // the auxiliar value temp equals second word
words[i] = words[i-1]; // second word equals first word
words[i-1] = temp; // first word equals temp value
exchange = true;
}
}
}

Hope I'm not wrong! GL
The Following User Says Thank You to pjanooo For This Useful Post:
Viol3t (May 26th, 2013)
 
Old May 26th, 2013, 12:30 AM
Registered User
 
Join Date: May 2013
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I am also a beginner to java. I think that kind of sorting is like the bubble sorting of the arrays. Am i right??? :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Understanding code-behind file morteza BOOK: Beginning ASP.NET 4 : in C# and VB 4 November 27th, 2011 02:29 PM
Can some one explain this code Please ! I am having a hard time understanding this MohamedHassanAli BOOK: Professional C# 2008 ISBN: 978-0-470-19137-8 1 March 4th, 2011 01:06 AM
NOT UNDERSTANDING CODE!!! biswa VB Databases Basics 1 October 9th, 2009 06:04 PM
Understanding code bcrossb BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 2 March 5th, 2009 02:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.