二维数组的倒序问题
有个二维数组,怎样以行为单位将数组倒序。谁有好的算法,把思路讲一下,因为那个二维数组有5k多条记录,所以要追求速度。谢谢!!!
------解决方案--------------------倒它干吗的呀?
就两重循环,不能再快了吧
有一种替代的方法就是实际不倒,但通过自己写一个访问函数就可以了,如:
int getItem(int[][] array, int row, int col){
return array[row][array[row].length - col-1];
}
------解决方案--------------------JDK6的新特性之十三:JTable的排序和过滤
原来的JTable基本上是只能显示数据,在JDK6新增了对JTable的排序和过滤功能,下面代码演示了这两个功能
public class JTableTester {
static String data[][] = {
{ "China ", "Beijing ", "Chinese "},
{ "America ", "Washington ", "English "},
{ "Korea ", "Seoul ", "Korean "},
{ "Japan ", "Tokyo ", "Japanese "},
{ "France ", "Paris ", "French "},
{ "England ", "London ", "English "},
{ "Germany ", "Berlin ", "German "},
};
static String titles[] = { "Country ", "Capital ", "Language "};
public static void main(String[] args) {
DefaultTableModel m = new DefaultTableModel(data,titles);
JTable t = new JTable(m);
final TableRowSorter sorter = new TableRowSorter(m);
t.setRowSorter(sorter); //为JTable设置排序器
JScrollPane sPane = new JScrollPane();
sPane.setViewportView(t);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p,BoxLayout.X_AXIS));
JLabel l = new JLabel( "Criteria: ");
final JTextField tf = new JTextField();
JButton b = new JButton( "Do Filter ");
p.add(l);
p.add(tf);
p.add(b);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(tf.getText().length()==0){
sorter.setRowFilter(null);
}else{
sorter.setRowFilter(RowFilter.regexFilter(tf.getText()));//为JTable设置基于正则表达式的过滤条件
}
}
});
JFrame f = new JFrame( "JTable Sorting and Filtering ");
f.getContentPane().add(sPane,BorderLayout.CENTER);
f.getContentPane().add(p,BorderLayout.SOUTH);
f.setSize(400,300);
f.setVisible(true);
}
}
------解决方案--------------------定义一个比较器,用比较器排序,JTable的数据模型不要用二维数组,最好用动态数组ArrayList或者Vector
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class A {
public static void main(String[] args) {
ArrayList <int[]> a = new ArrayList <int[]> ();
a.add(new int[] { 3, 2 });
a.add(new int[] { 1, 2 });
a.add(new int[] { 4, 2 });
a.add(new int[] { 7, 2 });
a.add(new int[] { 2, 2 });
System.out.println( "原数组: ");
print(a);
Collections.sort(a, new MyCom());
System.out.println( "升序: ");
print(a);
Collections.sort(a, new MyCom2());
System.out.println( "降序: ");
print(a);
}
static void print(ArrayList <int[]> a) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a.get(i).length; j++) {
System.out.print(a.get(i)[j] + " ");