`
乌拉蕾
  • 浏览: 72123 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

一道java笔试题的解法

    博客分类:
  • java
阅读更多

    昨天晚上看到一道java笔试题,然后觉得挺有意思的。

题目如下:

写一个java程序,实现对一个二维数组按指定的列集进行排序。要求实现类似sql中order by的功能,移动时,整行移动,不能打乱整行顺序。 可将二维数组想象成数据库里的一个表记录集,然后按指定的列集进行排序,即order by col1,col2。

 

以下是我写的代码:

 

public class SortedByCol {

	private 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},
			{ 26, 78, 2365, 78, 34, 256, 873 } };// 要排序的数组
	
	private HashMap<Integer, List> nums;
	
	private ArrayList<Integer> colist;
	
	private ArrayList<Integer> sortedcolist;
	
	private int col;
	
	public SortedByCol(){
		
	}
	
	private void getCol(int col) {
		// TODO Auto-generated method stub
		colist = new ArrayList<Integer>();
		for (Integer i : nums.keySet()) {
			colist.add(nums.get(i).get(col));
		}
	}
	
	@SuppressWarnings("unchecked")
	private void colSort(){
		sortedcolist = new ArrayList<Integer>();
		sortedcolist = (ArrayList<Integer>) colist.clone();
		
		System.out.println(sortedcolist);
		Object[] sortedArray = sortedcolist.toArray();  //pay attention Object
		Arrays.sort(sortedArray);
		boolean flag = false;
		for (Object value : sortedArray) {
			if (colist.indexOf(value) != colist.lastIndexOf(value)) {  //这里是判断那行重复的
				if (!flag) {
					flag = true;
					for (Integer i : nums.keySet()) {
						List li = nums.get(i);
						if (li.get(col) == value) {
							System.out.println(li);
						}
					}
				}
			} else {
				int i = colist.indexOf(value);
				List li = nums.get(i);
				System.out.println(li);
			}

		}
	}

	
	public void sort(int col){
		this.col = col;
              //  把二维数组的每一行放入一个hashMap中,key:对应的行,value:对应的该行的数据(用
              //  一个内部类list来保存
		putArrayInHash();
              //得到要排序的那一行的数据
		getCol(col);
              //进行排序
		colSort();
	}
	
	private void putArrayInHash(){
		nums = new HashMap<Integer, List>();
		for (int i = 0; i < array.length; i++) {
			List list = new List(i);
			for (int j = 0; j < array[0].length; j++) {
				list.put(array[i][j]);
			}
			nums.put(i, list);
		}
	}
	
	private void test() {
		System.out.println(array[1][2]);
	}
	
	public static void main(String[] args) {
		SortedByCol s = new SortedByCol();
		s.sort(6);   //这里的6是表示选择的列
	}

	class List{
		private ArrayList<Integer> li = new ArrayList<Integer>();
		
		private int id;
		
		public List(int id){
			this.id = id;
		}

		public Integer get(int col) {
			return li.get(col);
		}

		public void put(int j) {
			li.add(j);
		}
		
		public String toString(){
			StringBuilder sb = new StringBuilder();
			for (Integer i : li) {
				sb.append(' '+String.valueOf(i));  
			}
			return sb.toString();
		}
	}
	

}
 

 

        :)

 

2
0
分享到:
评论
2 楼 xuyao 2008-10-29  
很简单啊,直接把一行放到对象里,用Comparator
1 楼 liu_swei 2008-10-29  
居然定义一个类叫List,强!

相关推荐

Global site tag (gtag.js) - Google Analytics