将ArrayList <xyz>转换为ArrayList <float>

时间:2016-04-20 10:15:16

标签: android sqlite arraylist

我对class Dir def Dir.[] end def Dir.chdir end def Dir.chroot end def Dir.delete end def Dir.entries end def Dir.exist? end def Dir.exists? end def Dir.foreach end def Dir.getwd end def Dir.glob end def Dir.home end def Dir.mkdir end def Dir.initialitze end def Dir.open end def Dir.pwd end def Dir.rmdir end def Dir.unlink end def close end def each end def fileno end def inspect end def path end def pos end def read end def rewind end def seek end def tell end def to_path end end 相当陌生,我只想尝试使用不同的ArrayListArrayList的类型为float。我用它来在Sqlite中的数据库中存储值。稍后,我尝试将存储的值移动到ArrayList<xyz>。我知道两者都有不同的类型,但有没有办法进行转换。任何形式的帮助表示赞赏。

以下是代码:

Arraylist<float>

2 个答案:

答案 0 :(得分:1)

将以下函数添加到类xyz。

public Float floatValue()
{
 return this.f;
}

假设f是类xyz的float变量。

现在你可以将arraylist填充为:假设list_of_objects是xyz类对象的arraylist。

Arraylist<float> al=new Arraylist<float>();
for(xyz o: list_of_objects)
 al.add(o.floatValue());

答案 1 :(得分:1)

您必须迭代ArrayList,并为每个值进行转换并将其存储在ArrayList中。尝试下一个代码:

ArrayList<xyz> a1 = new ArrayList<xyz>();

//set the data in a1....

ArrayList<Float> a2 = new ArrayList<Float>();
int n = a1.size();

for(int i=0;i<n;i++)
    a2.add(a1.get(i).methodXYZtoFloat());

在方法 methodXYZtoFloat 它将返回Float )中,您必须定义XYZ到Float的转换。

希望它有所帮助!