【问题标题】:android programming安卓编程
【发布时间】:2011-05-23 07:22:48
【问题描述】:
questionArray.addObject("Hi honey, I just got to work. What are you upto?");
    answerArray.addObject("I was just thinking about you darling.:Just missing you from the time you left.:I was just going through our wedding photos.:Well right now, just talking to you & its giving me a headache.:Nothing much I was just texting my ex-boyfriend.");
    subQuestionArray.addObject("What do you mean by that?");
    subAnswerArray.addObject("Oh nothing dear, just missing you.:Nothing, just that I think dumping my ex was a mistake.");
    statementArray.addObject("Good to know, I've work to do, BYE!");

我遇到了一个错误。 (不能在数组类型 String[] 上调用 addObject(String)) 在 Java 中,questionArray、answerArray、subQuestionAray、statementArray 是字符串数组,应该使用什么来代替 addobject? 我正在使用 Eclipse helios 在 Android 上开发游戏。

【问题讨论】:

  • 您在哪里看到可以在数组上使用.addObject(String s);?我在文档中找不到该方法。
  • 其实这是在 iphone xcode 中使用的,我把它转换成 android,所以想要替换上面的..

标签: java android arrays


【解决方案1】:

将项目添加到String[] 的正确方法是使用索引:

String[] questionArray = new String[5]; //if there are five questions
questionArray[0] = "Hi honey, I just got to work. What are you upto?";

或者,如果您事先不知道数组的大小,请使用ArrayList,它会随着您添加更多项目而自动增大。

List<String> questionList = new ArrayList<String>();
questionList.add("Hi honey, I just got to work. What are you upto?");

【讨论】:

    【解决方案2】:

    您不能在 Java 中将对象添加到数组中。数组具有固定的宽度。您应该改用List<>Vector<>

    【讨论】:

      【解决方案3】:

      也许您应该考虑改用List&lt;String&gt; questionList = new ArrayList&lt;String&gt;()

      数组的大小不是动态的,您不能只添加超出数组大小的对象。使用 List 或 Vector 之类的集合。

      【讨论】:

        【解决方案4】:

        将 questionArray 等变成 向量 = 新向量(); 或者 ArrayList = new ArrayList(); 并使用 .add(对象o) 代替方法。这将解决你所有的问题:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-11-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-18
          • 2014-03-03
          相关资源
          最近更新 更多