Java Android数据传递数据双倍 [英] Java Android passing data Array double

查看:109
本文介绍了Java Android数据传递数据双倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序意图的问题

I have a question about intent in android application

我有一个数组在 class1

double [][] tableCityPair = new double[100][100];

---这里代码填写数组---
在类的末尾想要将tableCityPair发送到另一个类, class2

---here code to fill array--- in the end of class i want to send tableCityPair to another class, class2.

我应该如何在class2中声明数组?

是这个对吗?

how i should declare for array in class2 ?
is this right?

Intent it = getIntent();
double tabelJarakAntarKota= it.getDoubleExtra("tableCityPair",3);


推荐答案

Bundle class有传递和检索数组 double s的方法:

The Bundle class has methods for passing and retrieving an array of doubles:

  • void putDoubleArray(String, double[])
  • double [] getDoubleArray(String)

然而,这些工作适用于一维数组。您正在尝试传递一个2D数组。我不知道有这样做的直接方法。实现这一点的一种方法是将N个双精度数组放在循环中。

However, these work for one-dimensional arrays. You are attempting to pass a 2D array. I do not know of a direct way to do this. One way to achieve this would be to put N arrays of doubles in a loop.

for(int i=0;i<tableCityPair.length;i++){
    bundle.put("tableCity"+i, tableCityPair[i]);
}

在接收端,您可以执行以下操作:

And at the receiving end, you do:

double [] aPair = it.getExtras().getDoubleArray("tableCity"+i);

我不知道这一点的性能影响;因为您将添加100个附加组件作为捆绑包的一部分。

I'm not sure about the performance implications of this though; since you would be adding 100 extras as part of the bundle.

可能会有一个更好的方法(可能使您的对成为列表< Double>> 然后实现 Parcelable ),但我没有尝试任何它,所以我不会建议。

There could be a better way (perhaps make your pair a List<List<Double>> and then implement Parcelable) but I haven't tried any of it so I wouldn't suggest it.

这篇关于Java Android数据传递数据双倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆