【问题标题】:Not taking the right value from an array没有从数组中获取正确的值
【发布时间】:2012-11-21 14:44:07
【问题描述】:

我想定义一个包含一堆字符串的数组。

然后我希望能够引用数组中的不同字符串。听起来很简单,但使用以下代码,我将返回变量 onOneClick 的“to be”(即数组中的第二个字符串)(它应该指的是第一个字符串或“暗示”。

有什么想法吗?

String hintsList[] = {"to implicate", "to be", "to bite", "to draw", "to run", "to go", "to escape", "to fall", "to accept", "to open", "to laugh", "to listen", "to open", "to dance", "to use", "to save (not waste)", "to create"};

final String onOneClick = hintsList[1];
final EditText box0101 = (EditText)findViewById(R.id.box0101);

        box0101.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtHint.setText(onOneClick);                
        }
    });

【问题讨论】:

  • 数组从索引 0 开始。CS101。

标签: java android arrays arraylist settext


【解决方案1】:

数组从零开始索引。如果你想要第一个元素,请使用hintsList[0]

【讨论】:

    【解决方案2】:

    Java 中的数组是从零开始的。你应该使用final String onOneClick = hintsList[0];

    【讨论】:

      猜你喜欢
      • 2015-04-23
      • 2017-01-02
      • 2016-04-01
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      • 2016-08-26
      相关资源
      最近更新 更多