Java Android passing data Array double

2019-07-31 16:24发布

问题:

I have a question about intent in android application

i have an array in class1

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

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

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

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

回答1:

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

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

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);

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

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.



回答2:

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

and then... make intent it=.... then

put: `it.putExtra("size",tableCityPair.length)`

then:

    for(i=0;i<tableCitypair.length;i++)
{

it.putExtra("table"+i,tableVityPair[i][]);

}

THEN in CLASS B

double [][] tableCityPairinB = new double[100][100];
for(i=0;i<getIntent().getExtras("size");i++)
{
double[i][]=getIntent().getExtras("table"+i);

}