Java 从游标创建数组
在以下两种方法中(均覆盖Java 从游标创建数组,java,arrays,cursor,Java,Arrays,Cursor,在以下两种方法中(均覆盖long[]mArray),首选哪种方法 第一个方法迭代光标,为每一行调用ArrayList.add(),然后迭代ArrayList,将值复制到数组中 public void arrayFromCursor1(Cursor cursor) { // create a temp ArrayList to add to as we don't // know how many rows are in the cursor List<Long&g
long[]mArray
),首选哪种方法
第一个方法迭代光标
,为每一行调用ArrayList.add()
,然后迭代ArrayList
,将值复制到数组中
public void arrayFromCursor1(Cursor cursor) {
// create a temp ArrayList to add to as we don't
// know how many rows are in the cursor
List<Long> list = new ArrayList<Long>();
// iterate over the cursor
if (cursor.moveToFirst()) {
do {
list.add(cursor.getLong(cursor.getColumnIndex("column_name")));
} while (cursor.moveToNext());
// create a long[] of appropriate length and copy values from the
// ArrayList using a for loop
final int size = list.size();
mArray = new long[size];
for (int i = 0; i < size; i++) {
mArray[i] = list.get(i);
}
}
}
public void arrayFromCursor2(Cursor cursor) {
// no need for a temp ArrayList this time
// iterate over the cursor simply counting the rows
if (cursor.moveToFirst()) {
int size = 0;
do {
size++;
} while (cursor.moveToNext());
// create a long[] of appropriate length and iterate over the
// cursor again, this time with a for loop copying values to the array
mArray = new long[size];
cursor.moveToFirst();
for (int i = 0; i < size; i++) {
mArray[i] = cursor.getLong(cursor.getColumnIndex("column_name"));
cursor.moveToNext();
}
}
}
第二个方法在光标上迭代两次。一次,每次调用size++
对行进行计数,然后再次将值复制到数组中
public void arrayFromCursor1(Cursor cursor) {
// create a temp ArrayList to add to as we don't
// know how many rows are in the cursor
List<Long> list = new ArrayList<Long>();
// iterate over the cursor
if (cursor.moveToFirst()) {
do {
list.add(cursor.getLong(cursor.getColumnIndex("column_name")));
} while (cursor.moveToNext());
// create a long[] of appropriate length and copy values from the
// ArrayList using a for loop
final int size = list.size();
mArray = new long[size];
for (int i = 0; i < size; i++) {
mArray[i] = list.get(i);
}
}
}
public void arrayFromCursor2(Cursor cursor) {
// no need for a temp ArrayList this time
// iterate over the cursor simply counting the rows
if (cursor.moveToFirst()) {
int size = 0;
do {
size++;
} while (cursor.moveToNext());
// create a long[] of appropriate length and iterate over the
// cursor again, this time with a for loop copying values to the array
mArray = new long[size];
cursor.moveToFirst();
for (int i = 0; i < size; i++) {
mArray[i] = cursor.getLong(cursor.getColumnIndex("column_name"));
cursor.moveToNext();
}
}
}
public void arrayFromCursor1(游标){
//创建要添加到的临时数组列表
//知道光标中有多少行
列表=新的ArrayList();
//在光标上迭代
if(cursor.moveToFirst()){
做{
添加(cursor.getLong(cursor.getColumnIndex(“column_name”)));
}while(cursor.moveToNext());
//创建适当长度的long[],并从
//使用for循环的ArrayList
final int size=list.size();
mArray=新长[尺寸];
对于(int i=0;i
我想出了一个干净、简单的解决方案,可以从光标创建数组。这在将数组存储在外键表中时非常有用,并且可以使用原语
public long[] arrayFromCursor(Cursor cursor) {
int length = cursor.getCount();
long[] array = new long[length];
if (cursor.moveToFirst()) {
for (int i = 0; i < length; i++) {
array[i] = cursor.getLong(cursor.getColumnIndex("column_name"));
cursor.moveToNext();
}
}
return array;
}
public long[]数组自游标(游标游标){
int length=cursor.getCount();
long[]数组=新的long[length];
if(cursor.moveToFirst()){
for(int i=0;i
您可以使用列表中的toArray
方法mArray=list.toArray(新的long[list.size()])
。我收回它,刚刚意识到这对基元类型的数组不起作用。。。