Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从列表中对象的特定字段创建数组_Java_Android - Fatal编程技术网

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

Java 从列表中对象的特定字段创建数组,java,android,Java,Android,我不太清楚这个词该怎么说。我有一个对象列表,这些对象有特定字段的getter。我现在需要从ojbect列表中创建一个数组,但我只需要一个特定的数据段 有没有办法不用一个效率非常低的互动器来做这件事 这是在android应用程序的上下文中 //我们的原始列表 // our original list List<Integer> list = new ArrayList<Integer>(); // inserting some values

我不太清楚这个词该怎么说。我有一个对象列表,这些对象有特定字段的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   
列表=新的ArrayList(); //插入一些值
对于(int i=0;i仅结束此问题-问题下方的评论是正确的“答案”。似乎没有更好(或更有效)的答案做我要求的事情的方法。

无论你使用迭代器还是索引循环,效率都是一样的。你必须以某种方式循环列表,据我所知,迭代器的循环速度和现有的一样快,因为没有太多的开销。你怎么会认为迭代器是循环的呢效率会很低吗?我的假设不好?我想我只是想在幕后有一种更好的方法。