gpt4 book ai didi

java - 在 Java 中创建并迭代字符串列表

转载 作者:行者123 更新时间:2023-12-01 14:47:57 26 4
gpt4 key购买 nike

我正在尝试创建大小动态的数组列表,添加元素并在完成后迭代它们。目前我的代码是:

    String activities = "SELECT DISTINCT phoneactivity from ContextTable030313";
ArrayList<String> myActivities = new ArrayList();

Cursor cursor2 = db.rawQuery(activities, null);
// looping through all rows and adding to list
if (cursor2.moveToFirst()) {
do {
myActivities.add(cursor.getString(cursor2.getColumnIndex("ts")));

} while (cursor2.moveToNext());
}

但是它无法在 do 循环中运行。我相信我声明了一些错误的内容,并且收到以下警告:

- ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized
- Avoid object allocations during draw/layout operations (preallocate and reuse instead)
- Type safety: The expression of type ArrayList needs unchecked conversion to conform to
ArrayList<String>

但我不明白为什么这不起作用。

最佳答案

问题似乎是这样的:

Type safety: The expression of type ArrayList needs unchecked conversion to conform to ArrayList

这指出了这行代码:

ArrayList<String> myActivities = new ArrayList();

您应该将该行更改为:

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

换句话来说,您可以替换此代码段:

  if (cursor2.moveToFirst()) {
do {
myActivities.add(cursor.getString(cursor2.getColumnIndex("ts")));

} while (cursor2.moveToNext());
}

这样:

  while (cursor2.moveToFirst()) {       
myActivities.add(cursor.getString(cursor2.getColumnIndex("ts")));
}

关于java - 在 Java 中创建并迭代字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15217318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com