有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (3) 个答案

  1. # 1 楼答案

    您应该使用以下对象:

    public class Item {
        private String name;
        private double price; // you shouldn't use doubles for money, but this is unrelated
    
        public Item(String name, double price) {
            this.name = name;
            this.price = price;
        }
    
        public String getName() {
            return this.name;
        }
    
        public double getPrice() {
            return this.price;
        }
    }
    

    然后您可能会有一个项目数组(或列表):

    private Item[] items = new Item[] {new Item("Gtaspy", 8.99), ...};
    

    您可以使用Arrays.sort()对该数组进行排序(如果使用列表而不是数组,则使用Collections.sort()

    阅读Java tutorial on Collections了解更多详细信息

  2. # 2 楼答案

    可能的方法与此math3库中实现的方法相同: org。阿帕奇。平民数学3。util。MathArrays#sortInPlace

    /**
     * Sort an array in place and perform the same reordering of entries on
     * other arrays.  This method works the same as the other
     * {@link #sortInPlace(double[], double[][]) sortInPlace} method, but
     * allows the order of the sort to be provided in the {@code dir}
     * parameter.
     *
     * @param x Array to be sorted and used as a pattern for permutation
     * of the other arrays.
     * @param dir Order direction.
     * @param yList Set of arrays whose permutations of entries will follow
     * those performed on {@code x}.
     * @throws DimensionMismatchException if any {@code y} is not the same
     * size as {@code x}.
     * @throws NullArgumentException if {@code x} or any {@code y} is null
     * @since 3.0
     */
    public static void sortInPlace(double[] x,
                                   final OrderDirection dir,
                                   double[] ... yList)
        throws NullArgumentException, DimensionMismatchException {
        if (x == null) {
            throw new NullArgumentException();
        }
    
        final int len = x.length;
        final List<Pair<Double, double[]>> list
            = new ArrayList<Pair<Double, double[]>>(len);
    
        final int yListLen = yList.length;
        for (int i = 0; i < len; i++) {
            final double[] yValues = new double[yListLen];
            for (int j = 0; j < yListLen; j++) {
                double[] y = yList[j];
                if (y == null) {
                    throw new NullArgumentException();
                }
                if (y.length != len) {
                    throw new DimensionMismatchException(y.length, len);
                }
                yValues[j] = y[i];
            }
            list.add(new Pair<Double, double[]>(x[i], yValues));
        }
    
        final Comparator<Pair<Double, double[]>> comp
            = new Comparator<Pair<Double, double[]>>() {
            public int compare(Pair<Double, double[]> o1,
                               Pair<Double, double[]> o2) {
                int val;
                switch (dir) {
                case INCREASING:
                    val = o1.getKey().compareTo(o2.getKey());
                break;
                case DECREASING:
                    val = o2.getKey().compareTo(o1.getKey());
                break;
                default:
                    // Should never happen.
                    throw new MathInternalError();
                }
                return val;
            }
        };
    
        Collections.sort(list, comp);
    
        for (int i = 0; i < len; i++) {
            final Pair<Double, double[]> e = list.get(i);
            x[i] = e.getKey();
            final double[] yValues = e.getValue();
            for (int j = 0; j < yListLen; j++) {
                yList[j][i] = yValues[j];
            }
        }
    }
    
  3. # 3 楼答案

    或者将商品名称和价格封装在一个类中,然后使用该类的单个实例数组,并使用Comparator对它们进行排序? 例如

    public class Item {
    private String name;
    private double price;
    ...
    //getters and setters for name and price
    }
    
    ...
    
    Item []items = { new Item("Gatspy", 8.99), .... };
    
    ...
    
    class ItemComparator implements Comparator {
     public int compare( Object o1, Object o2 ) {
      Item i1 = (Item)o1;
      Item i2 = (Item)o2;
      return i1.getName().compareTo(i2.getName());
     }
    }
    
    ...
    
    Arrays.sort( items, new ItemComparator() );