2013-11-26 34 views
1

我的一項家庭作業是編寫一個小型的增強型 for-loop with float。我理解的整數數組這個循環風格的基礎,我的理解:增強的for-loops和浮點陣列

for (int i = 0; i < some.number; i++) 

相當於

for (int i : array_example) 

如果這是正確的,這是應該如何與充滿花車一個陣列工作?我認爲增量運算符(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會這樣說?

+3

for循環的最後一部分只是一個將在每次迭代後執行的指令。你甚至可以在那裏放一個'System.out.println()',它不一定是'i ++' – Blub

+0

這也適用於增強環路嗎? – Matt

+1

第一個for循環只是一個範圍,第二個迭代通過一個集合。在這種情況下,你說array_example的類型是int。 –

回答

4

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 
} 

,如果你需要指數和一些價值原因,你需要老式的循環。

因此,對於floatArray上的老式循環,您需要一個int索引和一個浮點值。如果floatArray = [2.2,4.4,6.6,8.8]

for (int idx = 0; idx<floatArray .length; i++) { 
     // idx will be 0,1,2,3 
     float value = floatArray [i]; 
     // value will be 2.2, 4.4, 6.6, 8.8 
    } 

與增強的for循環,你只需要一個浮子

for (float value : floatArray) { 
    // value will be 2.2, 4.4, 6.6, 8.8 
+0

這很有道理,但如果我需要從索引[1]開始而不是第一個(索引[0])呢?增強環路可能嗎? – Matt

+1

@Matt,你總是可以根據你的第一個創建另一個數組,但只爲你需要的新數組提供元素,例如'陣列[1:N]'。然後爲新陣列使用增強型for-loop。 – bblincoe

+3

你可以做像@bblincoe建議的花式東西。或者有一些布爾值或計數器在開始時跳過幾個布爾值或計數器。但是,國際海事組織在棘手的情況下使用老式循環通常是最簡單的。 – user949300

1

類型改變

for (float i : array_example) 

下方的迭代器具有計數爲整數,並具有該類型的索引。

編輯代碼values[i]是錯誤的,因爲i是浮動不能是索引。 IDE是你的選擇,它在編譯之前抱怨,因爲它能夠捕獲那樣的錯誤。你應該相信它。

1
for (int i : array_example) 

閱讀是這樣的。對於int數組array_example中的每個int,請使用int做一些事情。 i只是一個隨機變量,用於accessinti可以是任何變量。如果你願意,你可以稱它爲turkey

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 

編輯:OP編輯

後,您只需要System.out.println(i);。另一個錯誤是因爲你試圖使用float i作爲索引。索引需要是int

2

for循環包含三個部分:

  1. 初始化:您initalize值。也可以是空的。
  2. A 條件:只要此條件爲真,循環將迭代。
  3. 一種指令是在每次迭代之後執行,這不一定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 
} 
3

增強的for循環是 「語法糖」 爲標準的for循環。你可以閱讀更多有關增強的for循環的位置:https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with

下將工作(假設值是一個int數組):

for (float value : values) 
{ 
    // value contains the iteration's float value. e.g. 
    System.out.println(value); 
} 

當使用增強的for循環,有沒有必要使用索引遍歷數組中的項目。

+0

實際上,當使用帶數組的增強型'for'循環時,不會使用迭代器。它使用索引。 –

1

可以將增強型for-each循環應用於任何陣列或實現Iterable接口的任何對象。

,可用於與Iterator

一個循環的Java Collections滿足的標準,並且可以與換每圈中使用的short-cut

你的具體情況(float型數組),你將有這樣的事情:

float[] fArray = {1f, 2f, 3f, 4f, 23F, 24F}; 
for (float element: fArray) { 
    System.out.println(element); 
} 

請注意,element是一個實際的數組元素的副本,如果直接賦值給它,它將不會對陣列本身產生影響。

2

兩個for循環並不完全相同。第一個for循環實際上大部分類似於while循環。

for(init; condition; post) { 
     ... code 
    } 

是類似於:

init; 
    while(condition) { 
     ... code 
     post; 
    } 

如果深究下去,你會發現,大多數語法可以使用goto方法被重寫。這只是使糖「不太」容易出錯的合成糖。

另一種形式是使用迭代器。這個表單可以從數組等對象中獲得迭代器。可以在其他對象上實現。也就是說,你的代碼有兩個問題。

for (float i : values) 
    { 
     values[i] = i; //Getting an error here "can't convert from float to int. 
    } 

當你遍歷值,你可以不設置values[i]的東西,因爲i是float類型。數組的索引通常是整數,因爲您無法訪問兩個對象之間的對象。

第二個不太明顯的問題是您要更改for循環中值的值。上面的代碼有可能工作,但你必須明白,迭代器不是簡單的迭代列表中的對象的方法。

據我記憶,如果你有某種協調。編輯迭代器指向的值可能會使迭代器失效。它會引發異常,因爲您可能正在迭代無效數據。如果可能的話,迭代器模式通常強制引發異常並重新啓動for循環。如果您在for循環內更改數據,則可能會引發此類異常。