当使用循环增强的是数组被改造? [英] Are arrays being transformed when using an enhanced for loop?

查看:97
本文介绍了当使用循环增强的是数组被改造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java的是否5或更高版本适用于一些拳的形式来阵列?这个问题浮现在脑海如下code经过一个数组,就好像它是一个可迭代。

 为(字符串:参数){
// 做东西
}


解决方案

没有,数组的总是的引用类型。没有必要为拳击或拆箱,除非它在每个元素的访问。例如:

  INT [] X = INT新[10]; // x的值是参考INT Y = X [0]; //没有拳击
整数z = X [1]; //从X [1](这是一个int)以整数拳击转换

另外要注意的是,虽然增强的for循环可用于在阵列和iterables,它的编制分别为每个

请参阅节14.14.2 为两个不同的翻译增强的for循环。

Does Java 5 or higher apply some of form of "boxing" to arrays? This question came to mind as the following code goes through an array as if it's an Iterable.

for( String : args ){
// Do stuff
}

No, arrays are always reference types. There's no need for boxing or unboxing, unless it's on the access for each element. For example:

int[] x = new int[10]; // The value of x is a reference

int y = x[0]; // No boxing
Integer z = x[1]; // Boxing conversion from x[1] (which is an int) to Integer

Also note that although the enhanced for loop is available for both arrays and iterables, it's compiled differently for each.

See section 14.14.2 of the JLS for the two different translations of the enhanced for loop.

这篇关于当使用循环增强的是数组被改造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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