Category ArchiveJava
Java & Programming 15 Oct 2007 08:02 pm
Reverse an Array in Place
I was just looking through some code. I noticed that an array needed to be reversed and that the original order was never needed again. This was a perfect time to reverse the array in place. This was Java so I looked in the Arrays class. But alas there wasn't a reverse for primitive arrays! Java Lists get a reverse method in the Collections class. But primitive arrays get no such love.
Also, I thought of using the asList method from Arrays, but that just seems like a ton of overhead.
So instead I wrote a quick little method for ints and any other primitive type.
Here is the int example:
-
public static void reverseInPlace(int[] iar) {
-
final int length = iar.length;
-
final int halfLength = length / 2;
-
final int lengthMinus1 = length - 1;
-
for (int i = 0; i <halfLength; ++i) {
-
final int index2 = lengthMinus1 - i;
-
-
//swap
-
int tmp = iar[i];
-
iar[i] = iar[index2];
-
iar[index2] = tmp;
-
}
-
}
Basically, this method just swaps the items from the front to back until reaching the middle of the array. If the loop continued on, it would just switch everything back and the original order would be preserved!
The other implementations for the rest of the primitive types can be found here.
Java & Programming 05 Oct 2007 01:33 pm
A Good Reasoning to Nullify an Object!
If you overwrite a reference with a new object, the object is first created and /then/ the reference is overwritten, which means the object can be only GCed /after/ the new object has been created.
Usually this doesn’t matter. However, if you want to overwrite an object which is so big that it only fits once into the memory, you’ll need to null the reference before creating/assigning the new instance.
Eg:
//FatObject fits only once into memory
FatObject fatty;
fatty=new FatObject();
fatty=new FatObject();Will bomb with OOME. Whereas…
FatObject fatty;
fatty=new FatObject();
fatty=null;
fatty=new FatObject();Will be fine, because the second creation of the FatObject will trigger a full GC and the GC will be able to clear enough memory (since the old reference has been nulled).
Well, that rarely matters, but it’s good to know.
Jos Hirth wrote this in response to this post by Jeroen van Erp.
In this post, Jeroen was talking about a long standing bug/issue/misunderstanding of how Strings are handled in Java. The problem that occurs only comes up when taking a substring of a large String. If all that is wanted is the small substring, the substring must be wrapped by a new String call. This will allow the larger String to be garbage collected. Normally when a substring is taken, the underlying char array is shared. This is usually good practice, because it saves memory.
But, and this is a big but, if the substring is taken from a large string, then memory can be leaked, because a lot of memory is wasted in the underlying character array.
Now, why did I quote the above passage from the comment area? Cause I had never thought of the issue Jos brought up. It's interesting to think of an object of which only one can fit in memory. Let alone creating a second!
Java & Programming & Technology 27 Jun 2006 04:53 pm
Changing the Color of a JCheckBox check in a JTable
Ever wanted to change the color of a JCheckBox in a JTable to the table's foreground color? Now you can!
Example Screenshot:

This is a renderer for Booleans in a JTable.
-
import java.awt.Color;
-
import java.awt.Component;
-
import java.awt.Graphics;
-
-
import javax.swing.JCheckBox;
-
import javax.swing.JTable;
-
import javax.swing.table.TableCellRenderer;
-
-
/**
-
* This class implements a boolean renderer for a JTable. It overrides the icon for the CheckBox to draw in the
-
* foreground color.
-
*/
-
public BooleanRenderer() {
-
super();
-
setIcon(new ColorableMetalCheckBoxIcon());
-
}
-
-
boolean isSelected, boolean hasFocus, int row, int column) {
-
if (isSelected) {
-
setForeground(table.getSelectionForeground());
-
super.setBackground(table.getSelectionBackground());
-
} else {
-
setForeground(table.getForeground());
-
setBackground(table.getBackground());
-
}
-
return this;
-
}
-
-
/**
-
* Changes the check box check mark to be the foreground color.<br/>
-
* <b>NOTE:</b>This was found here http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4449413 .
-
*/
-
g.setColor(c.getForeground());
-
super.drawCheck(c, g, x, y);
-
g.setColor(old);
-
}
-
}
-
}
This is the corresponding Editor.
-
import java.awt.Component;
-
-
import javax.swing.DefaultCellEditor;
-
import javax.swing.JCheckBox;
-
import javax.swing.JTable;
-
-
/**
-
* This class implements a boolean editor for a JTable. It overrides the icon for the CheckBox to draw in the
-
* foreground color.
-
*/
-
-
public BooleanEditor() {
-
c.setIcon(new BooleanRenderer.ColorableMetalCheckBoxIcon());
-
}
-
-
Object value,
-
boolean isSelected,
-
int row,
-
int column) {
-
if (isSelected) {
-
c.setForeground(table.getSelectionForeground());
-
c.setBackground(table.getSelectionBackground());
-
} else {
-
c.setForeground(table.getForeground());
-
c.setBackground(table.getBackground());
-
}
-
return c;
-
}
-
}
Quick and Dirty Tester Program!
-
import java.awt.Color;
-
import java.awt.Dimension;
-
import java.awt.FlowLayout;
-
-
import javax.swing.*;
-
import javax.swing.table.AbstractTableModel;
-
import javax.swing.table.DefaultTableModel;
-
-
/**
-
* shows how to use the BooleanEditor and the BooleanRenderer.
-
*/
-
public JTableBooleanTester(){
-
-
-
-
TestTableModel ttm = new TestTableModel();
-
-
sp1.setPreferredSize(dim);
-
-
sp2.setPreferredSize(dim);
-
-
getContentPane().add(sp1);
-
getContentPane().add(sp2);
-
-
pack();
-
}
-
-
public void run() {
-
frame.setVisible(true);
-
}
-
-
});
-
}
-
-
-
public int getRowCount() {
-
return data.length;
-
}
-
-
public int getColumnCount() {
-
-
return names.length;
-
}
-
-
return data[rowIndex][columnIndex];
-
}
-
-
switch(columnIndex){
-
case 0:
-
data[rowIndex][columnIndex] = aValue;
-
}
-
fireTableDataChanged();
-
}
-
-
public boolean isCellEditable(int rowIndex, int columnIndex) {
-
return 0 == columnIndex;
-
}
-
-
public Class getColumnClass(int columnIndex){
-
return data[0][columnIndex].getClass();
-
}
-
}
-
}