【问题标题】:How to fix "Error: bad operand types for binary operator" in generic method?如何在泛型方法中修复“错误:二元运算符的错误操作数类型”?
【发布时间】:2019-04-27 07:44:47
【问题描述】:

我正在尝试打印数组中特定范围的内容。而且,我需要使用泛型方法,但我不断收到这些错误,说二元运算符的操作数类型错误。我想问我哪部分做错了,我该如何解决错误?

我正在使用 drjava。

  Integer[ ] integerArray = { 1, 2, 3, 4, 5};
  Double[ ] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5 };
  Character[ ] characterArray = { 'H', 'E', 'L', 'L', 'O' };
  Pet[ ] petArray = { new Pet( "Bob", "Tortoise", "TSA", "19950315" ),
                      new Pet( "SweetPea", "Horse", "Genie", "20030214" ),
                      new Pet( "Little", "Chicken", "John", "20190123" ),
                      new Pet( "Dale", "Chipmunk", "Sam", "20090527" ),
                      new Pet( "Smokey", "Bear", "USPW", "19440413" )     };
  
  System.out.printf( "%nRange of integerArray contains:%n" );
  printRange( integerArray, 1, 3); 
  
  System.out.printf( "%nRange of doubleArray contains:%n" );
  printRange( doubleArray, 1, 3 ); 

  System.out.printf( "%nRange of characterArray contains:%n" );
  printRange( characterArray, 1, 3 ); 
  
  System.out.printf( "%nRange of petArray contains:%n" );
  printRange( petArray, 1, 3 );
 
} // end main

// 这是我遇到错误的部分

public static <T> void printRange( T[ ] inputArray, T start, T stop ){

 // display array elements 
 // Error:bad operand type T for unary operator '++' and Error: bad operand types for binar operator
 
    for( T element = start; element < stop; element++ )    
    {
        //Error: bad operand types for binary operator '>='
        first type:  T
        second type: int
        // Error: bad operand types for binary operator '<='
        first type:  T
        second type: T
        //Error: bad operand types for binary operator '<'
        first type:  T
        second type: T
        if( start < stop && start >= 0 && stop <= inputArray[inputArray.length-1] )
        {
            System.out.printf( "%s", element.toString( ) );
        }
   
    } // end enhanced for loop

    System.out.println( );
 
} // end method printRange                                    
} // end class ArrayMethods

这是预期的输出。 integerArray 包含的范围:

2 3

2.2 3.3

E L

【问题讨论】:

  • 泛型类型T可以和int比较
  • 如果T 没有类型约束,则不会定义任何算术运算符,包括++。在某种程度上,它就像使用Object 类型,但实际上并没有向上转换为Object。我感觉您打算使用索引 for 循环,在这种情况下,您的索引计数器应始终为 int
  • 好的,我更改了代码,但仍然在“System.out.printf("%s", element.toString);" 上显示一个小错误。它说错误:不能取消引用int。 } // 结束增强 for 循环

标签: java arrays generics generic-collections


【解决方案1】:

++&gt;.. 之类的操作只允许用于数字类型。对于startstop 变量,您应该使用int 类型。并且更容易复制源数组的特定范围来打印它:

public static <T> void printRange(T[] inputArray, int start, int stop) {
    T[] copy = Arrays.copyOfRange(inputArray, start, stop);
    System.out.println(Arrays.toString(copy));
}

更新:使用for循环:

for (int i = 0; i < inputArray.length; i++) {
    if (i >= start && i < stop) {
        System.out.printf("%s ", inputArray[i]);
    }
}

【讨论】:

  • 在标准 for 循环中是否有这种情况,而不是使用 copyOfRanfe( )?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 2016-06-24
相关资源
最近更新 更多