添加到ArrayList <t> </t>后,Java Float变为0.0

时间:2010-01-25 23:56:13

标签: java collections arraylist floating-point

我有以下对象:

class Repeat{
    private long startIndex;
    private long endIndex;
    private int length;
    private float repetitions;
    private float period;
    private int errors;
    private float percentOverlap;

    public void setPercentOverlap(float percentOverlap) {
       this.percentOverlap = percentOverlap;
    }

    public float getPercentOverlap() {
       return percentOverlap;
    }
    .
    . other sets gets etc.
    .
}

当我设置percentOverlap并将重复添加到

ArrayList<Repeat> overlaps = new ArrayList<Repeat>(); 
overlaps.add(repeat);

然后当我将此集合转储到csv文件时。我得到0.0的一些值,但不是全部。即。 6.25变为0.0。我甚至在命令行上看到了这一点。

这是控制台输出:

  

- &GT;在我添加之前    - &gt;开始索引:570433结束索引:570465重叠:100.0
   - &gt;开始索引:570433结束索引:570465重叠:6.25
   - &gt;开始索引:570433结束索引:570465重叠:0.0
   - &gt;开始指数:570470结束指数:570510重叠:85.0
   - &gt;开始索引:570470结束索引:570510重叠:100.0

当我迭代这个集合时,就会出现这个问题。

  

开始指数:570433结束指数:570465重叠:0.0
  开始指数:570433结束指数:570465重叠:0.0
  开始指数:570433结束指数:570465重叠:0.0
  开始指数:570470结束指数:570510重叠:100.0
  开始指数:570470结束指数:570510重叠:100.0

我拿出文件写入,只是打印到控制台。

为什么会这样?

4 个答案:

答案 0 :(得分:3)

每个人都在告诉你正确的答案:你不止一次地将同一个对象添加到集合中。

你的外环做了一个新的重复repeat1。您的内部循环在此对象中设置值,并在每次迭代时将其添加到集合中。

即使您在每个内部迭代中在repeat1中设置了不同的值,它仍然是同一个对象。

这就是您获得所示结果的原因。您的收藏看起来像这样:

1:首先重复1 2:首先重复1 3:首先重复1 4:第二次重复1 5:第二次重复1

答案 1 :(得分:1)

从输出中,粗略的猜测是,当你应该添加一个新实例时,你会多次将相同的对象添加到ArrayList。显示的代码看起来没问题,所以错误在其他地方。

答案 2 :(得分:1)

以下条目的起始索引和结束索引看起来相同。

->Start Index: 570433 End Index: 570465 Overlap :100.0
->Start Index: 570433 End Index: 570465 Overlap :6.25
->Start Index: 570433 End Index: 570465 Overlap :0.0

->Start Index: 570470 End Index: 570510 Overlap :85.0
->Start Index: 570470 End Index: 570510 Overlap :100.0 

然后在输出中,对于相同的开始索引和结束索引重复重叠值(0.0,100.0)。这意味着相同的对象被重用于给定的开始索引和结束索引。因此,重复所有先前Repeat对象的值。使用新的开始索引和结束索引,将创建新的重复对象。

请检查创建Repeat对象实例的代码逻辑。

答案 3 :(得分:0)

for(Repeat repeat1 : collection1){

                // length of repeat from first collection input
                long length1 = repeat1.getEndIndex() - repeat1.getStartIndex();

                float percentOverlap;
                long overlapamount = 0;

                // For each repeat in collection2
                HERE:for(Repeat repeat2 : collection2){


                    // length of repeat from second collection input
                    long length2 = repeat2.getEndIndex() - repeat2.getStartIndex();

                    // if this condition fails, no reason to iterate any further.
                    // This is the feature that c# lacks. It saves quite a processing time. 
                    if (repeat1.getStartIndex() > repeat2.getEndIndex() || repeat1.getEndIndex() < repeat2.getStartIndex())
                    {
                         continue HERE;
                    }

                    if (repeat1.getStartIndex() <= repeat2.getStartIndex() && repeat2.getStartIndex() <= repeat1.getEndIndex())
                    {
                        // if repeat1 start index is less than or equal to repeat 2 start index
                        // and if repeat2 start index is less than or equal to repeat1 end index.
                        // 1,3 - 2,4
                        overlapamount = repeat1.getEndIndex() - repeat2.getStartIndex();
                    }
                    else if (repeat2.getStartIndex() <= repeat1.getStartIndex() && repeat1.getEndIndex() <= repeat2.getEndIndex())
                    {
                        // if repeat 2 start index is less than or equal to repeat 1 start index
                        // and if repeat 1 end index is less than or equal to repeat 2 end index
                        // 2,3 - 1,4
                        overlapamount = repeat1.getEndIndex() - repeat1.getStartIndex();
                    }
                    else if (repeat2.getStartIndex() <= repeat1.getStartIndex() && repeat2.getEndIndex() <= repeat1.getEndIndex())
                    {
                        // if repeat 2 start index is less than or equal to repeat1 start index
                        // and repeat 2 end index is less than or equal to repeat 1 end index
                        //
                        // 2,4 - 1,3
                        overlapamount = repeat2.getEndIndex() - repeat1.getStartIndex();
                    }
                    else if (repeat1.getStartIndex() <= repeat2.getStartIndex() && repeat2.getEndIndex() <= repeat1.getEndIndex())
                    {
                        // repeat 1 start index is less than or equal to repeat 2 start index
                        // and repeat 2 end index is less than or equal to repeat 1 end index
                        // 
                        // 1,4 - 2,3
                        overlapamount = repeat2.getEndIndex() - repeat2.getStartIndex();
                    } 

                    // Finds the overlapping percentage
                    percentOverlap = (float) (Math.max((float)((float)overlapamount /(float)length1), (float)((float)overlapamount /(float)length2)) * 100);
                    System.out.println(percentOverlap);
                    repeat1.setPercentOverlap(percentOverlap);

                    // Populates collection by percentage
                    overlaps.add(repeat1);
                    System.out.println("->"+repeat1.toString());
                }

            }

         for(Repeat x : overlaps){
                System.out.println(x.toString());
         }

    }

这是代码。这就是数据的方式。这与在两种方法之间比较DNA扫描算法和发现重叠等有关。