【问题标题】:Android Studio Change Array ValueAndroid Studio 更改数组值
【发布时间】:2017-03-06 04:34:11
【问题描述】:

您好,我想知道是否有一种方法可以获取我的数组中的一个元素并根据用户的回答将其删除。例如,在我的代码中,我有一个国家数组和一个显示国家/地区的随机生成器到用户的文本视图。如果用户按下是按钮,则生成器运行。如果用户按下否按钮,到目前为止什么都没有发生,但想法是删除正在显示的数组元素,生成器将再次运行,但是这样元素已删除。`

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button no = (Button) findViewById(R.id.No);
    final Button yes=(Button) findViewById(R.id.Yes);
    final TextView tvMessage=(TextView) findViewById(R.id.tvMessage);



    yes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String[] country = new String[10];
            country[0] = "greece";
            country[1] = "germany";
            country[2] = "america";
            country[3] = "serbia";
            country[4] = "france";
            country[5] = "england";
            country[6] = "cyprus";
            country[7] = "japan";
            country[8] = "russia";
            country[9] = "china";
            Random rand = new Random();
            int randome = rand.nextInt(10);
            tvMessage.setText(country[randome]);

        }
    });




}

【问题讨论】:

    标签: android arrays generator


    【解决方案1】:

    使用ArrayList删除动态索引值

    ArrayList<String> customlist = new ArrayList<String>();
    customlist.addAll(country);
    customlist.remove(randome);
    
    
    String []copycountry = new String[customlist.size()];
    customlist.toArray(copycountry);
    

    【讨论】:

    • 非常感谢您的宝贵帮助
    • 如果我想显示用户输入yes的国家的城市,例如如果他输入yes to greece,我想用希腊城市运行随机生成器。将 id 赋予值“greece”的arraylist 的方法,我可以创建另一个包含城市的arraylist,然后运行它???
    • 为了获得最佳效果,请使用数组列表而不是字符串数组。
    【解决方案2】:

    当数组大小不固定时,最好使用 List。

      int randome=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button no = (Button) findViewById(R.id.No);
        final Button yes=(Button) findViewById(R.id.Yes);
        final TextView tvMessage=(TextView) findViewById(R.id.tvMessage);
    
        final ArrayList<String> country = new ArrayList<>();
        country.add("greece");
        country.add("germany");
        country.add("america");
        country.add("serbia");
        country.add("france");
        country.add("england");
        country.add("cyprus");
        country.add("japan");
        country.add("russia");
        country.add("china");
        no.setsetOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (randome>=0 && randome<country.size()){
                    country.remove(randome);
                }
            }
        });
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Random rand = new Random();
                randome = rand.nextInt(country.size());
                tvMessage.setText(country.get(randome));
    
            }
        });
    }
    

    【讨论】:

    • 非常感谢您的宝贵帮助
    • 如果我想显示用户输入yes的国家的城市,例如如果他输入yes to greece,我想用希腊城市运行随机生成器。将 id 赋予值“greece”的arraylist 的方法,我可以创建另一个包含城市的arraylist,然后运行它???
    • 如果我想显示用户输入yes的国家的城市,例如如果他输入yes to greece,我想用希腊城市运行随机生成器。将 id 赋予值“greece”的arraylist 的方法,我可以创建另一个包含城市的arraylist,然后运行它???
    猜你喜欢
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    相关资源
    最近更新 更多