我有一系列不同类型的对象,我们称它们为子句

List<Clause> clauses = new ArrayList<Clause>();


在每个子句中,我想有一个子句的子列表,称为forwardClauses

我希望将每个子句放入它的forwardClauses中,然后将给定的x个子句放入过滤掉WeirdClause实例的子句之后。我也不关心WeirdClauses所拥有的forwardClauses。

所以
NormalClause extends Clause
WeirdClause extends Clause

有清单

[
NormalClause(uuid=1),
NormalClause(uuid=2),
NormalClause(uuid=3),
WeirdClause(uuid=4),
NormalClause(uuid=5)
]


如果x = 2(= 2子句向前)应该给我第一个

uuid=1
forwardClauses = [NormalClause(uuid=2), NormalClause(uuid=3)]

uuid=2
forwardClauses = [NormalClause(uuid=3), NormalClause(uuid=5)]

uuid=3
forwardClauses = [NormalClause(uuid=5)]

uuid=4
forwardClauses = []

uuid=5
forwardClauses = []


所以这是我问了这个问题(没有检查过)之后的第一个实现:

public static void setForwardClauses(List<Clause> clauses, int forwardClausesNum) {
    for (int i = 0; i< clauses.size(); i++){
        Clause clause = clauses.get(i);
        if(clause instanceof WeirdClause){
            continue;
        }
        int forwardClausesToGo = forwardClausesNum;
        for(int j = i+1; j < clauses.size(); j++){
            Clause forwardClause = clauses.get(j);
            if(forwardClausesToGo > 0 && !(forwardClause instanceof WeirdClause)){
                clause.addForwardClause(forwardClause);
                forwardClausesToGo--;
            }
        }
    }
}


编辑:基于Nukie的答案的改进版本O(N),利用O(1)list.get(i)

    private void lookForwardClauses(List<Clause clauses, int forwardClausesNum) {
        for (int i = 0; i< clauses.size(); i++){
            Clause clause = clauses.get(i);
            if(clause instanceof InnerText){
                continue;
            }
            int forwardClausesToGo = forwardClausesNum;
            int j = 1;
            while(i+j < clauses.size() && forwardClausesToGo > 0) {
                Clause forwardClause = clauses.get(i+j);
                if(!(forwardClause instanceof WeirdClause)){
                    clause.addForwardClause(forwardClause);
                    forwardClausesToGo--;
                }
                j++;
            }
        }
    }


无论我想到什么,都是O(N ^ 2),为下一个元素保留指针的方法将是微不足道的,但在这里它们必须等于forwardClausesNum,而且最重要的是一些元素没有被计算在内,因此笔者认为不会工作。

如果有更好的选择,我可以采取任何帮助,谢谢:)谢谢

最佳答案

我认为,您可以这样尝试:

public static void setForwardClauses(List<Clause> clauses, int forwardClausesNum) {
   int counter=0;
   Clause current = clauses.get(counter);
   //skip leading WeirdClauses
   do {
     counter++;
   } while (current instanceOf WeirdClause);

   if (current instanceOf WeirdClause)
     return;
   int group = 0;
   for (counter = counter + 1; counter < clauses.size();counter++){
     if (clauses.get(counter) instanceOf WeirdClause)
       continue;
     if (group == forwardClausesNum) {
       current = clauses.get(current);
       group = 0;
     } else {
       current.forwardClauses.add(clauses.get(counter));
       group++;
     }
   }
}


由于您没有在函数中添加返回值,因此我将此示例编写为副作用函数。但是您当然可以将结果放入其他集合中。

09-26 11:21