我试图创建一个算法来确定一个数组列表(s1)是否是另一个数组列表(s2)的子序列。第一个列表中的元素都需要在第二个列表中,并且顺序必须相同所以s1是s2的子序列,但s1不是s2的子序列我需要使用迭代器遍历每个列表,并且我应该只遍历每个列表一次(因为它必须以相同的顺序)我好像对next()变空有意见??是什么原因造成的/我怎样才能解决这个问题?
我目前拥有的代码似乎可以正常工作,只是没有转到第一个数组列表中的下一个元素。

import dataStructures.*;

public class Subsequence2
{
    public static void main(String[] args)
   {
       ArrayList<Character> s1 = new ArrayList<Character>();
       s1.add('n');
       s1.add('p');
       s1.add('a');

       ArrayList<Character> s2 = new ArrayList<Character>();
       s2.add('a');
       s2.add('n');
       s2.add('b');
       s2.add('p');
       s2.add('c');
       s2.add('a');

       Subsequence2 one = new Subsequence2();
       System.out.print("S1 is a subsequence of S2 is a ");
       System.out.print(one.subSequence(s1, s2));
       System.out.print(" statment.");
} //end main

public static <T> boolean subSequence(ArrayList<T> s1, ArrayList<T> s2)
{
 //if s1 is empty or if s1 and s2 are empty it is a subsequence
    if(s1.isEmpty() || (s1.isEmpty() && s2.isEmpty()))
    {
        return true;
    }
  //if s2 is empty and s1 is not is is not a subsequence.
    else if(s2.isEmpty())
    {
       return false;
    }
    else
    {
        int s1Count = 0; //count items matched
        Iterator<T> itr1 = s1.iterator();
        Iterator<T> itr2 = s2.iterator();
        while(itr1.hasNext())
     //for(Iterator<T> itr1 = s1.iterator(); itr1.hasNext();) //traverse s1
        {
            T c1 = itr1.getCurrent();
            itr1.next(); //go to next element of s1
            while(itr2.hasNext()) //traverse s2
            {
                T c2 = itr2.getCurrent();
//if items are equal check the next item and add 1 to count of items matched
               if(c1.equals(c2))
               {
                  itr2.next();
                  ++s1Count;
                  //used for testing- just want to see what index it is pulling
                  System.out.print("s1 index " + s1.indexOf(c1) + " s2 index " + s2.indexOf(c2) + " \n" + c1 + " "  + c2 + "\n");
               }
    //if it didn't match, check next element
               else
               {
                   itr2.next();
               }
               if(s1Count == s1.size()) //if match count is == to s1 size, it is a subsequence
               {
                  return true;
               }
            } // end itr2 while
         } //end for itr1
      } //end else not empty
      return false;
   } //end subSequence method
 }//end class

最佳答案

内环上的条件不应该只是itr2.hasNext()。只要itr2.hasNext()内循环循环循环一次,并且下一项与itr1的当前项不同一旦找到(在itr2中)当前的itr1项,就要停止迭代内部循环,并返回itr1中的下一项。

public static <T> boolean subSequence(ArrayList<T> s1, ArrayList<T> s2) {
    Iterator<T> itr1 = s1.iterator();
    Iterator<T> itr2 = s2.iterator();

    while (itr1.hasNext()) {
        T itemFrom1 = itr1.next();
        T itemFrom2;
        do {
            if( ! itr2.hasNext()){
                return false;
            }
            itemFrom2 = itr2.next();
        } while( ! itemFrom1.equals(itemFrom2));
    }
    return true;
}

基本上,这里发生的是,当我们还在迭代时,如果我们在itr1中用完了元素,那么我们还有itr2中的元素要匹配,所以它不是子序列。但只要我们继续在itr1中找到东西,我们就继续前进每当我们在itr2中找到与我们在itr2中查找的内容匹配的内容时,就应该进入外部循环的下一步了。

关于java - Java ArrayList迭代器next()无法按预期工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22884335/

10-13 09:53