简体   繁体   English

不兼容的类型编译错误

[英]Incompatible type compile error

link for my code error 我的代码错误的链接 在此处输入图片说明

Incompatible type found java.util.vector required HighRights. 找到不兼容的类型java.util.vector需要HighRights。 Sorry I am new to Java and I just don't understand how to do this question 抱歉,我是Java的新手,我只是不明白该怎么做

Notice: in this exercises we will NOT use Generics 注意:在本练习中,我们将不使用泛型

In the following program, the code in the method CountHighs is missing. 在以下程序中,缺少CountHighs方法中的代码。 Write the code for this method, which takes as argument the vector m and returns the number of objects in the vector that are instances of HighRights 编写此方法的代码,该方法将向量m作为参数,并返回向量中作为HighRights实例的HighRights

The method should also: - check that the elements extracted from the vector are indeed instances of the classes HighRights or LowRights . 该方法还应该:-检查从向量中提取的元素确实是类HighRightsLowRights实例。 If an element is not an instance of such classes, then the method should return -1. 如果元素不是此类的实例,则该方法应返回-1。

  • handle the NullPointerException in case the vector is null. 如果向量为null,则处理NullPointerException Use the following code when catching the occurring exception: 捕获发生的异常时,使用以下代码:

     System.out.println("Error"); System.exit(0); return 0; 

(Hint: (暗示:

  1. use m.size() to get the number of elements in the vector m 使用m.size()获得向量m中的元素数
  2. use the keyword instanceof to check if an object is an instance of a class) 使用关键字instanceof检查对象是否为类的实例)

For example, 例如,

  • if the vector m contains only two HighRights objects and one LowRights objects then CountHighs(m) will return 2 如果向量m仅包含两个HighRights对象和一个LowRights对象,则CountHighs(m)将返回2
  • if the vector m contains only two HighRights objects and one String objects then CountHighs(m) will return -1 如果向量m仅包含两个HighRights对象和一个String对象,则CountHighs(m)将返回-1
  • if the vector m contains only five LowRights objects no HighRights objects then CountHighs(m) will return 0 如果向量m仅包含五个LowRights对象,而没有HighRights对象,则CountHighs(m)将返回0

Any help/tips will be appreciated. 任何帮助/提示将不胜感激。 Thank you very much. 非常感谢你。

You should post full code (not as image) and the error stack trace, but I think I know the problem. 您应该发布完整的代码(而不是图像)和错误堆栈跟踪,但是我想我知道问题所在。 You have a public static int CountHighs(Vector m) method, and inside it you check condition m instanceof HighRights/LowRights - it's never true, a Vector is a Vector, not HighRights. 您有一个public static int CountHighs(Vector m)方法,在其中检查条件是否为m instanceof HighRights/LowRights永远不是真的,Vector是Vector,而不是HighRights。 You need to check if objects stored in the vector are HighRights or LowRights. 您需要检查矢量存储的对象是HighRights还是LowRights。 To do this, you should use a loop: 为此,您应该使用循环:

for(Object obj : m){ //it will iterate over the vector `m`, with each iteration `obj` will be the next element
    if(obj instanceof HighRights){
        ++countHighRights;
    }
    else if(obj instanceof LowRights){
        //do nothing - you should only count HighRights
    }
    else{ //obj is neither HighRight nor LowRight
        return -1;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM