简体   繁体   English

在Java中用零填充列表

[英]Fill a List with zeros in Java

I'm working in Java and I have a list of objects of type TimestampAndValue: 我正在用Java工作,并且有一个TimestampAndValue类型的对象列表:

public class TimestampAndValue{
    private double value;
    private long timestamp;

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }
}

My list is similar to this: 我的清单与此类似:

  • Element 1: timestamp = 0, value = 5 元素1:时间戳= 0,值= 5
  • Element 2: timestamp = 4, value = 6 元素2:时间戳= 4,值= 6
  • Element 3: timestamp = 6, value = 10 元素3:时间戳= 6,值= 10
  • Element 4: timestamp = 12, value = 1 元素4:时间戳= 12,值= 1

And I want to have this list in output: 我想在输出中包含此列表:

  • Element 1: timestamp = 0, value = 5 元素1:时间戳= 0,值= 5
  • Element 2: timestamp = 1, value = 0 元素2:时间戳= 1,值= 0
  • Element 3: timestamp = 3, value = 0 元素3:时间戳= 3,值= 0
  • Element 4: timestamp = 4, value = 6 元素4:时间戳= 4,值= 6
  • Element 5: timestamp = 5, value = 0 元素5:时间戳= 5,值= 0
  • Element 6: timestamp = 6, value = 10 元素6:时间戳= 6,值= 10
  • Element 7: timestamp = 7, value = 0 元素7:时间戳= 7,值= 0
  • Element 8: timestamp = 11, value = 0 元素8:时间戳= 11,值= 0
  • Element 9: timestamp = 12, value = 1 元素9:时间戳= 12,值= 1

I'll try to explain what I need in short. 总之,我将尽力解释我的需求。 When two timestamps aren't contiguous integers, I need to place the minimum number of zeros between them. 当两个时间戳不是连续的整数时,我需要在它们之间放置最小数目的零。 For example in the case between the timestamp 4 and 6 in the above list I need to place only one zero, but in the case in which two timestamps differ by two or more I need to place a zero right after the first timestamp and a zero immediately before the second timestamp. 例如,在上面列表中的时间戳4和6之间,我只需要放置一个零,但是在两个时间戳相差两个或多个的情况下,我需要在第一个时间戳之后放置一个零,并在其中放置一个零。在第二个时间戳记之前。 You can see this in the case between timestamp 6 and 10. I need also that the placed zeros have the correct timestamp set. 您可以在时间戳6到10之间看到这种情况。我还需要放置的零具有正确的时间戳设置。

For now I can't figure out how to solve it. 目前,我不知道如何解决它。 Thank you for your support! 谢谢您的支持!

This is the solution which worked for me using your suggestions: 这是使用您的建议为我工作的解决方案:

public static List<TimestampAndValue> insertMinimumNumberOfZerosBetweenValues(List<TimestampAndValue> list){
    if(list == null || list.isEmpty() || list.size() == 1)
        return list;

    int i;
    int j;
    long tempTimestamp1;
    long tempTimestamp2;
    long timestampDifference;

    List<TimestampAndValue> outList = new ArrayList<TimestampAndValue>();

    outList.add(list.get(0));
    for(i=0; i<list.size()-1; i++){
        j=i+1;

        tempTimestamp1 = list.get(i).getTimestamp();
        tempTimestamp2 = list.get(j).getTimestamp();
        timestampDifference = tempTimestamp2 - tempTimestamp1;

        if(timestampDifference == 2){
            TimestampAndValue tav = new TimestampAndValue();
            tav.setTimestamp(tempTimestamp1 + 1);
            tav.setValue(0);

            outList.add(tav);
        }
        else if(timestampDifference > 2){
            TimestampAndValue tav = new TimestampAndValue();
            tav.setTimestamp(tempTimestamp1 + 1);
            tav.setValue(0);

            outList.add(tav);

            TimestampAndValue tav2 = new TimestampAndValue();
            tav2.setTimestamp(tempTimestamp2 - 1);
            tav2.setValue(0);

            outList.add(tav2);
        }                

        outList.add(list.get(j));
    }

    return outList;
}

Maybe I'm not solving your problem correctly, but have you tried to implement a mechanism in wich returns a default value of 0 if the timestamp doesn't exist? 也许我无法正确解决您的问题,但是如果时间戳不存在,您是否尝试实现一种返回默认值0的机制? It will be much more efficient and simple 它将更加高效和简单

Is this homework? 这是作业吗? If so, please tag as such. 如果是这样,请加上标签。

Perhaps I am misunderstanding the question, but I would think a simple loop should work. 也许我对这个问题有误解,但我认为应该有一个简单的循环。 Sort your list by timestamp, then loop over all the values. 按时间戳对列表进行排序,然后遍历所有值。 As soon as you find a non-contiguous timestamp, insert a 0 entry. 一旦找到不连续的时间戳,请插入0条目。

You have to process pairs of timestamps from the input list, accumulating an output list: 您必须处理输入列表中的时间戳对,从而累积输出列表:

outputList = new list of timestamps;

for (int i = 0; i < numerOfTimestamps-1; i++) {
    timestamp1 = inputList.get(i);
    timestamp2 = inputList.get(i+1);

For each pair compare the distance between them: 对于每一对,比较它们之间的距离:

  • if they are contiguos, add timestamp1 to output list 如果它们是连续的,则将timestamp1添加到输出列表
  • if the difference is less than two, add timestamp1 and a new timestamp with 0 to output list 如果差异小于2,则将timestamp1和一个新的带有0时间戳添加到输出列表中
  • if the difference is equal to or greater than two, add timestamp1 and two new timestamps with 0 to output list 如果差异等于或大于2,则将timestamp1和两个新的0加到输出列表中

Then 然后

} // close loop

and add the last timestamp to the output list. 并将最后一个时间戳添加到输出列表。 (It's never added by the loop.) (它永远不会被循环添加。)

Note that you need to treat the empty input list separately. 请注意,您需要单独处理空的输入列表。

First a question Why would you need those integers in between? 首先是一个问题, 为什么您需要它们之间的整数?

Next a suggestion (not tested): 接下来的建议(未经测试):

List<TimestampAndValue> newList = new ArrayList<TimestampAndValue>();
TimestampAndValue lastAdded = null;    

for( int i = 0; i < oldList.length; i++ ) {   
  if( i > 0 && !isContiguous(lastAdded, oldList[i])) {
    newList.add(new TimestampAndValue(oldList[i].timestamp - 1, 0.0 ) );
  }

  newList.add( oldList[i] );
  lastAdded = oldList[i];

  if( i < (oldList.length - 1) && !isContiguous(oldList[i], oldList[i+1]) {
    lastAdded = new TimestampAndValue(oldList[i].timestamp + 1, 0.0 );
    newList.add( lastAdded );
  }
}

Basically you iterate over the list and insert the elements into a new list. 基本上,您遍历列表并将元素插入新列表中。 If the last value in the new list is not contiguous, add a 0 entry first. 如果新列表中的最后一个值不连续,请首先添加一个0条目。 If the next entry won't be contiguous, add a 0 entry afterwards. 如果下一个条目不是连续的,则在其后添加一个0条目。

Note that you still need to implement isContiguous( ... ) and handle null correctly. 请注意,您仍然需要实现isContiguous( ... )并正确处理null。

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

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