Java 增强的循环和浮点数组
我的一个家庭作业是编写一个带有浮点数的小型增强for循环。据我所知,我了解使用整数数组的这种循环样式的基础:Java 增强的循环和浮点数组,java,arrays,for-loop,floating-point,Java,Arrays,For Loop,Floating Point,我的一个家庭作业是编写一个带有浮点数的小型增强for循环。据我所知,我了解使用整数数组的这种循环样式的基础: for (int i = 0; i < some.number; i++) 如果这是准确的,那么对于一个充满浮点数的数组,这应该如何工作呢?我认为增量运算符(I++)只允许使用整数。只是想澄清一下 谢谢 编辑:下面是一个例子,说明我在做什么,Eclipse不喜欢什么。这可能让我更加困惑: public class java_array { public static
for (int i = 0; i < some.number; i++)
如果这是准确的,那么对于一个充满浮点数的数组,这应该如何工作呢?我认为增量运算符(I++
)只允许使用整数。只是想澄清一下
谢谢
编辑:下面是一个例子,说明我在做什么,Eclipse不喜欢什么。这可能让我更加困惑:
public class java_array
{
public static void main(String[] args)
{
float[] values = {1/2,1/3,5/3};
float total = 0;
for (float i : values)
{
values[i] = i; //Getting an error here "can't convert from float to int.
}
for (float i : values)
{
System.out.println(values[i]); //Getting an error here "can't convert from float to int.
}
}
}
所有内容都已声明为float
。为什么IDE会这样说?类型会改变
for (float i : array_example)
在迭代器下面有一个计数为整数的索引,并且具有该类型
在编辑代码中,值[i]
是错误的,因为i
是浮动的,不能作为索引。IDE是您的选择,它会在编译之前抱怨,因为它能够捕获这样的错误。你应该相信它。类型会改变
for (float i : array_example)
for (int i : array_example)
在迭代器下面有一个计数为整数的索引,并且具有该类型
在编辑代码中,值[i]
是错误的,因为i
是浮动的,不能作为索引。IDE是您的选择,它会在编译之前抱怨,因为它能够捕获这样的错误。你应该相信它
for (int i : array_example)
读起来像这样。对于int-arrayarray\u示例中的每个int
,使用该int
执行一些操作。i
只是用来访问int
的一个随机变量i
可以是任何变量。如果你愿意,你可以叫它土耳其
int[] array_example = {1, 2, 3};
for (int turkey : array_example){ // for each int in array_example
System.out.print(turkey + ", "); // print out that int
}
output: 1, 2, 3
对于float
float[] floatArray = {1.2, 2.3, 3.4};
for (float watermelon : floatArray){
System.out.print(watermelon + ", ");
}
output: 1.2, 2.3, 3.4
编辑:操作后编辑
您只需要System.out.println(i)代码>。另一个错误是因为您试图使用float i
作为索引。索引必须是int
读起来像这样。对于int-arrayarray\u示例中的每个int
,使用该int
执行一些操作。i
只是用来访问int
的一个随机变量i
可以是任何变量。如果你愿意,你可以叫它土耳其
int[] array_example = {1, 2, 3};
for (int turkey : array_example){ // for each int in array_example
System.out.print(turkey + ", "); // print out that int
}
output: 1, 2, 3
对于float
float[] floatArray = {1.2, 2.3, 3.4};
for (float watermelon : floatArray){
System.out.print(watermelon + ", ");
}
output: 1.2, 2.3, 3.4
编辑:操作后编辑
您只需要System.out.println(i)代码>。另一个错误是因为您试图使用float i
作为索引。索引必须是int
for循环包含三个部分:
初始化:初始化值。也可以是空的
条件:只要该条件为真,循环就会迭代
每次迭代后执行的指令,不一定是i++
第二个for循环实际上是foreach。它所做的是遍历数组中的所有元素并将它们返回给您。数组中的对象无关紧要,它可以是int、float、Object等等
例如:
Object [] objects = new Object[100];
for (Object o : objects) {
// will go through all 100 elements, returning them one after the other as o
}
float [] floats = new float[100];
for (float f : floats) {
// will go through all 100 elements as well, returning them as f
}
for循环包含三个部分:
初始化:初始化值。也可以是空的
条件:只要该条件为真,循环就会迭代
每次迭代后执行的指令,不一定是i++
第二个for循环实际上是foreach。它所做的是遍历数组中的所有元素并将它们返回给您。数组中的对象无关紧要,它可以是int、float、Object等等
例如:
Object [] objects = new Object[100];
for (Object o : objects) {
// will go through all 100 elements, returning them one after the other as o
}
float [] floats = new float[100];
for (float f : floats) {
// will go through all 100 elements as well, returning them as f
}
增强的for循环返回数组中的值,而不是索引
在整数情况下,如果myArray=[2,4,6,8]代码>
for (int i : myArray)
// returns 2,4,6,8, NOT 0,1,2,3
在传统循环中,您有一个额外的步骤
for (int idx = 0; idx<myArray.length; i++) {
// idx will be 0,1,2,3
int value = myArray[i]; <<< in many cases this is an extra step
// value will be 2,4,6,8
}
增强的for循环返回数组中的值,而不是索引
在整数情况下,如果myArray=[2,4,6,8]代码>
for (int i : myArray)
// returns 2,4,6,8, NOT 0,1,2,3
在传统循环中,您有一个额外的步骤
for (int idx = 0; idx<myArray.length; i++) {
// idx will be 0,1,2,3
int value = myArray[i]; <<< in many cases this is an extra step
// value will be 2,4,6,8
}
增强的for循环是标准for循环的“语法糖”。您可以在此处阅读有关增强for循环的更多信息:
以下方法可行(假设值是浮点数组):
使用增强型for循环时,不需要使用索引来迭代数组中的项。增强型for循环是标准for循环的“语法糖”。您可以在此处阅读有关增强for循环的更多信息:
以下方法可行(假设值是浮点数组):
使用增强型for循环时,无需使用索引来迭代数组中的项。您可以将每个
循环的增强型应用于实现接口的任何数组或任何对象
这是一个循环
Java满足这些条件,可以与for-each循环一起使用
在您的特定情况下(浮动数组),您将有如下内容:
float[] fArray = {1f, 2f, 3f, 4f, 23F, 24F};
for (float element: fArray) {
System.out.println(element);
}
请注意,element
是实际数组元素的副本,如果直接为其赋值,则不会对数组本身产生影响 您可以将每个
循环的增强型应用于实现接口的任何数组或任何对象
这是一个循环
Java满足这些条件,可以与for-each循环一起使用
在您的特定情况下(浮动数组),您将有如下内容:
float[] fArray = {1f, 2f, 3f, 4f, 23F, 24F};
for (float element: fArray) {
System.out.println(element);
}
请注意,element
是实际数组元素的副本,如果直接为其赋值,则不会对数组本身产生影响 两个for循环并不完全相同。第一个for循环实际上与while循环非常相似
for(init; condition; post) {
... code
}
类似于:
init;
while(condition) {
... code
post;
}
如果深入研究,您会发现大多数语法都可以使用gotos重写。它只是合成糖,让它“不那么”容易出错
另一种形式是使用迭代器。这个for表单能够得到一个迭代器