我試圖用名爲ted的示例項填充子網項的數組。我打開調試器,看到陣列中的項目保持爲空,即使在存在for循環之後。我可以看到currentItem被設置爲等於ted,但不會進入實際數組。我嘗試初始化每個循環中的currentItem,看看是否有幫助,但沒有。是否有可能在foreach循環中填充數組
int startHost = 0;
int endHost = 0;
int broadcast = 0;
int subnetNumber = 0;
String fullSubnetAddress = "Start address";
String fullBroadcastAddress = "Broadcast Address";
String fullStartHost = "startHost";
String fullEndHost = "endHost";
SubnetItem ted = new SubnetItem(subnetNumber, fullBroadcastAddress, fullStartHost, fullEndHost, fullBroadcastAddress);
SubnetItem[] subnetArray = new SubnetItem[this.totalSubnets];
for (SubnetItem currentSubnet : subnetArray)
{
currentSubnet = new SubnetItem();
currentSubnet = ted;
}
System.out.println("Pause");
http://stackoverflow.com/questions/11032235/java-modifying-elements-in-a- foreach –