gpt4 book ai didi

java - 链表有两种大小

转载 作者:行者123 更新时间:2023-12-02 07:22:42 26 4
gpt4 key购买 nike

我正在尝试将加速度计的值添加到 LinkedList 中。

private LinkedList<Float> rawValues = new LinkedList<Float>();
public void onSensorChanged(SensorEvent event)
{
int eventType = event.sensor.getType();

switch(eventType)
{
case Sensor.TYPE_ACCELEROMETER:
float accelerometer_z = event.values[2];


rawValues.add(accelerometer_z);

/**
* This printout gives me positions and event values
*/
System.out.println("getAccelerometer size: " +
rawValues.size() + " entry: " + rawValues.getLast());

break;
}
}

到目前为止一切顺利,我得到了预期的读数和数字位置。但是,当我尝试用其他方法对列表进行操作时,它似乎是空的!

public String[] getVelocity()
{
String[] velocityString = {"42","42"};
String values = "";
String theInteger = "";

/**
* Returns a 0 size...
*/
int theSize = rawValues.size();

System.out.println("getVelocity size: " + theSize);

LinkedList<Float> slopeValues = calculateSlopeValues( rawValues);

for (int i = 0; i < theSize; i++)
{
values += "Raw:"+i+ " " + String.valueOf(rawValues.get(i)) + "\n";
values += "\tSlope:"+i+ " " + String.valueOf(slopeValues.get(i)) + "\n";
}

theInteger = String.valueOf(Math.round(Collections.max(slopeValues)*100));

/**
* After adding everything to local variables, the list is cleared.
*/
slopeValues.clear();

velocityString[0] = theInteger;
velocityString[1] = values;


return velocityString;
}

getVelocity 方法是从主 Activity 的 onTouch 中调用的。请透露一些信息?

问候/M

重写 onTouch 方法:

@Override
public boolean onTouch(View view, MotionEvent event)
{
MySensorEventListener eventListener = new MySensorEventListener();

switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
String[] strings = eventListener.getVelocity();

String velocity_int = strings[0];
String velocity_string = strings[1];


velocityView.setText(velocity_int);
listView.setText(velocity_string);


break;
}

return false;
}

最佳答案

好吧,每次调用 onTouch() 时,您都会创建一个 MySensorEventListener 的新实例,并询问这个新实例的速度。由于每个 MySensorEventListener 实例都有自己的原始值链接列表,因此每次都会得到一个空列表。

监听器可能应该只创建一次,并存储在封闭类的实例字段中。

关于java - 链表有两种大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14010377/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com