java - 从列表中对象的特定字段创建数组

标签 java android

我不太确定如何正确表达。我有一个对象列表,这些对象有特定字段的 setter/getter 。我现在需要从 ojbect 列表中创建一个数组,但我只需要一个特定的数据。

有没有办法不使用看起来非常低效的交互器来做到这一点?

这是在 android 应用程序的上下文中。

最佳答案

    // our original list
    List<Integer> list = new ArrayList<Integer>();

    // inserting some values
    for(int i = 0;i<100;i++){
        list.add(i);
    }

    // work starts here :

    // select a range of elements based on index
    List<Integer> subList = list.subList(0, 50);
        // list.subList(from index-inclusive,to index-exclusive)

    // create an array to hold your new values
    Integer[] myArray = new Integer[0]; // must initialize

    // assign the part of your original list to this array
    myArray = subList.toArray(myArray);


    // test Result :
    System.out.println(Arrays.toString(myArray));
        // reult :
        /*
         * [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
         * 11, 12, 13, 14, 15, 16, 17, 18, 19, 
         * 20, 21, 22, 23, 24, 25, 26, 27, 28, 
         * 29, 30, 31, 32, 33, 34, 35, 36, 37, 
         * 38, 39, 40, 41, 42, 43, 44, 45, 46, 
         * 47, 48, 49]
         */

    // hope this was what you were looking for   

关于java - 从列表中对象的特定字段创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18904471/

相关文章:

java - 在 Java 中创建嵌套的 Fluent API

android - Android 遇到 Sun javax jar 问题

java - file.getAbsolutePath 如何工作?

java - 从 List<> 构建 ArrayList<>

java - Mac 设备访问可以在 C 中使用,但在 Java/JNA 中的等效代码则不行

java - XJB 模式注释无效字符 &(与号)

android - Activity 创建了两次

java - 底部导航 FindViewById 获取 null (kotlin)

android - Cloud Console 数据存储过滤器

java - 尝试用数据填充 View 时 AsyncLayoutInflater 崩溃