我有一个Android Studio项目,使用swarm粒子算法优化移动资源。我一直在我的健身功能中出现这个错误,我的主要活动从未运行或因此而向我显示结果。这是我的课程,我不断遇到问题......
public class CustomService implements Goodness {
public static int numOfServices = MainActivity.servicenames.size();
public static final int NUM_DIMENSIONS = 1 + numOfServices;
public static HashMap<String, Integer> serviceMap = new HashMap<String, Integer>(); //hashmap to store values
ArrayList<String> serviceNames = MainActivity.servicenames;
ArrayList<Double> costData = MainActivity.costDATA;
ArrayList<Double> costWlan = MainActivity.costWLAN;
ArrayList<Double> costUtilities = MainActivity.costUTILITY;
double batteryCost;
public void setBatteryCost(double batteryCost) {
this.batteryCost = batteryCost;
}
public CustomService(double batteryCost, ArrayList<Double> costData, ArrayList<Double> costWlan,
ArrayList<Double> costUtilities) {
if (costUtilities == null || costUtilities.size() < 1 || costData.size() < 1 || costWlan.size() < 1) {
throw new RuntimeException("Please add atleast 1 cost to Data, WLAN and Utility");
}
this.batteryCost = batteryCost; //make sure you add battery field to UI, user enters battery level
this.costData = costData;
this.costWlan = costWlan;
this.costUtilities = costUtilities;
}
public double getGoodness(boolean[] bits) {
double utility = 0.0;
double rcost = 0.0;
ArrayList<Double> resourceCost = new ArrayList<Double>();
Collections.sort(costUtilities);
double maxValue = Collections.max(costUtilities);
int NumOfDimensions = serviceMap.size();
serviceMap.put("DATA", 0);
serviceMap.put("WLAN", 1);
int data = serviceMap.get("DATA");
int wlan = serviceMap.get("WLAN");
for (int i = 2; i <= NumOfDimensions; i++) { //hashmap that stores the service names with a bit value i
for (int k = 0; k < numOfServices; k++) {
serviceMap.put(serviceNames.get(k), i); //if i = 2, k = 1, put(2, Facebook), put(3, Twitter)
}
if (bits[data] && bits[wlan]) {
return -500;
}
if (!bits[data] || bits[wlan]) {
return -1000; //particle goodness always returns this??
}
if (bits[data]) {
resourceCost = costData;
} else if (bits[wlan]) {
resourceCost = costWlan;
}
for (int n = 2; n <= numOfServices; n++) {
if (bits[serviceMap.get(n)]) {
utility += costUtilities.get(n - 1);
rcost += resourceCost.get(n - 1);
}
}
if (rcost < batteryCost) {
return utility;
}
}
return utility * 0.50;
}
}
基本上,我实现了一个hashmap,它接受来自main活动的用户输入,并为这些输入分配一个int值,该值在bits []数组中用作一个位。这些是我的错误......
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at test_classes.CustomService.getGoodness(CustomService.java:60)
at bpso.BinaryPso.optimize(BinaryPso.java:45)
at android_tests.CustomUseCase.test(CustomUseCase.java:56)
at ai69.psoui.ParticleActivity.runTest(ParticleActivity.java:108)
at ai69.psoui.ParticleActivity$runTests.doInBackground(ParticleActivity.java:59)
at ai69.psoui.ParticleActivity$runTests.doInBackground(ParticleActivity.java:56)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
答案 0 :(得分:0)
在我看来,您希望使用bits
数组作为输入/输出参数。但是,您无法在创建数组后更改其大小。所以你有几个选择:
1)确保在传递数组时,数组与serviceNames
的大小相同。
2)使用可以调整大小的对象(如BitSet
或List<Boolean>
)而不是数组。
3)在getGoodness
内创建数组,并返回一个复合值,该值包含double
和创建的bits
数组作为getGoodness
的返回值。