[OT] Help with Java


log in or register to remove this ad



You meant "array of strings," right? You said string of arrays. :) Look this up in your reference books or at java.sun.com:

java.util.Arrays

More specifically, you are looking for the sort method there that just takes an object. Since the String class implements the comparable interface it should work. They use the quicksort algorithm so it's fast.

Does that get you up and running? Is this self study or a class? Are you supposed to be using the API's or are you supposed to be learning algorithm fundamentals and writing your own sort algorithm?
 

kenjib said:
You meant "array of strings," right? You said string of arrays. :)

I was curious, too, but decided not to ask.

Seriously, this isn't much to go on. Are you supposed to learn the fundamentals of programming (bubble sort is as basic as it gets)? Dop you just need a pre-written sort program? Do you need to learn how to write more efficient sort algorithms?
 

Simple program I worte in a couple mins using a bubble sort...

Code:
[color=white]
import java.util.*;

public class Sorter {

	public static void main(String args[]) {

		String array[] = {"this", "is", "a", "simple", "example"};

		System.out.println("Unsorted:");
		printArray(array);

		sortStringArray(array);
		
		System.out.println("\nSorted:");
		printArray(array);
	}

	public static void sortStringArray(String array[]) {
		String temp;
		
		for (int counter = 0; counter < array.length - 1; counter++) {
			for (int loc = 0; loc < array.length - counter - 1; loc++) {
				// option:  change "compareToIgnoreCase" to "compareTo"
				// this will treat capital letters differently
				if (array[loc].compareToIgnoreCase(array[loc + 1]) > 0) {
					temp = array[loc];
					array[loc] = array[loc + 1];
					array[loc + 1] = temp;
				}
			}
		}
	}
	
	public static void printArray(String array[]) {
		for (int counter = 0; counter < array.length; counter++)
			System.out.println("" + counter + ")\t" + array[counter]);
	}
}
[/color]

Yep, I'm a computer scientist alright....
 


By the way, check out the bubble sort vs. quick sort demo on the Sun site:

http://java.sun.com/applets/jdk/1.0/demo/SortDemo/index.html

Click on the three pictures to see how long each method takes.

For commonly used routines like sort functions and data structures like vectors, linked lists, etc. it's usually best to use pre-made packages when available rather than writing your own code. There's no point in reinventing the wheel 100 times unless you have some very specific needs which the API doesn't cover. Plus you know that the one built into the java API is reliable and fast.

Quicksort R0><><0R5 j00

EDIT: I'm talking about day to day usage here, not usage for learning to be a good programmer. In that case you need to learn how Bubblesort works and why it works the way it does. Then you need to learn how Quicksort works and why it works the way it does to see why it's better. For this kind of thing, hands-on coding and trial and error are the best ways to learn. It gives good insight into the scalability of various algorithms, which is a very important concept.
 
Last edited:

Thanks everyone.

After studing for 3 days of what was suppose to be on the test, the teacher descided to give us programs to work on instead. Was suppose to take an hour, he figured it about that long, but we never did a sort so I was kind of unclear as to where to begin. As it was, I ended up running out of time, but I finished 2 of the 3 questions. Not bad for someone who never actually written Java before.

Well, just wanted to thank everyone for the help.

Akh
 

Remove ads

Top