Start of Tutorial > Start of Trail | Search |
The
Under Construction: We are in the process of updating and rewriting this lesson. Some sections are incomplete and are marked with [PENDING]. Continue to use our feedback form to tell us what you like and don't like about this lesson and the tutorial.
Sort
program shown below sorts the numeric values stored in an array, a data structure that can hold multiple values of the same type, and displays the sorted list. The program uses the bubble sort algorithm, a simple but inefficient algorithm, to sort the items. Running the Sort Program shows you how to run the program and the output to expect from it.public class Sort { public static void main(String[] args) { int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; for (int i = arrayOfInts.length; --i >= 0; ) { for (int j = 0; j < i; j++) { if (arrayOfInts[j] > arrayOfInts[j+1]) { int temp = arrayOfInts[j]; arrayOfInts[j] = arrayOfInts[j+1]; arrayOfInts[j+1] = temp; } } } for (int i = 0; i < arrayOfInts.length; i++) { System.out.print(arrayOfInts[i] + " "); } System.out.println(); } }
Impurity Alert: This program usingSystem.out
to display its output. UsingSystem.out
is not 100% Pure Java because some systems don't have the notion of standard output.Even a small program such as this uses many of the traditional language features of the Java programming language. This lesson describes variables, operators, expressions, and control flow statements.
Like other programming languages, Java allows you to declare variables in your programs. You use variables to hold data. All variables in Java have a type, a name, and scope.
The Java programming language provides a set of operators that you use to perform a function on one, two, or three operands.
This section discusses how to combine operators and variables into sequences known as expressions. You will also learn how to construct statements and statement blocks.
Programs use control flow statements to conditionally execute statements, to loop over statements, or to jump to another area in the program.
Start of Tutorial > Start of Trail | Search |