后递增运算符和数组赋值

这是下面的代码:


public class Country{


    private String name;

    private City [] cities;

    private int index =0;


    public Country (String n, int nrc){  // nrc as in number of cities

        name = n;

        cities = new City[nrc];

    }

    public boolean exists (City str){

        for(int i =0; i>index;i++){

            if(cities[i].equals(str)){

                return true;

            }

        }

        return false;

    }


    public void addCity (City str){

        if(str == null){

            System.out.print("City not initialized!");

        }

        if(exists(str)){

            System.out.print("City  exists!");

        }

        if(cities.length == index){

            System.out.print("Not enough space in array!");

        }

        cities[index++] = str;

    }

}

我想知道cities[index++] = str应该怎么做。任何人都可以帮忙吗?


绝地无双
浏览 136回答 3
3回答

海绵宝宝撒

让我们说你有String [] cities = {"Paris","Cairo", "Addis Ababa"}范围索引将 0 upto cities.length-1 使用此索引值从数组中获取元素或将元素添加/设置到数组中以便获取元素"cairo",我们使用String str = cities[1]; // we got element "cairo"设置元素 "Paris"cities[0] = "London";所以在你的情况下:cities[index++] = str;正在将元素 str 添加到数组cities中index=index

忽然笑

它将 City 添加到城市数组中的当前索引位置并增加索引位置。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java