简体   繁体   English

将Object ArrayList转换为Double List的最佳方法是什么

[英]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());
        }
    }
}

转换 ArrayList 列表的最佳方法是什么<object>到自定义 object java11 Spring 引导 2.1<div id="text_translate"><p> 我有 ArrayList 对象列表,其中对象可以是文本、BigDecimal、Long</p><pre> List&lt;Object&gt; data = { {"date","prize1","cardsNumers","cardObtained","totalCards","anotherPrize"}, {"2020-05-01",435345.67,43,56,67,345345345}, {"2020-03-01",45656.2,34,67,23,456456}, {"2020-02-01",56565,34,56,67,878987} }</pre><p> arrayList 中的第 1、2、3 等对象是第 0 个 arraylist 中提到的每个字段的值。 在如下表中,</p><p> <a href="https://i.stack.imgur.com/Tmdw2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tmdw2.png" alt="在此处输入图像描述"></a></p><p> 我想从上面获取每个文件的总和并分配给下面自定义 Object 的同一字段,</p><pre> public class SumObject { private Date date; private BigDecimal sumPrize1; private BigDecimal sumCardsNumers; private long sumCardObtained; private long sumTotalCards; private long sumAnotherPrize; }</pre><p> 我通过遍历 List 中的 arrayList 来实现这一点,并通过 get(index) 方法获取值。 但我想知道是否有任何其他好的方法可以做到这一点。</p><p> 如果有人有任何建议,请发布答案或给我一个我可以阅读的文件的方向。</p><p> 提前致谢。</p></div></object> - what would be best way to convert List of ArrayList<Object> to a custom object java11 Spring boot 2.1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转换 ArrayList 列表的最佳方法是什么<object>到自定义 object java11 Spring 引导 2.1<div id="text_translate"><p> 我有 ArrayList 对象列表,其中对象可以是文本、BigDecimal、Long</p><pre> List&lt;Object&gt; data = { {"date","prize1","cardsNumers","cardObtained","totalCards","anotherPrize"}, {"2020-05-01",435345.67,43,56,67,345345345}, {"2020-03-01",45656.2,34,67,23,456456}, {"2020-02-01",56565,34,56,67,878987} }</pre><p> arrayList 中的第 1、2、3 等对象是第 0 个 arraylist 中提到的每个字段的值。 在如下表中,</p><p> <a href="https://i.stack.imgur.com/Tmdw2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Tmdw2.png" alt="在此处输入图像描述"></a></p><p> 我想从上面获取每个文件的总和并分配给下面自定义 Object 的同一字段,</p><pre> public class SumObject { private Date date; private BigDecimal sumPrize1; private BigDecimal sumCardsNumers; private long sumCardObtained; private long sumTotalCards; private long sumAnotherPrize; }</pre><p> 我通过遍历 List 中的 arrayList 来实现这一点,并通过 get(index) 方法获取值。 但我想知道是否有任何其他好的方法可以做到这一点。</p><p> 如果有人有任何建议,请发布答案或给我一个我可以阅读的文件的方向。</p><p> 提前致谢。</p></div></object> - what would be best way to convert List of ArrayList<Object> to a custom object java11 Spring boot 2.1 在Java中将List <Long>对象转换为long []数组的最佳方法是什么? - What is the best way of converting List<Long> object to long[] array in java? 实现常量对象的最佳方法是什么? - What would be the best way to implement a constant object? 在 ArrayList 线程安全上移动 object 的最佳方法是什么? - What is the best way to move an object on an ArrayList thread safe? 将原始向量转换为安全列表类型的最佳方法是什么(Arraylist) - What is the best way to convert a raw vector to type safe list(Arraylist) 填充列表的最佳方法是什么 <Object> ? - What's the best way to populate List<Object>? 将字符串ArrayList转换为Double ArrayList - Converting a String ArrayList into a Double ArrayList 访问存储在ArrayList中的对象的最佳方法? - Best way to access an object stored in an ArrayList? 将 double[] 转换为不可变列表的成本更低的方法 - Less expensive way of converting a double[] to an immutable list 根据已排序的 ArrayList 对 ArrayList 进行排序的最佳方法是什么? - What is the best way to sort an ArrayList based on a already sorted ArrayList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM