一道java笔试题

这是某公司的一道java笔试题,题目内容如下:
写一个java程序,实现对一个二维数组按指定的列集进行排序?要求实现类似sql中order by的功能,移动时,整行移动,不能打乱整行顺序。
可将二维数组想象成数据库里的一个表记录集,然后按指定的列集进行排序,即order by col1,col2。
//a为二维数组,sortCols是要按哪几列排序,如0,2,以逗号分隔,升序排。
public void sortIntArrs(int[][] a,String sortCols)
{
}

以下是我回答的程序:

[code]
public class MyArray
{
/**
* @param args
*/
public static void main(String[] args)
{
int array[][] = new int[][]
{
{ 12, 34, 68, 32, 9, 12, 545 },
{ 34, 72, 82, 57, 56, 0, 213 },
{ 12, 34, 68, 32, 21, 945, 23 },
{ 91, 10, 3, 2354, 73, 34, 18 },
{ 12, 83, 189, 26, 27, 98, 33 },
{ 47, 23, 889, 24, 899, 23, 657 },
{ 12, 34, 68, 343, 878, 235, 768 },
{ 12, 34, 98, 4, 56, 78, 12, 546 },
{ 26, 78, 2365, 78, 34, 256, 873 } };// 要排序的数组
int co[] = new int[]
{ 0, 1, 2, 3, 4 };// 指定进行排序的列索引及排序顺序
MyArray arraylist = new MyArray();
arraylist.sortIntArrs(array, co);
}

public void sortIntArrs(int[][] a, int[] co)
{
for (int i = 0; i < a.length - 1; i++)
{
for (int j = i + 1; j < a.length; j++)
{
sort(a, co, i, j, 0);
}
}
for (int m = 0; m < a.length; m++)
{
for (int n = 0; n < a[m].length; n++)
{
System.out.print(a[m][n] + ",");
}
System.out.println();
}
}

public void sort(int[][] a, int[] co, int i, int j, int co_index)
{
if (co_index < co.length)
{
if (a[i][co[co_index]] > a[j][co[co_index]])
{
int aa[] = a[i];
a[i] = a[j];
a[j] = aa;
} else if (a[i][co[co_index]] == a[j][co[co_index]])
{
sort(a, co, i, j, co_index + 1);
}
} else
return;
}
}

[/code]

我的方法中没有将指定的列用字符串参数传递,我采用的是整型数组!
不过我的程序是不正确的,各位有正确的解决方法吗?请赐教!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值