简体   繁体   English

Arraylist 给出奇怪的结果

[英]Arraylist giving weird result

so I am trying to solve a problem (scale problem) but the result is not what I expected and in particular, I cannot understand why it's giving me these numbers.所以我试图解决一个问题(规模问题),但结果不是我所期望的,特别是,我不明白为什么它给了我这些数字。 Inline if(rep.indexOf(i) == 1) I am trying to check if the ArrayList value is true to add the weight to the left but if its fault adds it to the right side.内联if(rep.indexOf(i) == 1)我正在尝试检查 ArrayList 值是否为真以将权重添加到左侧,但如果它的错误将其添加到右侧。 I tried using the value TRUE or FALSE for the argument but it gives an error so I just put the value 1 for testing to figure out the result numbers.我尝试使用值 TRUE 或 FALSE 作为参数,但它给出了错误,所以我只是将值 1 用于测试以找出结果数字。 The expected result should be 20 as I have 5 true values being added to the left side.预期结果应该是 20,因为我在左侧添加了 5 个真值。 R=0, L=1+2+3+4+10=20, F=|20-0|=20 R=0, L=1+2+3+4+10=20, F=|20-0|=20

import java.util.*;
import java.lang.Math;
class Untitled {
    public static void main(String[] args) {
        ArrayList<Double> weight = new ArrayList<Double>( );
        ArrayList<Boolean> rep = new ArrayList<Boolean>( );
        rep.add(true); rep.add(true); rep.add(true); rep.add(true);rep.add(true);
        weight.add(1.0); weight.add(2.0); weight.add(3.0); weight.add(4.0); weight.add(10.0);
        
        System.out.println(ScalesFitness(rep,weight));
        
    }
    public static double ScalesFitness(ArrayList<Boolean> rep, ArrayList<Double> weights) {
        System.out.print(rep);
        System.out.println();
        
        
        double left = 0, right = 0, scalesolution =0;
                        
                    for(int i=0; i<rep.size(); i++)
            {
                if(rep.indexOf(i) == 1)
                {
                    left = left + weights.indexOf(i);
                    System.out.println("value of right side"+rep.indexOf(i));
                    
                }
                else {
                    right = right + weights.indexOf(i);
                    System.out.println("value of right side"+rep.indexOf(i));
                }
            }
        //}
        scalesolution = Math.abs(left-right);
        return scalesolution;
    }
    
}

The results:结果:

[true, true, true, true, true]
value of right side-1
value of right side-1
value of right side-1
value of right side-1
value of right side-1
5.0

Thanks in advance提前致谢

rep.indexOf(i) 

This doesn't retrieve the element of the position marked by i : it will return the position of the element passed as argument, if exists.这不会检索由i标记的 position 的元素:它将返回作为参数传递的元素的 position(如果存在)。 If it doesn't exist, it will return -1.如果它不存在,它将返回-1。

From the docs : 从文档

public int indexOf(Object o)

Returns the index of the first occurrence of the specified element in this list, >or -1 if this list does not contain the element.返回此列表中指定元素第一次出现的索引,如果此列表不包含该元素,则 > 或 -1。 More formally, returns the lowest index i such that (o==null? get(i)==null: o.equals(get(i))), or -1 if there is no such index.更正式地说,返回满足 (o==null?get(i)==null: o.equals(get(i))) 的最低索引 i,如果没有这样的索引,则返回 -1。


As there's no element in the arraylist that holds any of the values offered by i , you will just receive -1 as result from that call.由于 arraylist 中没有包含i提供的任何值的元素,因此您将收到 -1 作为该调用的结果。

What you probably need is to call this other method :你可能需要的是调用这个其他方法

rep.get(i)

public E get(int index)

Returns the element at the specified position in this list.返回此列表中指定 position 处的元素。

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

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