public class Test_1 {
/**
* @param args
*/
public static void main(String[] args) {
int init[] = new int[] { 10, 30, 20, 60, 50 };
String order = getOrder(init);// 获取数组从大到小排列对应的顺序
System.out.println("order=="+order);
int iArray[] = { 10, 20, 30, 50, 60 };
List sort = getSort(iArray, order);//按照规则显示iArray的数据。
System.out.println("sort=="+sort);
/*
* 我现在的问题就是如果iArray 这个里面的数据变少了,被删除了1个到N个,
* 但是规则却无法改变了。还是1,3,2,5,4,。那我怎么做才能在sort里面也删除
* 掉iArray里面被删除的数据呢?
*
* 即:sort现在返回【10, 30, 20, 60, 50】
* 然后iArray数据为 【10, 20, 30, 50, 60 】
* 规则为:1,3,2,5,4,
*
* 希望的效果:
* 如果iArray数据为 【10, 20, 30】
* 规则还是:1,3,2,5,4,
* 希望返回【10, 30, 20】
*
*
* */
System.out.println("The End");
} // 获取数组的排序规则。
// 获取该数组值按从小到大顺序,对应的顺序。
/*
* 例如数组是: {19,12,13,16,15} 根据数组内容的大小顺序,排列出一个顺序。 即:5,1,2,4,3
*/
public static String getOrder(int array[]) {
StringBuffer order = new StringBuffer();
TreeMap<Integer, Integer> h = new TreeMap<Integer, Integer>();
for (int i = 0; i < array.length; i++)
h.put(array[i], i + 1); TreeMap<Integer, Integer> h_1 = new TreeMap<Integer, Integer>();
int i = 1;
for (Object o : h.keySet())
h_1.put(h.get(o), i++); for (Object o : h_1.keySet())
order.append(h_1.get(o)).append(",");
return order.toString();
}
//array 数据仓库
//order 数据仓库顺序
//return list 按照数据仓库顺序显示的数据 public static List getSort(int array[], String order) {
String s[] = order.split(",");
TreeMap<Integer, Integer> h = new TreeMap<Integer, Integer>();
for (int i = 0; i < array.length; i++)
h.put(array[i], i); ArrayList<Integer> a = new ArrayList<Integer>();
for (Object o : h.keySet())
a.add(h.get(o)); ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = 0; i < s.length; i++)
al.add(array[a.get(Integer.parseInt(s[i]) - 1)]); return al;
} /**
* @param args
*/
public static void main(String[] args) {
int init[] = new int[] { 10, 30, 20, 60, 50 };
String order = getOrder(init);// 获取数组从大到小排列对应的顺序
System.out.println("order=="+order);
int iArray[] = { 10, 20, 30, 50, 60 };
List sort = getSort(iArray, order);//按照规则显示iArray的数据。
System.out.println("sort=="+sort);
/*
* 我现在的问题就是如果iArray 这个里面的数据变少了,被删除了1个到N个,
* 但是规则却无法改变了。还是1,3,2,5,4,。那我怎么做才能在sort里面也删除
* 掉iArray里面被删除的数据呢?
*
* 即:sort现在返回【10, 30, 20, 60, 50】
* 然后iArray数据为 【10, 20, 30, 50, 60 】
* 规则为:1,3,2,5,4,
*
* 希望的效果:
* 如果iArray数据为 【10, 20, 30】
* 规则还是:1,3,2,5,4,
* 希望返回【10, 30, 20】
*
*
* */
System.out.println("The End");
}
}
注:
1、规则不能删除。(但是只要能实现需求其他规则也可以。但是就是要保证不能删除规则内容)
2、规则中不能包含数据仓库的内容
package com.test.sql;import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;public class Test_2 {
/** * @param args */
public static void main(String[] args) {
int init[] = new int[] { 10, 30, 20, 60, 50 };
Map<Integer,Integer> dics = new LinkedHashMap<Integer, Integer>();
String order = getOrder(init,dics);// 获取数组从大到小排列对应的顺序
System.out.println("order==" + order);
int iArray[] = { 10, 20, 30, 50, 60 };
List sort = getSort(iArray, order);// 按照规则显示iArray的数据。
System.out.println("sort==" + sort);
int iArray_1[] = { 10, 20, 30 };
List sort_1 = getSort(iArray_1, dics);// 按照规则显示iArray的数据。
System.out.println("sort==" + sort_1);
System.out.println("The End");
}
// 获取数组的排序规则。
// 获取该数组值按从小到大顺序,对应的顺序。
/* * 例如数组是: {19,12,13,16,15} // 根据数组内容的大小顺序,排列出一个顺序。 即:5,1,2,4,3 */ public static String getOrder(int array[],Map<Integer,Integer> dic) {
StringBuffer order = new StringBuffer(); TreeMap<Integer, Integer> h = new TreeMap<Integer, Integer>();
for (int i = 0; i < array.length; i++){
h.put(array[i], i + 1);
dic.put(array[i], 0);
}
TreeMap<Integer, Integer> h_1 = new TreeMap<Integer, Integer>();
int i = 1;
for (Object o : h.keySet())
h_1.put(h.get(o), i++);
for (Object o : h_1.keySet())
order.append(h_1.get(o)).append(",");
return order.toString();
} // array 数据仓库
// order 数据仓库顺序
// return list 按照数据仓库顺序显示的数据
public static List getSort(int array[], String order) {
String s[] = order.split(",");
TreeMap<Integer, Integer> h = new TreeMap<Integer, Integer>();
for (int i = 0; i < array.length; i++)
h.put(array[i], i);
ArrayList<Integer> a = new ArrayList<Integer>();
for (Object o : h.keySet())
a.add(h.get(o));
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = 0; i < s.length; i++)
al.add(array[a.get(Integer.parseInt(s[i]) - 1)]);
return al;
}
public static List getSort(int array[], Map<Integer,Integer> dics) {
for (Integer key : dics.keySet()) { //clear dics
dics.put(key, 0);
}
for(int i : array){
if (dics.get(i) != null)
dics.put(i, 1);
}
List<Integer> al = new ArrayList<Integer>();
for(Map.Entry<Integer, Integer> entry : dics.entrySet()){
if(entry.getValue() == 1){
al.add(entry.getKey());
}
}
return al;
}}
* 如果iArray数据为 【10, 20, 30】
* 规则还是:1,3,2,5,4,
* /希望返回【10, 30, 20】你的要求是不是根据规则将iArray的数据映射回去?那不管iArray怎么变,你把iArray的数据放到HashMap中去,根据规则找key,然后返回value不就可以了?
public static List getSort(int array[], String order) {
String s[] = order.split(",");
if(s.length < array.length){
//TODO:抛出异常
}else if(s.length > array.length)
{
int [] ss = new int[array.length];
for (int i = 0; i < array.length; i++){
ss[i] = Integer.parseInt(s[i]);
}
String s1 = getOrder(ss);/*根据传入的order获得适合传入数组的order*/
//System.out.println("new temp order == "+s1);
String[] s2 = s1.split(",");
s = s2;
} //相等是不用处理
TreeMap<Integer, Integer> h = new TreeMap<Integer, Integer>();
for (int i = 0; i < array.length; i++)
h.put(array[i], i); ArrayList<Integer> a = new ArrayList<Integer>();
for (Object o : h.keySet())
a.add(h.get(o)); ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = 0; i < s.length; i++)
al.add(array[a.get(Integer.parseInt(s[i]) - 1)]); return al;
}
只是在内部产生了一个临时的order,实际和原来order的顺序是一样的,只不过下标适合传入的数组。同时并没有修改原来的order。