2013-07-08 55 views
1

我試圖寫代碼來遍歷InstallationComponentSetup類型的集合:遍歷Java中的集合(一個C++程序員)

java.util.Collection<InstallationComponentSetup> components= context.getInstallationComponents(); 
Iterator it = components.iterator(); 
while (it.hasNext()) 
{ 
    if (((InstallationComponentSetup)it).getName() == "ACQ") 
    { 
     return true; 
    } 
} 

if語句來轉換失敗,但我不真的知道爲什麼(我是一名C++程序員!)。

如果有人能給我一些關於我在做什麼錯的指示,我將不勝感激。

+0

@AndyThomas。可能重複兩個不同的問題。 B'coz這確實有一個字符串比較的問題。 –

+1

我不認爲這是重複的 - 這個問題不僅僅是比較字符串。 – arshajii

+0

@ user1414413你能定義'失敗'嗎?你是否得到了classcastexception? –

回答

2

如果您正在比較String,請使用equals()方法。 即使你的投射是錯誤的。你必須在迭代器上調用next()來獲得下一個元素。因此it.next()給出了下一個元素,它將成爲InstallationComponentSetup的一個對象,it不是InstallationComponentSetup類型的對象,因此演員將失敗。

在這裏,您正在將Iterator轉換爲您的班級類型,將會失敗。

if (((InstallationComponentSetup)it).getName() == "ACQ") 
{ 
    return true; 
} 

我認爲沒有必要鑄這裏您所定義的Collection持有特定類型的元素,也如果你聲明特定類型的Iterator。 你可以簡單地做:

// define Iterator of InstallationComponentSetup 
Iterator<InstallationComponentSetup> it = components.iterator(); 
if("ACQ".equals(it.next().getName())) { 
    return true; 
} 

您也可以考慮使用Java中的增強for循環,如果你的目的是爲只讀的元素。

for(InstallationComponentSetup component: components) { 
     if("ACQ".equals(component.getName())) { 
     return true; 
    } 
} 
0

使用it.next()獲取下一個元素。

此外,使用.equals()方法比較Java中的字符串。否則,將參考進行比較。

最後,對於類型參數化的迭代器來說,轉換不是必需的。

while (it.hasNext()) 
{ 
    if (it.next().getName().equals("ACQ")) { 
     ... 
    } 
} 
4

itIterator,而it.next()InstallationComponentSetup

錯誤來自Iterator無法投射爲InstallationComponentSetup的事實。

此外,你甚至不應該需要轉換,如果你適當地parametrizeIterator

Iterator<InstallationComponentSetup> it = components.iterator(); 

最後,不要的東西,如a == b比較字符串,而是使用a .equals (b)。進一步的細節見"How do I compare strings in Java"


你也可能想看看進入for-each循環,如果你想要做的是遍歷集合。您的代碼可以改寫爲:

for (InstallationComponentSetup component : components) 
    if (component.getName().equals("ACQ")) 
     return true; 
0

你必須在迭代檢索下一個元素你比較之前:

java.util.Collection<InstallationComponentSetup> components= context.getInstallationComponents(); 
Iterator<InstallationComponentSetup> it = components.iterator(); 
while (it.hasNext()) { 
    if ("ACQ".equals(it.next().getName())) { 
     return true; 
    } 
} 
0

這將是更容易使用foreach循環,利用通用型的,使用等於字符串和更改字符串比較順序爲null安全。

Collection<InstallationComponentSetup> components= context.getInstallationComponents(); 
for (InstallationComponentSetup setup : components) 
{ 
    if ("ACQ".equals(setup.getName())) 
    { 
     return true; 
    } 
} 
1

你有你比較之前檢索下一個元素的迭代:

InstallationComponentSetup next = it.next(); 
     if (next.getName() == "ACQ") 
     { 
      return true; 
     } 
+0

OP中的Iterator不是通用的,所以'it.next()'將返回一個'Object'類型。其次,它仍在使用'=='進行字符串比較。 –

1

嘗試使用下面的代碼。它更簡潔,更易於理解。

Collection<InstallationComponentSetup> components= context.getInstallationComponents(); 
for(InstallationComponentSetup comp : components){ 
    if("ACQ".equals(comp.getName()){ 
     return; 
    } 
} 

我想你在代碼中有兩個問題。

  1. 將迭代器強制轉換爲對象不起作用。您需要使用it.next()來獲取對象並移動迭代器。
  2. 像已經提到的你需要等於比較字符串。 ==比較「內存位置」(用C++術語)。
0

install4j API仍然適用於Java 1.4,所以現在還沒有泛型。這將工作:

for (Object o : context.getInstallationComponents()) { 
     InstallationComponentSetup component = (InstallationComponentSetup)o; 
     if (component.getName().equals("ACQ")) { 
      return true; 
     } 
    }