考虑跟踪您的增量,这样您就可以知道它们有多少,并且以后可以选择做一些额外的操作。另外,利用 Java 在排序和包装方面已经提供的功能。
例如 :
import java.util.*;
class ProfitDelta implements Comparable<ProfitDelta> {
private int delta;
private int profit;
public ProfitDelta(int delta, int profit) { this.delta = delta; this.profit = profit; }
public int getDelta() { return delta; }
public int getProfit() { return profit; }
public int compareTo(ProfitDelta o) {
return o.delta - this.delta;
}
}
class Main {
static Integer[] stocks = { 1, 7, 6, 7, 10, 5, 6, 10 };
public static void main(String[] args) {
System.out.println("Best amount of profit is ");
for (ProfitDelta profit : getBestProfitAmount(stocks)) {
System.out.println("Profit : " + profit.getProfit() + ", delta=" + profit.getDelta());
}
}
public static ProfitDelta[] getBestProfitAmount(Integer[] stocks) {
ProfitDelta[] profits = new ProfitDelta[stocks.length];
List<Integer> sortedStocks = Arrays.asList(stocks);
List<ProfitDelta> sortedProfits = Arrays.asList(profits);
Collections.sort(sortedStocks);
int delta = 0;
int stockValue;
for (int i = 0; i < stocks.length; i++) {
stockValue = sortedStocks.get(i);
delta = stockValue - delta;
sortedProfits.set(i, new ProfitDelta(delta, stockValue));
delta = stockValue;
}
Collections.sort(sortedProfits);
return profits;
}
}
会给你输出:
Best amount of profit is
Profit : 5, delta=4
Profit : 10, delta=3
Profit : 1, delta=1
Profit : 6, delta=1
Profit : 7, delta=1
Profit : 6, delta=0
Profit : 7, delta=0
Profit : 10, delta=0
如果在您的原始数组中排序很重要,您可能希望包装sortedStocks
到另一个将保留原始索引值的类中,并将属性添加originalIndex
到ProfitDelta
. 或者更简单的事件,像这样:
import java.util.*;
class ProfitDelta implements Comparable<ProfitDelta> {
private int originalIndex = 0;
private int delta = 0;
private int profit;
public ProfitDelta(int index, int profit) { this.originalIndex = index; this.profit = profit; }
public int getOriginalIndex() { return originalIndex; }
public int getDelta() { return delta; }
public void setDelta(int delta) { this.delta = delta; }
public int getProfit() { return profit; }
public int compareTo(ProfitDelta o) { return o.delta - this.delta; }
}
class ProfitComparator implements Comparator<ProfitDelta> {
@Override public int compare(ProfitDelta o1, ProfitDelta o2) {
return o1.getProfit() - o2.getProfit();
}
}
class Main {
static int[] stocks = { 1, 7, 6, 7, 10, 5, 6, 10 };
public static void main(String[] args) {
System.out.println("Best amount of profit is ");
for (ProfitDelta profit : getBestProfitAmount(stocks)) {
System.out.println("Profit : " + profit.getProfit() +
", delta=" + profit.getDelta() +
", originalIndex=" + profit.getOriginalIndex());
}
}
public static ProfitDelta[] getBestProfitAmount(int[] stocks) {
ProfitDelta[] profits = new ProfitDelta[stocks.length];
int delta = 0;
List<ProfitDelta> sortedProfits = Arrays.asList(profits);
for (int i = 0; i < stocks.length; i++) {
sortedProfits.set(i, new ProfitDelta(i, stocks[i]));
}
Collections.sort(sortedProfits, new ProfitComparator());
for (ProfitDelta profit : profits) {
profit.setDelta(profit.getProfit() - delta);
delta = profit.getProfit();
}
Collections.sort(sortedProfits);
return profits;
}
}
哪个输出:
Best amount of profit is
Profit : 5, delta=4, originalIndex=5
Profit : 10, delta=3, originalIndex=4
Profit : 1, delta=1, originalIndex=0
Profit : 6, delta=1, originalIndex=2
Profit : 7, delta=1, originalIndex=1
Profit : 6, delta=0, originalIndex=6
Profit : 7, delta=0, originalIndex=3
Profit : 10, delta=0, originalIndex=7