2012-02-22 73 views

回答

8

迄今为止做这将是重新思考设计,并把所有三个相关数据成class为此目的设计的情况下,最好的办法,那么就进行排序的一个ArrayList

任何其他的方法可以是分别设置了一些Map s到保持String S之间的关系,然后手动排序的第一个(一个困难的混乱)或写入其中移动自己的排序算法后走动名单第二个列表与第一个列表(可能更容易,但更糟糕的混乱)

+0

+1很好的答案。无论如何,统一的数据结构可以稍后分成三个不同的ArrayList。 – 2012-02-22 14:46:38

+0

感谢您的帮助Ernest。但其实我没有任何静态值。数组列表可能超过三个。我的意思是说所有的数据都是dyanamic.hence我不能创建任何常量对象。那么如何以上述格式来做到这一点。 – Rahul 2012-02-23 07:16:39

+0

@Rahul - 那么唯一的方法就是编写你自己的'sort()'例程,它对一个特殊的'ArrayList'执行所有的比较操作,但是对所有'ArrayList'进行交换。你可以从'Collections.sort()'的JDK源代码开始,然后从那里开始。 – 2012-02-24 14:42:43

0

如果您自己写了排序代码,只展开修改第一个数组的代码部分,以修改其他两个数组同样的时尚。

尽管最佳方法是重构代码,使其中包含所有三个数组的信息的对象列出一个列表。

正如欧内斯特说:)

0

排序时第一个数组列表,而不是只在交换第一个数组列表中的元素做它也为其他的也一样,使用相同的索引。

arraylist1[x]=arraylist1[y]; 
arraylist2[x]=arraylist2[y]; 
arraylist3[x]=arraylist3[y]; 

当然有更多的步骤交换(如分配给辅助变量),但这不是我想在这里显示。

0

这是一个普遍的需求。 一个选项(和你使用Java的排序,这可能比你写一个更好):

/** 
* Sorts by the first list 
*/ 
static void sort(List... lists) { 
    assert lists.length > 0; 

    Object[][] objects = new Object[lists[0].size()][lists.length]; 

    for (int i = 0; i < lists.length; i++) { 
     int j = 0; 
     for (Object object : lists[i]) { 
      objects[j++][i] = object; 
     } 
    } 

    Arrays.sort(objects, new Comparator<Object[]>() { 
     public int compare(Object[] o1, Object[] o2) { 
      return ((Comparable)o1[0]).compareTo(o2[0]); 
     } 
    }); 

    for (int i = 0; i < lists.length; i++) { 
     lists[i].clear(); 
     for (Object[] tuple : objects) { 
      lists[i].add(tuple[i]); 
     } 
    } 
} 

使用它像

List<String> a = new ArrayList<String>(Arrays.asList(new String[]{"dog", "cat", "cat"})); 
List<Integer> b = new LinkedList<Integer>(Arrays.asList(new Integer[]{1, 2, 3})); 
List<Object> c = new Vector<Object>(Arrays.asList(new Object[]{"object", 0.5, new Object()})); 

sort(a, b, c); 

System.out.println(a); 
System.out.println(b); 
System.out.println(c); 

一个缺点使用,这是它取决于.clear(),它不是针对某些列表实现的,但在这种情况下可以适应它。

1
private void sortingMechanism() { 
for(int i=0;i<list1.size();i++){ 
    for(int j=i+1;j<list1.size();j++){ 
     if(list1.get(i)>list1.get(j)){ 
      Collections.swap(list1, i, j); 
      Collections.swap(list2, i, j); 
      Collections.swap(list3, i, j); 

     } 
    } 
} 
} 
0

您还可以使用hashmap关联要排序的数组的值和另一个的值。排序后,可以使用第一个数组的值作为关键字轻松找到第二个数组的值。

0

这样做的一种方法是首先捕获排序顺序,然后将其应用于每个单独的数据列表。下面是一个例子,它对两个数组进行排序,而不创建专门设计的对象来保存每个数组的数据或实现自己的排序算法。除排序要求外,其时间复杂度为O(N),并且需要两个长度为N的附加整数数组。N是每个数据数组中的元素数。

public static void main(String[] args) { 
    // Original data in multiple arrays 
    String[] letters = new String[] {"C","D","B","E","A"}; 
    int[] numbers = new int[] {3,4,2,5,1}; 

    // Array to hold the destination locations. 
    // Each element refers to the index of the element in the letters array 
    //  that goes into that location. 
    // e.g. 
    // The case of [C, D, B, E, A], this array would contain [4, 2, 0, 1, 3]. 
    // The 0-th element, 4, means that the 4th element in the source array (A) 
    // should end up in index 0. 
    Integer[] srcIndexes = new Integer[letters.length]; 

    // Assign indexes 
    for (int i = 0; i < letters.length; ++i) 
    srcIndexes[i] = i; 

    // Sort the destination index array according to the letters array. 
    Arrays.sort(srcIndexes, (Integer a, Integer b) -> letters[a].compareTo(letters[b])); 

    // Array to hold the source locations. 
    // Each element refers to the index where the element in the letters array should go to. 
    // e.g. 
    // The case of [C, D, B, E, A], this array would contain [2, 3, 1, 4, 0]. 
    // The 0-th element, 2, means that C should end up in index 2 of the resultant 
    // array (i.e. the destination index). 
    int[] dstIndexes = new int[letters.length]; 
    for (int i = 0; i < letters.length; ++i) { 
    dstIndexes[srcIndexes[i]] = i; 
    } 

    // Iterate through the indexes to move the data in the data array into their resultant locations. 
    for (int srcIndex = 0; srcIndex < letters.length;) { 
    int dstIndex = dstIndexes[srcIndex]; 

    // Index is already in place. 
    if (srcIndex==dstIndex) { 
     ++srcIndex; 
     continue; 
    } 

    // Swap elements in the source and destination indexes. 
    swap(dstIndexes, srcIndex, dstIndex); // Make sure to swap the indexes too. 
    swap(letters, srcIndex, dstIndex); // Swap elements in the letters array. 
    swap(numbers, srcIndex, dstIndex); // Swap elements in the numbers array. 
    } 

    System.out.println("Indexes : "+Arrays.toString(dstIndexes)); 
    System.out.println("Letters : "+Arrays.toString(letters)); 
    System.out.println("Numbers : "+Arrays.toString(numbers)); 
} 

private static <T> void swap(T[] objArray, int i, int j) { 
    T tmp = objArray[i]; 
    objArray[i] = objArray[j]; 
    objArray[j] = tmp; 
} 

private static void swap(int[] intArray, int i, int j) { 
    int tmp = intArray[i]; 
    intArray[i] = intArray[j]; 
    intArray[j] = tmp; 
} 
相关问题