本文介绍了如何相对于彼此对两个数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python创建原型,为此使用了zip函数,但我不确定如何在Java中执行此操作.基本上,我有两个列表(一个是 names ,一个是 data ),希望它们相对于彼此排序.我的程序仅处理列表(在这种情况下为 data ),但是我使用 names 作为对正在处理的 data 的引用我想尝试以不同的顺序处理我的数据.这是结构的一个示例(实际上我的数据没有提供给我存储,但是我可以对其进行基本排序或反向排序,没什么花哨的.)

I prototype in python and I'm used the zip function for this, I'm not sure how to do this in Java. Basically I have two lists (one is names and one is data) and want them sorted in relation to each other. My program only processes a list (data, in this case) but I use the names as a reference to what data I'm processing and I want to try to experiment with processing my data in a different order. Here's an example of the structure (in reality my data is not given to me stored but I would do either a basic sort or a reverse sort on it, nothing fancy).

String[] names = new String[]{"Monkey1", "Dog2", "Horse3", "Cow4", "Spider5"};
int[] data = new int[]{1, 2, 3, 4, 5};

所以倒数就是

name = Spider5, Cow4, Horse3, Dog2, Monkey1
data = 5, 4, 3, 2, 1

我发现了这个问题:是否存在可接受的Java等效项到Python的zip(),但我宁愿(如果可能并且出于胆怯的话)使用我已经拥有的库(Java Commons,apache commons等)来做到这一点.如果没有其他方法,那么我会给 functional java 打个针.有什么建议吗?

I found this question: Is there an accepted Java equivalent to Python's zip(), but I would rather (if possible and for the faint of heart) do this using libraries I already have (Java commons, apache commons, etc). If there's no other way then I'll give functional java a shot. Any suggestions?

推荐答案

以下是完整的代码:

StringIntTuple.java:

StringIntTuple.java:

public class StringIntTuple{
    public final int intValue;
    public final String stringValue;
    public StringIntTuple(int intValue, String stringValue){
        this.intValue = intValue;
        this.stringValue = stringValue;
    }
    public String toString(){
        return "(" + this.intValue + ", " + this.stringValue + ")";
    }

}

StringIntTupleStringComparator.java:

StringIntTupleStringComparator.java:

import java.util.Comparator;


public class StringIntTupleStringComparator implements
        Comparator<StringIntTuple> {

    @Override
    public int compare(StringIntTuple a, StringIntTuple b) {
        // TODO Auto-generated method stub
        return a.stringValue.compareTo(b.stringValue);
    }

}

StringIntTupleIntComparator.java:

StringIntTupleIntComparator.java:

import java.util.Comparator;


public class StringIntTupleIntComparator implements Comparator<StringIntTuple> {

    @Override
    public int compare(StringIntTuple a,
            StringIntTuple b) {
        return ((Integer)a.intValue).compareTo((Integer)b.intValue);
    }

}

Driver.java:

Driver.java:

import java.util.ArrayList;
import java.util.Collections;


public class Driver {

    /**
     * @param args
     */
    public static String[] names = new String[] {"Monkey1", "Dog2", "Horse3", "Cow4", "Spider5"};
    public static int[] data = new int[] {1,2,3,4,5};
    public static void main(String[] args) {
        ArrayList<StringIntTuple> list = new ArrayList<StringIntTuple>();
        for(int i =0; i<names.length; i++){
            list.add(new StringIntTuple(data[i],names[i]));
        }
        Collections.sort(list, new StringIntTupleIntComparator());
        System.out.println(list.toString());
        Collections.sort(list, new StringIntTupleStringComparator());
        System.out.println(list.toString());
    }


}

输出(首先按int字段,然后按String字段排序):

Output (sorted first by int field, then by String field):

[(1,Monkey1),(2,Dog2),(3,Horse3),(4,Cow4),(5,Spider5)]

[(1, Monkey1), (2, Dog2), (3, Horse3), (4, Cow4), (5, Spider5)]

[(4,Cow4),(2,Dog2),(3,Horse3),(1,Monkey1),(5,Spider5)]

[(4, Cow4), (2, Dog2), (3, Horse3), (1, Monkey1), (5, Spider5)]

编辑1(更多信息):

如果您想对任何元组都使用此功能,即不将字段类型限制为int,String,则可以对泛型执行相同的操作,即:

If you want to make this work for any Tuple, i.e. which doesn't constrain the field types to int, String, you can simply do the same operation with generics, i.e.:

public class Tuple<A,B>{
    public Tuple(A aValue, B bValue){
        this.aValue = aValue;
        this.bValue = bValue;
    }
    public final A aValue;
    public final B bValue;

}

然后,相应地调整比较器,便有了通用的解决方案.编辑2(午餐后):在这里.

Then, just tweak the Comparators accordingly, and you have a generic solution.EDIT 2(After lunch): Here it is.

public class TupleAComparator<A extends Comparable<A>,B extends Comparable<B>> implements Comparator<Tuple<A,B>> {

    @Override
    public int compare(Tuple<A, B> t1, Tuple<A, B> t2) {
        return t1.aValue.compareTo(t2.aValue);
    }

}

代码补充作为对注释1的回答(增强注释2)TupleArrayList.java:

EDIT 3: Code supplement as answer to Comment #1 (augmenting comment #2)TupleArrayList.java:

import java.util.ArrayList;
import java.util.List;


public class TupleArrayList<A,B> extends ArrayList<Tuple<A,B>> {

    /**
     * An ArrayList for tuples that can generate a List of tuples' elements from a specific position within each tuple
     */
    private static final long serialVersionUID = -6931669375802967253L;

    public List<A> GetAValues(){
        ArrayList<A> aArr = new ArrayList<A>(this.size());
        for(Tuple<A,B> tuple : this){
            aArr.add(tuple.aValue);
        }
        return aArr;
    }

    public List<B> GetBValues(){
        ArrayList<B> bArr = new ArrayList<B>(this.size());
        for(Tuple<A,B> tuple : this){
            bArr.add(tuple.bValue);
        }
        return bArr;
    }

}

这篇关于如何相对于彼此对两个数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 16:50
查看更多