Archive

Posts Tagged ‘Java’

Enable boxing errors in Eclipse

September 16th, 2010 No comments

Java 1.5 (5.0) and above support autoboxing conversions for primitives and wrapper objects. This will unclutter the code and give more flexibility to programmers, however this can have a negative impact on performance and can cause some unexpected behaviors. (Use boxing with care).

Eclipse by default will throw a warning if there are boxing and unboxing conversions in the code. If you want eclipse to throw an error during these conversions, follow the steps below –

Eclipse –> Window –> Preferences –> Java –> Compiler –> Errors/Warnings

scrn_enable_eclipse_autoboxing_error

If you want to learn more autoboxing in Java click here


Share
Categories: Tech Tips Tags: ,

Download JDK 1.5 (direct link)

August 25th, 2010 1 comment


Here is a direct link to download Java 1.5.0.22 or Java 5.0 update 22. Going directly to this link will save time registering at oracle.com and waiting for an email with download link.


http://java.sun.com/javase/downloads/5u22/jdk

Alternate option is to download from CNET.

Share
Categories: Tech Tips Tags:

How to Sort a Two Dimensional String Array using JAVA

February 15th, 2010 No comments

Recently I had to write a program to sort data (String) in a table and was looking for an efficient and easiest way to do sorting in java. I found two options – one was to write my own sorting program, and the other was to use the Comparator class in the Util package. I found that using Comparator or Comparable interface was very easy compared to writing my own sorting algorithm.

If you are new to using “Comparator”, I highly recommend reading this one page tutorial.
http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

Here is a working example for sorting a two dimensional string array:

import java.util.Arrays;
import java.util.Comparator;

public class Sort {	
	public static void main(String args[]){
		//Array with address to sort
		String[][] addressArray = new String[][]{
				{"E","abc st","Sunshine", "FL"},
				{"S","abc st","Sunrise", "FL"},
				{"E","abd st","Cleveland", "OH"},
				{"N","aab st","Dallas", "TX"},
				{"W","xyz st","San Antonio", "TX"}};
				
		//Sort the array by city name or column 3
		Arrays.sort(addressArray, new ColumnComparator(2));
		
		//Print the sorted array
		for(int i=0; i<addressArray.length; i++){
			String[] row = addressArray[i];
			for(int j=0; j<row.length; j++) {
				System.out.print(row[j] + " | ");
			}
			System.out.print("\n");
		}
	}
}
//Class that extends Comparator
class ColumnComparator implements Comparator {
	int columnToSort;
	ColumnComparator(int columnToSort) {
		this.columnToSort = columnToSort;
	}
	//overriding compare method
	public int compare(Object o1, Object o2) {
		String[] row1 = (String[]) o1;
		String[] row2 = (String[]) o2;
		//compare the columns to sort
		return row1[columnToSort].compareTo(row2[columnToSort]);
	}
}

Results – sorted by city name

E | abd st | Cleveland | OH | 
N | aab st | Dallas | TX | 
W | xyz st | San Antonio | TX | 
S | abc st | Sunrise | FL | 
E | abc st | Sunshine | FL | 
Share
Categories: Tech Tips Tags: , ,