简体   繁体   English

尝试基于字符串arraylist的索引加总double array list的总成本

[英]Trying to add up total cost of double array list based on index of string arraylist

So I have an array list of recurring strings deploy , and an array list of those strings with no duplicates singDeploy . 因此,我有一个循环字符串deploy的数组列表,以及那些没有重复的singDeploy字符串数组列表。 I also have an array list of doubles rate that have the same index as deploy . 我也有一个doubles rate数组列表,该列表具有与deploy相同的索引。 I'm trying to add up all the rates for each deploy string and store them in totCost that match up to singDeploy index however my for loop is giving me an java.lang.IndexOutOfBoundsException . 我试图将每个部署字符串的所有费率totCost ,并将它们存储在与singDeploy索引匹配的totCost ,但是我的for循环给了我java.lang.IndexOutOfBoundsException Not too sure why and still don't fully know if my loop gets that job done. 不太清楚为什么,仍然不完全知道我的循环是否完成了该工作。

 List<String> deploy = new ArrayList<>();
 List<String> singDeploy = new ArrayList<>();
 List<Double> totCost = new ArrayList<>();
 List<Double> rate = new ArrayList<>();
  singDeploy.addAll(Arrays.asList("Green Cow", "Green Elephant", "Green Rhinocerous", "Purple Cow", "Yellow Cat", "Red Sloth"));

  deploy.addAll(Arrays.asList("Green Cow", "Green Cow", "Green Elephant", "Green Cow", "Green Cow", "Green Rhinocerous", "Purple Cow", "Yellow Cat", "Red Sloth", "Green Cow", "Purple Cow", "Green Cow", "Red Sloth", "Yellow Cat", "Green Rhinocerous", "Purple Cow"));

  rate.addAll(Arrays.asList(0.553683754016, 1.02150872447, 0.134625067987, 1.98664453833, 0.861269450714, 0.36946659851, 0.909230205525, 0.00248039618935, 0.160619970732, 0.545320579333, 0.0709241158945, 3.96782782573E-6, 0.849530002338, 1.29965105158, 6.72974424027E-6, 4.51463564725E-6));


 for(int i=0;i<singDeploy.size();i++) {
    for(int j=0;i<deploy.size();j++) {
    if(singDeploy.get(i)==deploy.get(j)) {
        double temp=totCost.get(i);
        System.out.println(temp);
        totCost.add(temp+=rate.get(j));
    }
     else {
        j++;
        }
   }

Multiple things, 多种事物,

for(int j=0;j <deploy.size();j++) { // Use j , for(int j=0;j <deploy.size();j++) { // Use j

totCost.get(j); // Use j again,

singDeploy.get(i).equals(deploy.get(j)) // Use equals

Or try this, 或者尝试一下

for(int i=0;i < singDeploy.size() ;i++) {
             Double curCost = 0.0;
             String currDeploy = singDeploy.get(i);
             for(int j=0;j<deploy.size();j++) {
                 if(deploy.get(j).equals(currDeploy))
                     curCost += rate.get(j);
             }
             totCost.add(curCost);
          }

In this line: double temp=totCost.get(i); 在这一行: double temp=totCost.get(i); You are trying to access an element in totCost which does not have an element inside. 您正在尝试访问intCost中没有元素的元素。 That could be the reason for IndexOutOfBound. 这可能是IndexOutOfBound的原因。

At line for(int j=0;i<deploy.size();j++) { , you should put j instead of i for(int j=0;i<deploy.size();j++) { ,您应该放置j而不是i

for(int j=0;j<deploy.size();j++) {

It is going for infinite loop in your current code. 这将导致当前代码中的无限循环。 Also, as Bhargav mentioned, you are trying to access the element ie 另外,正如Bhargav所述,您正在尝试访问元素,即

double temp=totCost.get(i);

which doesn't have any element inside. 里面没有任何元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM