Get index of contain sublist from list java

前端 未结 3 1748
小鲜肉
小鲜肉 2020-12-19 05:54

I have string lists look like this:

 List parentDataList:  {\"this\", \"is\", \"a\", \"test\", \"string\", \"and\", \"a\", \"test\", \"other\"}         


        
3条回答
  •  猫巷女王i
    2020-12-19 06:50

    You can change @Alessio's code like this. It also works on your cases.

    public List getIntervals(String[] parent, String[] child) {
        List intervals = new ArrayList();
        Interval interval = new Interval();
    
        for (int i = 0, j = 0; i < parent.length; i++) {
            if (child[j].equals(parent[i])) {
                j++;
                if (j == 1) {
                    interval.start = i;
                }
                if (j == child.length) {
                    interval.end = i;
                    intervals.add(interval);
                    interval = new Interval();
                    j = 0;
                }
            } else {
                j = 0;
            }
        }
    
        return intervals;
    }
    

提交回复
热议问题