我对编程场景比较陌生,我希望你帮我分类这些数组。我们的想法是在textArea上显示一个菜单项,并按名称对项目进行排序。 parralel数组包含食品,另一个包含价格。
String[] items = {"Gatspy", "Coffee", "Chicken", "Mango Juice"};
double[] prices = {8.99, 23.50, 29.90, 7.50};
答案 0 :(得分:4)
或者如何将项目名称和价格封装在一个类中,然后拥有该类的一个实例数组并使用Comparator
对它们进行排序?
E.g。
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() );
答案 1 :(得分:2)
首先不要使用数组,使用Map
。在您的情况下,使用TreeMap
,它按键排序。
Map<String, Double> map = new TreeMap<String, Double>();
map.put("Gatspy", 8.99);
// put the other items
现在迭代条目:
for(Map.Entry<String, Double> entry : map.entrySet()){
System.out.println("<option value=\""
+ entry.getValue()
+ "\">"
+ entry.getKey()
+ "</option>");
}
参考: Java Tutorial&gt; Collections Trail&gt; The Map Interface
答案 2 :(得分:0)
你应该使用对象:
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), ...};
如果您使用List而不是数组,则可以使用Arrays.sort()(或Collections.sort())对此数组进行排序。
阅读Java tutorial on Collections了解更多详情。
答案 3 :(得分:0)
可能的方法可能与此math3库中实现的方法相同: org.apache.commons.math3.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];
}
}
}