[英]What would be the best way of converting Object ArrayList to Double List
I've started to learn Java/Android and I am trying to gather RSSI values of bluetooth devices. 我已经开始学习Java / Android,并且正在尝试收集蓝牙设备的RSSI值。 I have 2 fragments on the same activity and every device I discover I show on a listview and keep collecting the data in a ArrayList which holds my object class. 我在同一活动中有2个片段,在列表视图中显示的每个设备都有两个片段,并继续将数据收集在保存我的对象类的ArrayList中。 later when I want to visualize the data for a device, I need to convert this object array list to ArrayList to get only RSSI values and show on the second fragment. 稍后,当我想可视化设备的数据时,我需要将此对象数组列表转换为ArrayList,以仅获取RSSI值并在第二个片段上显示。 My Problem is this conversion made my screens very slow. 我的问题是这种转换使我的屏幕非常慢。 Any suggestions? 有什么建议么? Thank you 谢谢
This is the function , I think it makes it slow; 这是功能,我认为它会使速度变慢;
public static ArrayList<Double> extractDeviceRSSIOnly(ObjDevice device,
ArrayList<ObjDevice> list)
{
ArrayList<Double> selectedList=new ArrayList<Double>();
if(device!=null&& list!=null) {
if (list.size() > 0) {
for(int i=0;i<list.size();i++)
{
if(list.get(i).get_macAddress().equals(device.get_macAddress())) {
selectedList.add(list.get(i).get_signalStrength());
}
}
}
return selectedList;
}
}
Your code looks good. 您的代码看起来不错。 You can make it a tiny bit more efficient by avoiding calling get(i)
twice. 您可以避免两次调用get(i)
从而使其效率更高一点。 The enhanced for will help you with that, and will give you a cleaner code. 增强了的功能将帮助您实现这一目标,并为您提供更简洁的代码。
As suggested by @Makoto, you can avoid the list.size() > 0
test too. 正如@Makoto所建议的,您也可以避免list.size() > 0
测试。 An empty list will simply not iterate. 空列表将不会重复。
if (device != null && list != null) {
for (ObjDevice objDevice : list) {
if (objDevice.get_macAddress().equals(device.get_macAddress())) {
selectedList.add(objDevice.get_signalStrength());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.