什么是“:”在这个初学java做的示例程序中使用泛型? [英] What is ":" doing in this beginners java example program using generics?

查看:125
本文介绍了什么是“:”在这个初学java做的示例程序中使用泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我需要帮助理解一些东西。我明白?:是如何一起使用的,但是阅读了一些Java开始,我发现这种情况在几个地方出现了。最近是这样的...

  public static< U> (box> box){
box.add(u);






$ p我很困惑的是:是在做。任何帮助,将不胜感激。我正在Oracle网站的一个页面上看到这个例子: http ://download.oracle.com/javase/tutorial/java/generics/genmethods.html

解决方案

是Java的for-each循环结构。这与泛型没有任何关系,或者:不适用于泛型。它的简写如下:为集合中的每个类型框命名框执行以下操作...



这里是链接到官方文档



简单的代码示例:(而不是管理泛型执行int数组求和)

  int [] intArray = {1,5,9,3,5}; 
int sum = 0;
for(int i:intArray)sum + = i;
System.out.println(sum);




输出:23



Okay so I need help understanding something. I understand how "? :" are used together but reading over some beginning Java stuff I see this situation popping up in a few places. Most recently is this...

public static <U> void fillBoxes(U u, List<Box<U>> boxes) {
    for (Box<U> box : boxes) {
        box.add(u);
    }
}

What I am confused about is what exactly ":" is doing. Any help would be appreciated. I am looking at this example on a page at Oracle's website, located here: http://download.oracle.com/javase/tutorial/java/generics/genmethods.html

解决方案

That is Java's for-each looping construct. It has nothing to do with generics per-se or: is not for use exclusively with generics. It's shorthand that says: for every type box in the collection named boxes do the following...

Here's the link to the official documentation.

Simpler code sample: (instead of managing generics performing a summation of an int array)

int[] intArray = {1,5,9,3,5};
int sum = 0;
for (int i : intArray) sum += i;
System.out.println(sum);

Output: 23

这篇关于什么是“:”在这个初学java做的示例程序中使用泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆