当前位置: 动力学知识库 > 问答 > 编程问答 >

Adding an array of doubles to an array list gives me weird (hex?) values - java

问题描述:

I'm getting problems when i try to add an array of doubles to an ArrayList.

ArrayList<double[]> vok = new ArrayList<double[]>();

And the method I'm having issues with is:

 public void onSensorChanged(SensorEvent e) {

if(e.sensor.getType()==Sensor.TYPE_MAGNETIC_FIELD){

double[] temp = new double[3] ;

double x = e.values[0];

temp[0]=x;

double y = e.values[1];

temp[1]=y;

double z = e.values[2];

temp[2]=z;

vok.add(temp);

System.out.println(vok);

}

When I hover over temp in debug, it displays the correct values e.g.

temp = [40.55999755859375, -20.100000381469727, -28.260000228881836]

I would like in the array but when it is added to "vok" i end up with symbols like [[email protected]] in the array (hex?). I have tried changing the method in which I add elements to vok = Arrays.asList(temp) and changing the corresponding types but I'm still having the same problem.

I'm not sure if there is a better method to do this than the one I'm using but I've been working on this all day and this is the closest I've got. My end goal is to have values stored like:

Double[][] vee = {{0.4222307801246643,-0.12258315086364746,-0.2996482849121094},

{-0.4222307801246643,-0.06810164451599121,0.1498241424560547},

{-0.1089627742767334,0.027240753173828125,0.23154544830322266},

{0.0,0.16344404220581055,0.04086112976074219}}

for use in another class that requires that type of input. Please help! :)

网友答案:

Don't do

System.out.println(vok);

Do

int i = 0;
for (double[] arr : vok) {
    System.out.println("vok_" + i++ + " = " + Arrays.toString(arr));
}

Your problem is that you are (indirectly) calling the toString() of a double[], which results in strings like [[email protected]] and not it's contents.

So your code is correct and does what you want it to do. All you have wrong is how you present the data on the console.

网友答案:

Instead of writing

System.out.println(vok);

You can use something like the following:

Iterator<double[]> e = vok.iterator();
while (e.hasNext())
{
    double[] array = e.next();
    // You can structure the output of the printing in whatever way makes sense for you
    System.out.print(Arrays.toString(array));
}

This is analogous to jlordo's answer, he just beat me to it by a few minutes and I wanted to post what I had done anyway.

分享给朋友:
您可能感兴趣的文章:
随机阅读: