【问题标题】:string variable to index of String array ??? JAVA字符串变量到字符串数组的索引??? JAVA
【发布时间】:2015-03-08 17:32:05
【问题描述】:

我有一个工作正常的字符串数组,字符串存储在数组中,但是当我想从数组中取出一个字符串并将其放入变量时,我得到了一个错误。我不明白为什么

    int arraySizelink = 1; 
    String previousUrl ;

ArrayList<String> historyArray = new ArrayList<String>();

previousUrl = historyArray.indexOf(arraySizelink);

提前致谢。

我在这里和谷歌上寻找答案,但我找不到任何东西。抱歉,如果之前有人问过这个问题。我还是 Java 新手,还在学习。

【问题讨论】:

  • 这不会编译。它不是 logcat 错误,它在 IDE 中显示为语法错误
  • indexOf an int in a string list?

标签: java string arraylist tostring


【解决方案1】:

indexOf 需要 Objectget 获取指定索引处的项目

previousUrl = historyArray.get(arraySizelink);

【讨论】:

  • 你先生!是我的救命之恩。我对 java 还是很陌生,正如你所知,它是一个学习曲线。非常感谢你:)
  • 我会带上它的 :) 谢谢
【解决方案2】:

只需进行一些调整即可获得您想要的:

int arraySizelink = 1; 

// Use the interface to declare the variable
List<String> historyArray = new ArrayList<>(); 
// If you're using Java 7+, no need to repeat the type 
// inside <> when instantiating because its figured out automatically.

// get method would take an index and return an object from the list.
String previousUrl = historyArray.get(arraySizelink);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-11
    • 2014-03-02
    • 1970-01-01
    • 2018-08-10
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    相关资源
    最近更新 更多