Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果运算符符号后有方法(而不是变量),如何实现左关联性?_Java_Algorithm_Associativity - Fatal编程技术网

Java 如果运算符符号后有方法(而不是变量),如何实现左关联性?

Java 如果运算符符号后有方法(而不是变量),如何实现左关联性?,java,algorithm,associativity,Java,Algorithm,Associativity,最近我又回到了编码(有一段时间没做了)。我正在尝试编写一个程序,它将以左关联顺序而不是右关联顺序保存和执行集合上的操作 SetInterface<BigInteger> performOperation(Scanner in, SetInterface<BigIneger> firstSet) { SetInterface<BigInteger> result = new Set<BigInteger>(); SetInt

最近我又回到了编码(有一段时间没做了)。我正在尝试编写一个程序,它将以左关联顺序而不是右关联顺序保存和执行集合上的操作

SetInterface<BigInteger> performOperation(Scanner in, SetInterface<BigIneger> firstSet) {
      SetInterface<BigInteger> result = new Set<BigInteger>();

      SetInterface<BigInteger> secondSet = nextSet(in); //this line will read the next set, and will call performOperation()

      if (operator == '+') {
            result = ((Set<BigInteger>) firstSet).union((Set) secondSet);
            //I have tried the below two lines of code, but they both did not result in left associtivity
            //result = secondSet.union((Set) firstSet);
            //result = result.union((Set) secondSet);
      }

      return result;
SetInterface performOperation(扫描仪输入,SetInterface firstSet){
SetInterface结果=新集合();
SetInterface secondSet=nextSet(in);//此行将读取下一个集合,并将调用performOperation()
if(运算符=='+'){
结果=((集)第一集)。并集((集)第二集);
//我试过下面两行代码,但它们都没有产生左联想
//结果=secondSet.union((Set)firstSet);
//结果=结果.union((Set)secondSet);
}
返回结果;

我的集合类中的union方法如下所示:

公共集合联合(集合第二集合){
集合结果=新集合();
Set secondSetCopy=new Set();
result.set=(LinkedList)this.set.copy();
secondSetCopy.set=(LinkedList)secondSet.set.copy();
while(secondSetCopy.size()>0){
add((T)secondSetCopy.get());
secondSetCopy.remove(secondSetCopy.get());
}
返回结果;
}
上述代码的结果如下:A+(B+(C+D))
我想达到的是:((A+B)+C)+D


如果有人能告诉我如何解决这个问题,我将不胜感激

public Set union(Set secondSet) {
        Set<T> result = new Set<T>();
        Set<T> secondSetCopy = new Set<T>();

        result.set = (LinkedList<T>) this.set.copy();
        secondSetCopy.set = (LinkedList<T>) secondSet.set.copy();

        while (secondSetCopy.size() > 0) {
            result.add((T) secondSetCopy.get());
            secondSetCopy.remove(secondSetCopy.get());
        }

        return result;
    }