Scala中循环初学者:如何声明泛型元素?

Scala中循环初学者:如何声明泛型元素?,scala,generics,Scala,Generics,我是Scala新手,在一个简单的泛型for循环声明中遇到了麻烦,我的类的一个实例FinSet[T]与我的另一个FinSet[T]实例“联合”,另一个实例。以下是我当前对U(Union的缩写)的实现: 这在类ListSet[T]中,它是抽象类FinSet[T]的扩展。两者都显示在这里: abstract class FinSet[T] protected () { /* returns a list consisting of the set's elements */ def toList

我是Scala新手,在一个简单的泛型for循环声明中遇到了麻烦,我的类的一个实例FinSet[T]与我的另一个FinSet[T]实例“联合”,另一个实例。以下是我当前对U(Union的缩写)的实现:

这在类ListSet[T]中,它是抽象类FinSet[T]的扩展。两者都显示在这里:

abstract class FinSet[T] protected () {
 /* returns a list consisting of the set's elements */
  def toList:List[T]

  /* given a value x, it retuns a new set consisting of x
     and all the elemens of this (set)
  */
  def +(x:T):FinSet[T]

  /* given a set other, it returns the union of this and other,
     i.e., a new set consisting of all the elements of this and
     all the elements of other
  */
      def U(other:FinSet[T]):FinSet[T]  

  /* given a set other, it returns the intersection of this and other,
     i.e., a new set consisting of all the elements that occur both
     in this and in other
  */
  def ^(other:FinSet[T]):FinSet[T]

  /* given a set other, it returns the difference of this and other,
     i.e., a new set consisting of all the elements of this that
     do not occur in other
  */
  def \(other:FinSet[T]):FinSet[T]

  /* given a value x, it retuns true if and only if x is an element of this 
  */
  def contains(x: T):Boolean

  /* given a set other, it returns true if and only if this is included
     in other, i.e., iff every element of this is an element of other
  */
  def <=(other:FinSet[T]):Boolean = 
    false // replace this line with your implementation


  override def toString = "{" ++ (toList mkString ", ") ++ "}"

  // overrides the default definition of == (an alias of equals)
  override def equals(other:Any):Boolean = other match {
    // if other is an instance of FinSet[T] then ...
    case o:FinSet[T] => 
      // it is equal to this iff it includes and is included in this
      (this <= o) && (o <= this)
    case _ => false
  }
}
抽象类FinSet[T]受保护(){
/*返回由集合元素组成的列表*/
def toList:列表[T]
/*给定一个值x,它重新运行一个由x组成的新集合
以及这(套)的所有元素
*/
def+(x:T):FinSet[T]
/*给定一个集合other,它返回该集合与其他集合的并集,
i、 例如,一个新的集合,由该集合的所有元素组成,并且
其他所有要素
*/
def U(其他:FinSet[T]):FinSet[T]
/*给定一个集合other,它返回该集合与其他集合的交集,
i、 例如,一个新的集合,由同时出现的所有元素组成
在这方面和其他方面
*/
定义^(其他:FinSet[T]):FinSet[T]
/*给定一个集合other,它返回该集合与其他集合的差值,
i、 一个新的集合,由该集合的所有元素组成
其他情况下不发生
*/
def\(其他:FinSet[T]):FinSet[T]
/*给定一个值x,当且仅当x是该值的一个元素时,它才返回true
*/
def包含(x:T):布尔值
/*给定一个集合other,当且仅当包含该集合时,它才返回true
换句话说,也就是说,如果这个元素的每个元素都是另一个元素
*/
def
//它等于它包含的和包含在本文件中的iff

(这在for循环中,您将
Int
s分配给
otherElem
x直到y
产生
范围[Int]
,这有效地为您提供了从
x
y
)的
Int
的迭代,而不是otherList的成员。您想要的是:

  def U(other:FinSet[T]) = {
    for(otherElem <- other.toList){
      this.+(otherElem)
    }
    this
  }

总的来说,生成您感兴趣的数据结构的可变版本似乎有点麻烦。我强烈建议您研究不可变的数据结构以及如何使用它们-一旦您了解了原理,使用它们会更好更安全。

您能为
FinSe添加代码吗t
?我实际上在这里实现了抽象类的扩展,名为ListSet[t].无论如何,是的,我会为FinSet和ListSet添加代码。我确实对该代码有问题,我假设它现在已修复。如果您希望包含正确的代码,我可以编辑我的问题。取决于您-如果我的答案足够有用,请继续!如果您还有其他问题,请在此处作为注释提问(如果是上述讨论中合乎逻辑的后续内容)或作为新问题提问,并在此处添加适当的细节。
abstract class FinSet[T] protected () {
 /* returns a list consisting of the set's elements */
  def toList:List[T]

  /* given a value x, it retuns a new set consisting of x
     and all the elemens of this (set)
  */
  def +(x:T):FinSet[T]

  /* given a set other, it returns the union of this and other,
     i.e., a new set consisting of all the elements of this and
     all the elements of other
  */
      def U(other:FinSet[T]):FinSet[T]  

  /* given a set other, it returns the intersection of this and other,
     i.e., a new set consisting of all the elements that occur both
     in this and in other
  */
  def ^(other:FinSet[T]):FinSet[T]

  /* given a set other, it returns the difference of this and other,
     i.e., a new set consisting of all the elements of this that
     do not occur in other
  */
  def \(other:FinSet[T]):FinSet[T]

  /* given a value x, it retuns true if and only if x is an element of this 
  */
  def contains(x: T):Boolean

  /* given a set other, it returns true if and only if this is included
     in other, i.e., iff every element of this is an element of other
  */
  def <=(other:FinSet[T]):Boolean = 
    false // replace this line with your implementation


  override def toString = "{" ++ (toList mkString ", ") ++ "}"

  // overrides the default definition of == (an alias of equals)
  override def equals(other:Any):Boolean = other match {
    // if other is an instance of FinSet[T] then ...
    case o:FinSet[T] => 
      // it is equal to this iff it includes and is included in this
      (this <= o) && (o <= this)
    case _ => false
  }
}
class ListSet[T] private (l: List[T]) extends FinSet[T] {
  def this() = this(Nil)

  // invariant: elems is a list with no repetitions
  //            storing all of the set's elements 
  private val elems = l

  private def add(x:T, l:List[T]):List[T] = l match {
    case Nil => x :: Nil
    case y :: t => if (x == y) l else y :: add(x, t)
  }

  val toList = 
    elems

  def +(x: T) = 
    this.toList.+(x)

  def U(other:FinSet[T]) = {
    var otherList = other.toList
    for(otherElem <- 0 until otherList.length){
      this.+(otherElem)
    }
    this
  }

  def ^(other:FinSet[T]) = 
    this

  def \(other:FinSet[T]) = 
    this

  def contains(x:T) = 
    false
}
  def U(other:FinSet[T]) = {
    for(otherElem <- other.toList){
      this.+(otherElem)
    }
    this
  }
def U(other:FinSet[T]) = new ListSet((this.toList ++ other.toList).distinct)