简体   繁体   English

对下界通配符使用增强的for循环

[英]Using enhanced for loop with lower bounded wildcard

I have this for loop: 我有这个循环:

for (int j = 0; j < list.size(); j++) {
    System.out.print(list.get(j) + " ");
}

the signature of the method is: 该方法的签名是:

public static void addNumbers(List<? super Number> anoList)

the list is declared as: 该列表声明为:

static List<? super Number> list2;

or 要么

static List list2;

for that matter. 对于这个问题。

All good so far. 到目前为止一切都很好。 But then Netbeans gives a hint to convert the for loop to an enhanced for loop. 但是随后Netbeans给出了将for循环转换为增强的for循环的提示。 When i apply that i get: 当我申请时,我得到:

    for (? super Number anoList1 : anoList) {
        System.out.print(anoList1 + " ");
    }

which is not working and produces lots of errors. 这是行不通的,并且会产生很多错误。 For one: cannot find anoList1 and . expected 对于一个: cannot find anoList1. expected . expected , : expected , ; expected . expected : expected ; expected ; expected . ; expected

I have tried several things to get rid of the errors, but none of them is working. 我尝试了几种方法来消除错误,但是没有一个起作用。

What is the correct way for this enhanced for loop to be written? 编写此enhanced for loop的正确方法是什么?

From other topics i have read, the advice is not to use an enhanced for loop at all and leave it like it is. 从我阅读过的其他主题中,建议不要完全使用enhanced for loop并保持原样。

What is the correct way for this enhanced for loop to be written? 编写此增强的for循环的正确方法是什么?

for (Object obj : anyList) {...}

Since a List<? super Number> 由于List<? super Number> List<? super Number> is a List of elements which could be any supertype of Number (including Object ), the only guarantee we have about the actual type of the List is Object . List<? super Number>是元素的List,它可以是Number任何超类型(包括Object ),我们对List实际类型的唯一保证是Object

Here ? super Number 在这里? super Number ? super Number means that the list may contain elements of any type which is supertype of Number . ? super Number表示列表可以包含Number超类型的任何类型的元素。 Practically this means that nothing can be guaranteed about the list elements type except that they are descendants of Object type. 实际上,这意味着除了列表元素类型是Object类型的后代之外,不能保证任何其他内容。 Such method signature actually look useless. 这种方法签名实际上看起来毫无用处。 Probably you wanted to use 可能您想使用

public static void addNumbers(List<? extends Number> anoList)

Which would mean that list element type is some subtype of Number like List<Integer> or List<Double> . 这意味着列表元素类型是Number某些子类型 ,例如List<Integer>List<Double>

I'm riding on the answer given by Tagir. 我是塔吉尔(Tagir)给出的答案。 You'll see that this won't compile 您会看到它不会编译

public static void addNumbers(List<? super Number> anoList){
    for(Number n : anoList){ // Fail

    }
}

Fails with 与失败

Error:(29, 24) java: incompatible types: capture#1 of ? super java.lang.Number cannot be converted to java.lang.Number

But this is good 但这很好

public static void addNumbers(List<? extends Number> anoList){
    for(Number n : anoList){

    }
}

So as Tagir mentioned, you probably want to use extends instead of super . 因此,正如塔吉尔(Tagir)所提到的,您可能要使用extends而不是super However, if you really do need ? super Number 但是,如果您确实需要? super Number ? super Number , then Radiodef's answer is your only option for the enhanced for loop. ? super Number ,那么Radiodef的答案是增强的for循环的唯一选择。

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

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