Advanced array techniques – Java tutorial

Advanced array techniques – Java tutorial

Arrays can be very useful when it comes to sorting. If you are sorting from the lowest value to the highest, you are sorting in ascending order. If you are sorting the highest value to the lowest, you are sorting in descending order. Another sorting method called bubble sort sorts an unsorted array by swapping values until they are from lowest to highest. For instance, the values 88, 33, 99, 22, 54 will be swapped until they read 22, 33 54, 88, 99. Below is a program showing how bubble sorting works.

public class SortCharArray
{
public static void main(String[] args) throws Exception
{
// Declaring a character array that can hold 10 characters, and int x as subscript
char[] someChars = new char[10];
int x;

// for loop that allows user to enter 10 characters
for(x = 0; x < someChars.length; ++x)
{
System.out.print("Enter a character ");
someChars[x] = (char)System.in.read();
System.in.read(); System.in.read();
}

//for loop that displays characters as entered
System.out.println("Before sorting");
for(x = 0; x < someChars.length; ++x)
System.out.print(someChars[x] + " ");

// Call method named bubbleSort and pass two arguments
bubbleSort(someChars, someChars.length);

// for loop that prints sorted characters
System.out.println("\nAfter sorting");
for(x = 0; x < someChars.length; ++x)
System.out.print(someChars[x] + " ");

// Final print statement
System.out.println();
}

public static void bubbleSort(char[] array, int len)
{
// Declaring int a and b for looping and char temp variable
int a, b;
char temp;

// Nested for loops to sort array
int highSubscript = len - 1;
// Declaring variable for comparisons
int compsToMake = len - 1;
for(a = 0; a < highSubscript; ++a)
{
for(b = 0; b < compsToMake; ++b)
if(array[b] > array[b + 1])
{
temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
--compsToMake;
}
}
}

Advanced array techniques

Arrays can be very useful when it comes to sorting. If you are sorting from the lowest value to the highest, you are sorting in ascending order. If you are sorting the highest value to the lowest, you are sorting in descending order. Another sorting method called bubble sort sorts an unsorted array by swapping values until they are from lowest to highest. For instance, the values 88, 33, 99, 22, 54 will be swapped until they read 22, 33 54, 88, 99. Below is a program showing how bubble sorting works.

public class SortCharArray
{
public static void main(String[] args) throws Exception
{
// Declaring a character array that can hold 10 characters, and int x as subscript
char[] someChars = new char[10];
int x;

// for loop that allows user to enter 10 characters
for(x = 0; x < someChars.length; ++x)
{
System.out.print("Enter a character ");
someChars[x] = (char)System.in.read();
System.in.read(); System.in.read();
}

//for loop that displays characters as entered
System.out.println("Before sorting");
for(x = 0; x < someChars.length; ++x)
System.out.print(someChars[x] + " ");

// Call method named bubbleSort and pass two arguments
bubbleSort(someChars, someChars.length);

// for loop that prints sorted characters
System.out.println("\nAfter sorting");
for(x = 0; x < someChars.length; ++x)
System.out.print(someChars[x] + " ");

// Final print statement
System.out.println();
}

public static void bubbleSort(char[] array, int len)
{
// Declaring int a and b for looping and char temp variable
int a, b;
char temp;

// Nested for loops to sort array
int highSubscript = len - 1;
// Declaring variable for comparisons
int compsToMake = len - 1;
for(a = 0; a < highSubscript; ++a)
{
for(b = 0; b < compsToMake; ++b)
if(array[b] > array[b + 1])
{
temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
--compsToMake;
}
}
}

Characters and numbers are a simple form of sorting. You can also sort Strings. One important point to remember with Strings is that the names are memory addresses. In this sense, you must use the compareTo() method.

public class SortStrings
{
public static void main(String[] args)
{
String[] students = {"Kim", "Ken", "Tom", "Kathy", "Brad"};
int x;
System.out.println("Before sorting");
for(x = 0; x < 5; ++x)
System.out.println(students[x]);
sortStrings(students, students.length);
System.out.println("\nAfter sorting");
for(x = 0; x < 5; ++x)
System.out.println(students[x]);
}
public static void sortStrings(String[] array, int len)
{
int a, b;
String temp;
int highSubscript = len - 1;
for(a = 0; a < highSubscript; ++a)
for(b = 0; b < highSubscript; ++b)
if(array[b].compareTo(array[b + 1]) > 0)
{
temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
}
}

As you may have noticed, these arrays print in the form of a single column. These are called single-dimensional arrays. To achieve the effect of a table, you need more than one column. This is what we call a two-dimensional array (also known as a matrix or a spreadsheet). The way you create a two-dimensional array is:

int[][] someNumbers =
{ {2, 4, 6, 8},
{4, 16, 36, 64},
{3, 13, 31, 57} };

One problem with Strings is that you cannot have myName = “John” and myName = “Smith” at the same time. One thing you can do is use the StringBuffer to join two strings. If you typed StringBuffer myName = new StringBuffer(“John”);, and then typed myName.append(” Smith”);, you’d end up with the String “John Smith”. The append() method adds a String to a String similar to the way concatenation works. Along with these methods is the insert() that you can use to insert a String. For instance, if you wanted to add your middle name to the String. You would then type myName.insert(5, “Alexander “);. Seeing as Strings work the same way as arrays, the letter “J” in John is in the [0] position of the entire String. The number “5” means go to the [5] element and insert “Alexander”. If you just wanted to add “A”, you would type myName.setCharAt(5, “A”); instead of the insert() method. Then, put a space inside the parenthesis after John.

If you’re not too sure about what you’ve learned so far, or if you seem to be having problems in the future, there are some newsgroups you can attend:

comp.lang.java.advocacy
comp.lang.java.announce
comp.lang.java.api
comp.lang.java.beans
comp.lang.java.gui
comp.lang.java.help
comp.lang.java.misc
comp.lang.java.programmer
comp.lang.java.security
comp.lang.java.setup
comp.lang.java.softwaretools
comp.lang.java.tech

If you can’t find the answer you’re looking for at any sites, you can always take a look at Frequently Asked Questions or the Help file at the Sun Microsystems Web site.

Question: Ok, I’ve learned a great deal coming here. The only thing I want to know is how I can apply all this knowledge to my Web page. Is there a way?

Answer: Well, I’m glad to hear that you’ve learned something besides patience. Now, to be able to transfer what you’ve learned to the Internet, you need to create an applet.