我必须在数组末尾添加一个字符串参数。如果 String 已经存在于数组中,则抛出异常。我不知道。任何人都可以帮忙吗?这是我到目前为止所拥有的
public static void addCity (String city) throws Exception
{
for (int i = 0; i < MAX_CITIES;i++)
{
if (city == cityNames[i])
throw new Exception("This city already exists");
}
String [] temp = new String[cityNames.length+1];
for (int i = 0; i < cityNames.length;i++) {
temp[i] = cityNames[i];
}
temp[temp.length-1] = city;
cityNames = temp;
manyItems++;
}